Command line user input validation


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Command line user input validation
# 1  
Old 10-14-2010
Command line user input validation

Hi guys, I have a piece of snippet below which asks the user to input some numbers

Code:
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
    fi

read daemonOption

i=1

if [ "$daemonOption" = '7' ] ; then
   return
fi

if [ "$daemonOption" = "" ] ; then
   echo "Error..."
   stop_daemons;
   return
else
   daemonLength=`expr length $daemonOption`
   while [ "$i" -lt $daemonLength+1 ]
   do
      substring=`expr substr $daemonOption $i 1`
      if [ "$substring" != '1' ] && [ "$substring" != '2' ] && [ "$substring" != '3' ] && [ "$substring" != '4' ] && [ "$substring" != '5' ] && [ "$substring" != '6' ]
      then
         echo "Error..."
         stop_daemons;
         return
      fi
   let i++
   done
fi

The problem with the above is for example:

if datasub and recon are both off, then the user will see the following
echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"

But the user can still enter the number 2 or 5, which is not right

I need some way of only allowing the user to enter the numbers that are on the menu, any ideas guys?
# 2  
Old 10-14-2010
Have you tried using "case" statement?

Code:
read daemonOption

case "${daemonOption}" in
     "value 1" | "value 11") <do something>;;
     "value 2") <do something>;;
     "value n") <do something>;;
     *) echo "Invalid value.";;
esac

# Example
a=10
case "$a" in
	"1" | "11") echo 1;;
	"2") echo 2;; 
	"10") echo 10;;
	*) echo "Invalid value"
esac

Also, if you want only numbers, you can use:
Code:
# ...
read daemonOption

expr ${daemonOption} + 1 1>/dev/null 2>&1
exprRetCode=${?}

if [ ${exprRetCode} -ne 0 ]
then
       echo "WARN: Only number allowed."
fi

Regards!
# 3  
Old 10-14-2010
Many thanks for the reply felipe. But is there a way to set the validation when the options are echo'd, for example:
Code:
    if isDatasubEnabled && isReconEnabled; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput = "1234567";
    
    elif isDatasubEnabled && isReconEnabled=0; then
        echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput = "123467";

    elif isDatasubEnabled=0 && isReconEnabled; then
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
        allowedInput = "13457";
        
    else
        echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
        allowedInput = "13467";
    fi

and then somehow used this allowedInput variable in the 2nd part of the code to validate the input? Because what the code will eventually do at the end is send the daemonOption to a oracle procedure

The full function:
Code:
function stop_daemons {
echo "Stop Daemons - Please enter one or a combination of the following:"

	if isDatasubEnabled && isReconEnabled; then
		echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
	
	elif isDatasubEnabled && isReconEnabled=0; then
		echo "1 = CGT, 2 = Subscriber, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"

	elif isDatasubEnabled=0 && isReconEnabled; then
		echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 5 = Reconciliation, 6 = All, 7 = Exit"
		
	else
		echo "1 = CGT, 3 = Order Monitor, 4 = Revaluations, 6 = All, 7 = Exit"
	fi

read daemonOption

i=1

if [ "$daemonOption" = '7' ] ; then
   return
fi

if [ "$daemonOption" = "" ] ; then
   echo "Error..."
   stop_daemons;
   return
else
   daemonLength=`expr length $daemonOption`
   while [ "$i" -lt $daemonLength+1 ]
   do
      substring=`expr substr $daemonOption $i 1`
      if [ "$substring" != '1' ] && [ "$substring" != '2' ] && [ "$substring" != '3' ] && [ "$substring" != '4' ] && [ "$substring" != '5' ] && [ "$substring" != '6' ]
      then
         echo "Error..."
         stop_daemons;
         return
      fi
   let i++
   done
fi

  sqlplus -S / <<EOF

BEGIN
  pfs245p.stop_daemons('$daemonOption');
END;
/

EOF

  daemons
}

# 4  
Old 10-15-2010
Check below, it may help you!
Code:
#!/bin/bash

isNumber ()
{
	# Validates if argument is a number or not
	# return/echo the argument if it is a number 
	# return -1 if it is not a number
	errRetCode="-1"
	
	var="${1}"
	
	# expr exit status:
	#	0 if the expression is neither null nor 0,
	#	1 if the expression is null or 0,
	#	2 if the expression is syntactically invalid,
	#	3 if an error occurred.
	var=`expr ${var} + 0 2>/dev/null`
	[ ${?} -gt 1 ] && var=${errRetCode}
	echo ${var}
	return ${var}
}

