Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Bash - How to detect if a variable is more than a certain amount of characters

Writer Emily Wong
#/bin/bash
echo "This message may not be more than 8 characters"
read detect

How do I detect the number of letters? Would it be an IF statement?

if #detect var
then
else
fi

EDIT: How can I make it repeat until the user types a correct message?

5

2 Answers

You can use if-then-else-fi like this:

if [[ ${#detect} -gt 8 ]] ; then echo "Error message..." exit 1
else echo "Good to go..." exit 0
fi

exit 1 generally means failure and exit 0 generally means success. In either case your script ends immediately when exit is encountered so use them appropriately.

You can also use implied if-then which is fairly unique the the shell language:

[[ ${#detect} -gt 8 ]] && { echo "Error message..." ; exit 1 ; }
# successful code here
exit 0

If you didn't need an error message the { ... } braces aren't required for a single command eg:

[[ ${#detect} -gt 8 ]] && exit 1

can be used to say "If the variable named detect is > 8 then exit".


Enhancing the process

Generally it is considered rude to allow the user only one chance to input a string. The polite method is to ask for the string again after telling them it needs to be 8 characters or less. For example:

echo "Enter character string 1 to 8 characters long or press <CTRL>+C to exit."
while True ; do read StringVar [[ ${#StringVar} -ge 1 ]] && [[ ${#StringVar} -le 8 ]] && break echo "Sorry that string is ${#StringVar} long. Please try again."
done
# successful code here

In this case the program keeps requesting input until variable StringVar is obtained and it is greater than or equal to 1 AND it is less than or equal to 8 at which point the while look is broken out of with the break command.

Alternately the user can press Ctrl+C/kbd> to terminate the bash script.

The concise lines:

[[ ${#StringVar} -ge 1 ]] && [[ ${#StringVar} -le 8 ]] && break
echo "Sorry that string is ${#StringVar} long. Please try again."

... can be made ridiculously verbose like this:

if [[ ${#StringVar} -ge 1 ]] ; then if [[ ${#StringVar} -le 8 ]] ; then break else echo "Sorry that string is ${#StringVar} long. Please try again." continue fi
else echo "Sorry that string is ${#StringVar} long. Please try again." continue
fi

Although not incorrect it is wasteful to both the programmer's and system's time.

I assume that you want to ask the user for input again and again until the length of input less than 8 characters. If so, you can use while loop instead of if-else statements. In that, loop will continue for infinite times until the length of input is less than 8 characters. When the length of "input" is less than 8 characters only then the script continues. The code will something like:

#!/bin/bash
echo -n "Enter message with less than 8 characters: "
read detect
while [[ ${#detect} -gt 8 ]]
do echo -n "Length of the message was greater than 8. Try again...: " read detect
done
echo $detect
#and the rest of the code continues

Moreover, if you are running the script from root (/) folder, then the shebang #!bin/bash might work (which you mentioned in the original post) but if you are running that from elsewhere, include the absolute path of the interpreter, i.e. /bin/bash, therefore modifying shebang to #!/bin/bash.

4

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy