LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 03-02-2011, 04:59 AM   #1
ksushants
LQ Newbie
 
Registered: Apr 2009
Posts: 10

Rep: Reputation: 0
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.
 
Old 03-02-2011, 06:21 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,140
Blog Entries: 54

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
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'.
 
1 members found this post helpful.
Old 03-02-2011, 06:24 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,490

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
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.
 
2 members found this post helpful.
Old 03-02-2011, 07:33 AM   #4
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 550

Rep: Reputation: 46
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.

Last edited by devUnix; 03-02-2011 at 07:35 AM.
 
Old 03-02-2011, 08:35 AM   #5
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,140
Blog Entries: 54

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
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)...
 
Old 03-02-2011, 09:10 AM   #6
devUnix
Member
 
Registered: Oct 2010
Location: Bengaluru, India
Distribution: RHEL 5.1 on My PC, & SunOS / Sun Solaris, RHEL, SuSe, Debian, FreeBSD and other Linux flavors @ Work
Posts: 550

Rep: Reputation: 46
Quote:
Originally Posted by unSpawn View Post
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.

Last edited by devUnix; 03-02-2011 at 09:11 AM.
 
Old 03-02-2011, 09:27 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,140
Blog Entries: 54

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
Hmm. The use of 'eval' comes to mind...
 
  


Reply

Tags
shell


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get variable values from the keyboard Kenny_Strawn Programming 2 07-12-2010 03:56 AM
Not able to invoke variable values vjayraghavan Programming 1 05-05-2010 02:22 AM
Values like VARIABLE[i] ? BlueSpirit Programming 5 04-27-2007 01:35 PM
checking for null values from config file? zerointeger Programming 1 10-12-2005 11:29 AM
Checking for repeat values techrolla Programming 4 12-09-2003 08:41 PM


All times are GMT -5. The time now is 05:40 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration