What can i do to check that the input is all alphabet.. ?

What can i do to check that the input is all alphabet.. ?

expr "$answer" : "[A-Za-z]*$"

Above command will return non-zero (length of answer) if it consists of only upper/lower letters. To restrict it to only lowercase or only uppercase, just remove A-Z or a-z. As coded, does not allow for spaces in answer.

You can either capture the output in a variable, then test the variable, or you can nest the above command directly within an if-statement.

thanks

if you want to check if a variable is ALL alphabet:

echo $variable | grep [0-9] > test_alpha.log

if test -s test_alpha.log
then
echo "$variable is NOT ALL alphabet"
else
echo "$variable is ALL alphabet"
fi

thanks,
:slight_smile:

inquirer:

Solutions that involve creating a disk file and testing that disk file will be less efficient. Also, the solution will fail if you are not in a directory you can write to. Or if another unix account ran the script in the same directory, you may not have permission to overwrite their clutter, in which case your solution will get an error and then proceed to test the results of the already existing file.

You can avoid all these issues with solutions that do not create disk files. In many cases, you can simply test the results of a command instead of testing its output. For example, two different ways:

grep abc myfile > /dev/null
if [ $? -eq 0 ] ; then
   echo 'grep found something'
else
   echo 'grep found nothing'
fi

if grep abc myfile > /dev/null ; then
   echo 'grep found something'
else
   echo 'grep found nothing'
fi

Also, the poster wanted a test for alpha characters. My solution tests for one or more alpha characters, and will test false for anything else, including special characters, control characters, or a null response. Your solution does not rule these out, testing only for presence or absence of digits.

I do have complex scripts that require interim work files. For those, I write them to /tmp, I remove them at end of script, and to avoid multiple users stomping on each other's work files, I use the process ID in the filename, such as:

workfile1=/tmp/workfile1$$