[SOLVED] Checking variable against multiple values
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
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.
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 400
Rep:
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.
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)...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.