LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Checking variable against multiple values (http://www.linuxquestions.org/questions/linux-newbie-8/checking-variable-against-multiple-values-865926/)

ksushants 03-02-2011 04:59 AM

Checking variable against multiple values
 
Hi,



I would appreciate if you help in scripting the following requirement:



I have to read the database name from the user. It should not be blank and it should match some specified values. I have scripted it as follows:



db=

while [ -z "$db" ] ;

do

echo "Enter the name of database :\c "

read db

if [ "$db" != "BE" –o "$db" != "DE" ] ; then

echo "ERROR: Please enter a valid database name."

db=

fi

done



The “while” part takes care for the blank value entered but when it comes to checking of the values in the “if” statement, the database name should match BE or DE. It fails in the “if” part.



atr@chfbsr147:/home/atr> ./abc.sh

Enter the name of database :\c [if entered blank value, it works]



ERROR: Please enter a valid database name

Enter the name of database :\c [if entered a wrong value, it works]

aa

ERROR: Please enter a valid database name

Enter the name of database :\c [entered a correct value still it fails]

BE

ERROR: Please enter a valid database name

Enter the name of database :\c [entered a correct value still it fails]

DE

ERROR: Please enter a valid database name

Enter the name of database :\c



Can you please suggest checking multiple values for the variable db.



Thanks in advance.

unSpawn 03-02-2011 06:21 AM

Code:

_help() { echo "You need help."; exit 1; }
hasDbName() { echo "Enter the name of database: "; read DBNAME; checkDbName; }
checkDbName() { case ${DBNAME} in BE|DE) continue;; *) hasDbName;; esac; }

[ $# -eq 0 ] && hasDbName

while getopts d:h OPT; do
 case "${OPT}" in
  d) DBNAME="${OPTARG}"; checkDbName;;
  h) _help;;
  *) hasDbName;;
esac; done

echo "Database = ${DBNAME}"

So now the user could run 'scriptname -d DE' or 'scriptname -d' or 'scriptname'.

colucix 03-02-2011 06:24 AM

One expression excludes the other, hence the result is always TRUE, that is every value of the db variable, BE, DE, AA, AB and so on brings to one or two TRUE expressions: if db equals BE it does not equal DE so that the logical OR between TRUE and FALSE gives always TRUE. Put a logical AND and you will get the desired result: if both the condition are TRUE, that is db is neither BE or DE, the user is prompted again. Hope it is clear.

devUnix 03-02-2011 07:33 AM

Here is the script that does what you want to do:

(First see this test to make sure this is exactly what you want and only then copy and paste the script) ;)

Sample Runs of the Script

Code:

-bash-2.05b# getDB.sh
Enter DB Name:
Enter DB Name: GO
Error: Please enter a valid DB Name.

-bash-2.05b# getDB.sh
Enter DB Name: DE
DB Name: DE [OKAY]

-bash-2.05b# getDB.sh
Enter DB Name: BE
DB Name: BE [OKAY]

-bash-2.05b#

Script: getDB.sh

Code:

#!/bin/bash
DBNAME=""
while [ `echo $DBNAME | awk '{print length}'` -eq 0 ]; do
        read -p "Enter DB Name: " DBNAME
done
if [ "$DBNAME" = "BE" ] || [ "$DBNAME" = "DE" ]; then
        echo -e "DB Name: $DBNAME [OKAY]\n"
else
        echo -e "Error: Please enter a valid DB Name.\n"
        exit 1
fi
exit 0


You can take out the last statement:

Code:

exit 0
incase you want to add more statements.

Note 1#

Code:

exit 1
tells the OS / Shell that the script exited with some error(s) or was not successful, whereas a status of "0" means successful termination of the script.


Note 2#

It is also important to check for whistespaces (blank spaces such as " ") which I have done using awk.

unSpawn 03-02-2011 08:35 AM

If you use /bin/bash, then isn't using "echo $DBNAME | awk '{print length}'" a bit convoluted? I mean it almost sounds like "cat|grep" as if there's no "[ ${#DBNAME} -eq 0 ]" or "test -z "${DBNAME}" (or n)...

devUnix 03-02-2011 09:10 AM

Quote:

Originally Posted by unSpawn (Post 4276369)
as if there's no "[ ${#DBNAME} -eq 0 ]" or "test -z "${DBNAME}" (or n)...


Please, have a look at these stuffs:

Code:

-bash-2.05b# weird="ONE"
-bash-2.05b# echo ${#weird}
3
-bash-2.05b# weird="  "
-bash-2.05b# echo ${#weird}
3
-bash-2.05b# test -z "${weird}"
-bash-2.05b# echo $?
1
-bash-2.05b# weird="ONE"
-bash-2.05b# test -z "${weird}"
-bash-2.05b# echo $?
1
-bash-2.05b# weird="  "
-bash-2.05b# echo $weird | awk '{print length}'
0
-bash-2.05b# weird="ONE"
-bash-2.05b#  echo $weird | awk '{print length}'
3
-bash-2.05b#

We do not want to get whitespaces as a valid input/value. So, to check for it, my method works excellently.

Please, give your views on it.

unSpawn 03-02-2011 09:27 AM

Hmm. The use of 'eval' comes to mind...


All times are GMT -5. The time now is 07:07 AM.