Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting What can i do to check that the input is all alphabet.. ?
# 1  
Old 01-27-2002
What can i do to check that the input is all alphabet.. ?

What can i do to check that the input is all alphabet.. ?
XXXXXXXXXX
# 2  
Old 01-28-2002
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.
Jimbo
# 3  
Old 01-28-2002
Quote:
Originally posted by Jimbo
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
XXXXXXXXXX
# 4  
Old 02-19-2002
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,
Smilie
inquirer
# 5  
Old 02-19-2002
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:

Code:
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$$
Jimbo

Previous Thread | Next Thread
Test Your Knowledge in Computers #75
Difficulty: Easy
Apple's Mac OS X was based on NeXTStep, but iOS was not based on Mac OS X.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Check Column corresponding to input

I have a file which extracts data from an HTML file For Eg HTML file contains: New York;ABC;145;Yes;YES;No New York;BCD;113;Yes;YES;No New York;NAS;63;Yes;YES;No ------------------------ London-48;CBT;16;Yes;YES;No London-48;CME;17;Yes;YES;No London-48;EUR;52;Yes;YES;No... (7 Replies)
Discussion started by: newkid.7955
7 Replies

2. Shell Programming and Scripting

Check user input

Hi, I need my script to check if the user enters 3 values if not 5 values to my script and alert if the input has any other number of values. for example: ./myscript.sh 22 56 3221 - > correct ./myscript.sh 22 56 3221 45 777 -> correct ./myscript.sh 22 56 3221 45 -> incorrect Please... (6 Replies)
Discussion started by: mohtashims
6 Replies

3. Shell Programming and Scripting

To Check whether the Input is a Year or Not?

Hi Guys I have a Requirement that the input will be given and i have to check whether the input is a Year or not. For Example 2004,2009 and so on forth will be considered a year and anything else like 12:15 or else will not be. Have built the below Code set -x echo " Enter... (6 Replies)
Discussion started by: Ajesh
6 Replies

4. Shell Programming and Scripting

Input file check

Hi, I have a script which runs daily. It gets 3 input files test1,test2,test3. I want to do a validation in my script to make sure i have all the 3 files available before running. If any one of the file is missing i want to break the script. Could you please help me with this request. ... (1 Reply)
Discussion started by: Krrishv
1 Replies

5. Shell Programming and Scripting

Check input parameter

Hi all i need to check that if user has passed any input parameter while executing he shell script like ./test1.sh -a"-v" then do smothing if user execute the script without giving input paramater then ./test1.sh then do something how can we check this input parameter (6 Replies)
Discussion started by: aishsimplesweet
6 Replies

6. Programming

How to check an input variable

Suppose we have a simple situation, like the following C++ instructions: int x; cout << "Insert x: "; cin >> x; while ( x-- < 0 ) ; Of course, if it is written something different from an integer, the while loop shall not end. So, how can we check if the input x is of the right type? (2 Replies)
Discussion started by: Luke Bonham
2 Replies

7. UNIX for Dummies Questions & Answers

checking wether an input is using letters of the alphabet

afternoon forums. I need to get a way of testing as to wether an inputed character is part of the english alphabet. i have come up with the following code but its not working at all. until '] do echo This is not a Letter done any help would be beneficial to me. (1 Reply)
Discussion started by: strasner
1 Replies

8. Shell Programming and Scripting

To check if the first character is a alphabet or number

Hi, I need to find whether the first character in a line is a alphabet or a number. If its a number i should sort it numerically. If its a alphabet i should sort it based on the ASCII value.And if it is something other than alphabet or number then sort it based on ASCII value. The code i used... (2 Replies)
Discussion started by: ragavhere
2 Replies

9. UNIX for Dummies Questions & Answers

check the input

how to check whether a given input is a number or a string? (1 Reply)
Discussion started by: Shilpi
1 Replies

10. Shell Programming and Scripting

Check on Input

HI all, I would like to know how the user can be restricted for entering only the number and not characters in sheel scripts.. Suppose code is like this echo 'Enter the number' read Value Now user may enter 'a' as value... But i want to disallow him for entering characters other than... (3 Replies)
Discussion started by: dhananjaysk
3 Replies

Featured Tech Videos