validateOpt ()
{
	# Validates a input value against a list of values
	# returns 0 if is a valid opt
	# returns 1 if it is a 
	inputOpt="${1}"
	allInputOpt="${2}"
	
	errRetCode="-1"
	
	nInputOpt=`isNumber "${inputOpt}"`
	if [ ${nInputOpt} -eq -1 ]
	then
		echo "ERROR: Invalid number: [${inputOpt}]."
		return ${errRetCode}
	fi
	
	for validOpt in ${allInputOpt}
	do
		if [ ${nInputOpt} -eq ${validOpt} ]
		then
			echo "INFO: Option is valid: [${inputOpt}]."
			return 0
		fi
	done
	
	echo "ERROR: Invalid option: [${inputOpt}]."
	return ${errRetCode}
}

# Some Tests

validateOpt "1" "1 2 4 5 10"
validateOpt "10" "1 2 4 5 10"
validateOpt "-1" "1 2 4 5 10"
validateOpt "a" "1 2 4 5 10"

validateOpt "1" "1 2 4 5 10"
if [ ${?} -ne 0 ]
then
	# Either user entered an invalid number or an invalid option
fi

Regards.
# 5  
Old 10-16-2010
Thanks alot for this felipe - it looks like it is exactly what I need. I will combine this function with my stuff and post back!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Input Email ID Validation

Hi Experts, I've a script where users will input the email id. I want to loop the script until the user enter proper email id. Its validating for all condition and error out if any wrong data, but for one condition alone its not erroring out. Capture_EmailID() { echo -e "Please Enter... (1 Reply)
Discussion started by: senthil.ak
1 Replies

2. Windows & DOS: Issues & Discussions

Put the numeric validation in user input when value is 5.1.2.3

Hi I need to put the validation in batch script when user will enter the build number it should be numeric.I can put the validation for numeric values but there is .(dot) in number so it would not take it as numeric. Is it possible we can store it in variable and remove the .(dot) from the... (1 Reply)
Discussion started by: anuragpgtgerman
1 Replies

3. Windows & DOS: Issues & Discussions

Validation in user input in batch script

I need to insert the validation in my batch script.When user enter the value it should be numeric+minimum length should be 2 for e.g. 02,03 if he puts 1a,1A,2a3 t hen should print the message that it is wrong and print te message enter valid value. Echo RC is in format of 02 set /p... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

4. Shell Programming and Scripting

How to take input from the user from the command line and execute commands basedon that?

Hi, I am using solaris 10 and bash shell.Script execution follows below.Initially it will check whether a directory exists or not if does not exist it will create it.(This I have completed) Second step:I have four users say user1,user2,user3,user4.Script should prompt for the user id and... (11 Replies)
Discussion started by: muraliinfy04
11 Replies

5. UNIX for Dummies Questions & Answers

Delete a line containing a string from user input

I have code that accepts input from a user, and when the user hits enter it is supposed to delete that whole line from the file. echo "Which record? " read record sed '/$record/d' file However, it does not delete it. Any help? (1 Reply)
Discussion started by: itech4814
1 Replies

6. Shell Programming and Scripting

Input validation of file

Hi, I'm trying to do input validation for a file. If the user fails to enter a file name, the script should prompt for the file name or exit. I'm using this script to integrate with a bigger script which does ftp of the files which are "read". #!/bin/ksh echo "enter a file name" if then... (16 Replies)
Discussion started by: sam_bd
16 Replies

7. Shell Programming and Scripting

how to do validation the input requirement with if/else

Hello Everyone, I have the following script; ------------------------------------------------------------------ #!/bin/ksh set HSC=$1 set SEX=$2 set EXP='export ORACLE_SID=AFISDB' if ($#argv !=2); then echo Usage $0 HSC SEX else date > modify_gender_trace.txt $EXP echo... (5 Replies)
Discussion started by: wingcross
5 Replies

8. UNIX for Dummies Questions & Answers

How to read a line of text from user input?

Hiii I wanna a read a line of text from standard input. The user enter data like this way name phone_no month1_salary month2_salary that is user enter the name ,phone no and salary of 2 months in a single line by giving spaces. I wanna add the 3rd and 4th fields ...ie add both... (4 Replies)
Discussion started by: krishnampkkm
4 Replies

9. Shell Programming and Scripting

Command line inputs validation

Hi, I have a script called read.sh that takes a file as input. Now I want to make that script take the file as input with a -f option preceding the filename. How can I do this validation. How can I find whether the given option is -f or not inside the script. Thanks in advance (2 Replies)
Discussion started by: sendhilmani123
2 Replies

10. Shell Programming and Scripting

Input validation ?

I am trying to validate user input, at the moment what i have is as below :- echo "\n\tPlease, enter package name to import: \c" read PACKAGE So far this works perfect , But if the user does not input any thing it stalls :( What I need is, If the user does not enter the name of the... (9 Replies)
Discussion started by: systemali
9 Replies
Login or Register to Ask a Question