Validating user input


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Validating user input
# 1  
Old 11-22-2009
Validating user input

I'm trying to set up a script that takes user input and validates that the user input was entered correctly.

So far I have this:

Code:
while :
do
        echo "Please enter your name."
        read NAME
        if [$NAME = ""]

        then
        echo "You have not entered a name."
        echo "Please try again."
        continue

        else
        echo "Your name is $NAME."
        break

        fi
done


if I leave it blank it asks me to re-enter a name. Now my question is how would I ask the user to re-input a name if they put a number within their name... ie Al45an.

Thanks.

Last edited by fufaso; 11-22-2009 at 09:21 AM.. Reason: code tags, please...
# 2  
Old 11-22-2009
Code:
if [ "$NAME" = "" ]

# 3  
Old 11-22-2009
bash ..

Code:
        if [ -z $NAME ]

# 4  
Old 11-22-2009
Thanks guys, that fixed my issue.

Any ideas how to validate that the user put in a name as oppose to numbers?
As it stands, if the user inputs 123 for their name, that's what stores...
# 5  
Old 11-23-2009
In bash/ksh you could use e.g.
Code:
if [ "$NAME" = "" ] || [ "${NAME//[!0-9]}" != "" ];

But I would just use a case statement, which is universal and easier to understand:
Code:
case $NAME in 
  ""|*[0-9]*) 
        echo "You have not entered a name."
        echo "Please try again."
        ;;
  *)
        echo "Your name is $NAME."
        break
        ;;
esac

# 6  
Old 11-23-2009
This is what I've got with the code you showed me, and it's catching numbers, but it's also catching letters and there is an endless loop of re-entering the name regardless of what is inputted.

Code:
while :
do
        echo "Please enter your name."
        read NAME
        if [ "$NAME" = "" ] || [ "{$NAME//[!0-9]}" != "" ];
        then
        echo "You have not entered a name."
        echo "Please try again."
        continue

        else
        echo "Your name is $NAME."
        break

        fi
done

# 7  
Old 11-23-2009
Hi fufaso
Code:
"${NAME//[!0-9]}"

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to get the user input recursively until the user provides valid input

Hi, echo "Enter file name of input file list along with absolute path : " read inputFileList if then for string in `cat inputFileList` do echo $string done else echo " file does not exist" fi From the above code, if the user enters a invalid file... (1 Reply)
Discussion started by: i.srini89
1 Replies

2. Shell Programming and Scripting

Validating uppercase/lowercase of user input with perl compared to unix folders

Hi, I need to copy files from a source directory to a destination directory in unix. I'm using the file::copy for the actual copy. The problem is that the source and dest directories are supplied by different users, who might type the name of the directories in various combinations of lower... (6 Replies)
Discussion started by: Furou
6 Replies

3. Shell Programming and Scripting

validating user entered date

I need the date validation. I searched in the google but i didn't find my requirements. requirements: 1) user has to enter the date in YYYY/MM/DD format 2) MM validations 3) DD validations. and if the month is april it should allow 30 days only and for May month it should allow 31 days like... (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

4. Shell Programming and Scripting

Validating Input parameters

Hi All, I have tried to use ckdate (sun) command in script. It checks the input parameter which should be in 'YYYYMMDD format. date=$( echo $1 | ckdate -f "%Y%m%d") | true if ] then print " success" else print "no success" fi But in whatever format i pass the parameter,... (3 Replies)
Discussion started by: Amit.Sagpariya
3 Replies

5. UNIX for Dummies Questions & Answers

Validating input based on fixed number of fields

Yes, i did... let me state my problem in more detail Inputs: I have one input CSV file And, i have stored no. of comma each line should in a variable. e.g. $ cat cmt.csv this, is a ,comma ,count test1 ,,this, is a ,comma ,count test2 this, is a ,comma ,count test3... (6 Replies)
Discussion started by: Dipali
6 Replies

6. Shell Programming and Scripting

validating a input file for numeric and character

i have a input file like this 001|rahim|bajaj|20090102 while reading the file i need to check whether the first column is a number second column is a name is there any methodology to check for the same thanks in advance (2 Replies)
Discussion started by: trichyselva
2 Replies

7. Shell Programming and Scripting

Validating user input is not blank

Trying to create a script in BASH that would ask the user to enter another user name making sure the input is not blank before they hit enter then to check the home directory of that user does exist, I have the check folder sorted it's just the loop to make sure the user has entered chars (5 Replies)
Discussion started by: MBN
5 Replies

8. Shell Programming and Scripting

Validating the input date format

I have one script.for that the inputs are fromdate(dd/mon/yyyy) and todate(dd/mon/yyyy). How i can validate the input format?for eg.27/08/2008 is not valid.27/aug/2008 or 27/Aug/2008 are valid. and the todate is optional.so if the todate is not present in the input then i need to assign the... (6 Replies)
Discussion started by: Sharmila_P
6 Replies

9. Shell Programming and Scripting

validating input using regular expressions

I am trying to validate input from the user in a script. I thought is was easy to do using regular expressions but I can't figure out how to use REs in a conditional. I have tried using grep from a temp file, sed from a temp file, sed from command line, comparison in an if condition and I cannot... (1 Reply)
Discussion started by: nrodolfich
1 Replies

10. Programming

validating input

how do i validate y script so that it only accepts values between 1 and 3 and against any character input, cause at the moment i can only validate against numbers outside 1 and 3 but not characters cheers (4 Replies)
Discussion started by: ruffenator
4 Replies
Login or Register to Ask a Question