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 ?
Subbeh
February 19, 2013, 8:15am
2
case "$#" in
3|5) echo correct ;;
*) echo incorrect ;;
esac
Try something like this
#!/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.
Subbeh
February 19, 2013, 8:37am
5
You could use something like this:
case "$#" in
3|5) correct=1 ;;
*) echo incorrect ;;
esac
if [[ $correct -eq 1 ]] ; then
echo do stuff
fi
1 Like
rbatte1
February 19, 2013, 8:43am
6
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:-
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
1 Like
I agree with both Subbeh and rbatte1 that case statement is good in this scenario. However you can also try this.
#!/bin/sh
if [ $# -ne 3 ] && [ $# -ne 5 ]
then
echo "input is not 3 or 5"
fi