Check user input


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check user input
# 1  
Old 02-19-2013
Wrench 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[3 values entered]
./myscript.sh 22 56 3221 45 777 -> correct[5 values entered]
./myscript.sh 22 56 3221 45 -> incorrect[4 values entered]

Please let me know how-to ?
# 2  
Old 02-19-2013
Code:
case "$#" in
        3|5) echo correct ;;
        *) echo incorrect ;;
esac

# 3  
Old 02-19-2013
Try something like this

Code:
#!/bin/sh
if [ $# -ne 3 ]
then
echo "input is less than 3"
fi

Similarly for other you can check like this.
# 4  
Old 02-19-2013
Power

Quote:
Originally Posted by Vikram_Tanwar12
Try something like this

Code:
#!/bin/sh
if [ $# -ne 3 ]
then
echo "input is less than 3"
fi

Similarly for other you can check like this.
@Vikram: What i need is to check if the input is not equal to both 3 and 5. Please let me know how-to

Subbeh's code works for me but I dont know how to feed multiple statements upon check.
# 5  
Old 02-19-2013
Quote:
Originally Posted by mohtashims
@Vikram: What i need is to check if the input is not equal to both 3 and 5. Please let me know how-to

Subbeh's code works for me but I dont know how to feed multiple statements upon check.
You could use something like this:

Code:
case "$#" in
        3|5) correct=1 ;;
        *) echo incorrect ;;
esac

if [[ $correct -eq 1 ]] ; then
        echo do stuff
fi

This User Gave Thanks to Subbeh For This Post:
# 6  
Old 02-19-2013
The original case statement from Subbeh is best.

Have a read of the man page for ksh and find the section about case

If you want to exit when the number of arguments passed is not three or five, then do so in the case statement and then all processing continues after that, so:-
Code:
case "$#" in
        3|5) print "correct"
             ;;
        *)   print "incorrect"
             exit 99
             ;;
esac

print "You continuing processing here."



I hope that this helps,

Robin
Blackburn/Liverpool,
UK
This User Gave Thanks to rbatte1 For This Post:
# 7  
Old 02-19-2013
I agree with both Subbeh and rbatte1 that case statement is good in this scenario. However you can also try this.

Code:
#!/bin/sh
if [ $# -ne 3 ] && [ $# -ne 5 ]
then
echo "input is not 3 or 5"
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Would like to check user input for letters within a loop

Hi All, #!/bin/bash #Just trying to check if letters are in the user input. Any tips? # I have tried regexp and using 0-9 etc, i cannot get this to work either in just an if statement or while in a loop. echo "Please pick a number" read num if ; then echo "Please enter a number"... (7 Replies)
Discussion started by: jvezinat
7 Replies

2. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

3. Shell Programming and Scripting

How to check the user input to be valid using shell script?

How to check the user input to be valid using shell script? The valid input is in the format like as follows. 1. It can only have r,w,x or a hyphen and nothing else. 2. ensure the r, w, x are in the correct order. for example: rwxr-xr-x is a valid format. Thanks (5 Replies)
Discussion started by: hyeewang
5 Replies

4. Shell Programming and Scripting

How-To Check & Filter user input

Hi, On my Java webpage which invokes the shell script has two checkboxes viz ... apache and weblogic apache require one parameter i.e apache home from the user while Weblogic requires three or five params from the user vi.z weblogic_home or <jdk_home, config_home & pid>, username and... (4 Replies)
Discussion started by: mohtashims
4 Replies

5. Homework & Coursework Questions

Function to Check if string input from user is alphabetic only

Good Evening. I'm new to C. Can you please help me. I'm creating an error checking function, user will input a string, this will check if the input is all alphabet or all letters only. If there is a digit or other special char, it will print Error then ask input from user again. Here's my... (1 Reply)
Discussion started by: eracav
1 Replies

6. Shell Programming and Scripting

Script interacts with user , based on user input it operates

i have a script which takes input from user, if user gives either Y/y then it should continue, else it should quit by displaying user cancelled. #!/bin/sh echo " Enter your choice to continue y/Y OR n/N to quit " read A if then echo " user requested to continue " ##some commands... (7 Replies)
Discussion started by: only4satish
7 Replies

7. 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

8. UNIX for Dummies Questions & Answers

how to check the user input from terminal

Hello everybody!!! I am writing my own rm command in unix. I prompt the user to type if he wants to delete a file and then read what he typed. But how do i check what he typed? This is my program so far: echo 'Delete prog1.c (y/n)?' read yesOrNo if yesOrNo == 'y' then rm prog1.c... (6 Replies)
Discussion started by: mskart
6 Replies

9. Shell Programming and Scripting

Bash : how do i check the user input and make sure is only chracter or only number ?

Bash : how do i check the user input and make sure is only character or only number ? (7 Replies)
Discussion started by: CheeSen
7 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
Login or Register to Ask a Question