LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-14-2008, 03:36 PM   #1
GNUJoshua
LQ Newbie
 
Registered: Jul 2008
Posts: 5

Rep: Reputation: 0
BASH if/elif variable problem


I am having a really rough time figuring this out. The following script always defaults to the first "if" instead of moving on to the elif statements.

Code:
DMID=/usr/bin/dmidecode 
MODEL=`$DMID -s system-product-name`
echo "DMI reports Hardware Identity: $MODEL" 

if [[ $MODEL=~ "HP Compaq 6910p*" ]]
	then
	echo "Please wait for 6910p"
elif [[ $MODEL=~ "HP Compaq dc7800p*" ]]
	then
	echo "Please wait for dc7800p"
else
echo "$MODEL unsupported currently or drivers not found." 
fi
I can even confirm that the first echo outputs "HP Compaq dc7800p Small Form Factor" but then it goes to "Please wait for 6910p" for some reason, leaving me to believe the script isn't moving past the first "if" statement. I cannot wrap my head around why...

The 1st problem is with the output of dmidecode, because I am dealing with output that could have spaces and could have "some" variation.


For example, if dmidecode outputs the following:
"HP Compaq dc7800p Small Form Factor"

I simply want to match "dc7800p" in the script so that if the data polled by DMI is in lowercase, or includes intl. keyboard codes, the script still goes to the relative place.

What am I missing here?
 
Old 07-14-2008, 04:30 PM   #2
GNUJoshua
LQ Newbie
 
Registered: Jul 2008
Posts: 5

Original Poster
Rep: Reputation: 0
Sorry guys... think I fixed it... lately it seems that no matter what the question, grep is the answer.

Code:
DMID=/usr/bin/dmidecode 
MODEL=`$DMID -s system-product-name`
echo "DMI reports Hardware Identity: $MODEL" 

if [ `echo $MODEL | grep -o 6910p` ]
	then
	echo "Please wait for 6910p"
elif [ `echo $MODEL | grep -o dc7800p` ]
	then
	echo "Please wait for dc7800p"
else
echo "$MODEL unsupported currently or drivers not found." 
fi
 
Old 07-14-2008, 05:34 PM   #3
FranDango
Member
 
Registered: Jun 2008
Posts: 101

Rep: Reputation: 15
Even though you fixed it already with a different approach, I'm still curious:

Quote:
if [[ $MODEL=~ "HP Compaq 6910p*" ]]
will result to this error message on my system:
Quote:
bash: test.sh: line 6: conditional binary operator expected
bash: test.sh: line 6: syntax error near `6910p*"'
bash: test.sh: line 6: `if [[ $MODEL=~ "HP Compaq 6910p*" ]]'
Is BASH supposed to deal with wild cards that way?

Linux Archive

Last edited by FranDango; 09-20-2008 at 04:56 AM.
 
Old 07-14-2008, 08:18 PM   #4
pxsnet
LQ Newbie
 
Registered: Mar 2004
Location: CHN
Distribution: gentoo, RHEL
Posts: 16

Rep: Reputation: 0
# move * out of the double quotation marks and then it should work
# and also try this out. i think "grep" is not so portable....

case "$MODEL" in
"HP Compaq 6910p"*)
echo 1
;;
"HP Compaq dc7800p"*)
echo 2
;;
esac

Last edited by pxsnet; 07-14-2008 at 08:24 PM.
 
Old 07-14-2008, 11:26 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
As per this page, http://tldp.org/LDP/abs/html/bashver3.html, you should have double quotes around both vars in a regex/if, and also, a space on either side of the regex operator (=~), so like this:

if [[ "$MODEL" =~ "HP Compaq 6910p*" ]]
 
Old 07-14-2008, 11:38 PM   #6
pxsnet
LQ Newbie
 
Registered: Mar 2004
Location: CHN
Distribution: gentoo, RHEL
Posts: 16

Rep: Reputation: 0
Quote:
Originally Posted by chrism01 View Post
As per this page, http://tldp.org/LDP/abs/html/bashver3.html, you should have double quotes around both vars in a regex/if, and also, a space on either side of the regex operator (=~), so like this:

if [[ "$MODEL" =~ "HP Compaq 6910p*" ]]

That's the point. =)

have var in lhs quoted is always suggested.
 
Old 07-15-2008, 07:01 AM   #7
jcookeman
Member
 
Registered: Jul 2003
Location: London, UK
Distribution: FreeBSD, OpenSuse, Ubuntu, RHEL
Posts: 417

Rep: Reputation: 33
Quote:
Originally Posted by chrism01 View Post
As per this page, http://tldp.org/LDP/abs/html/bashver3.html, you should have double quotes around both vars in a regex/if, and also, a space on either side of the regex operator (=~), so like this:

if [[ "$MODEL" =~ "HP Compaq 6910p*" ]]
The link you refer to shows an example for BASH_VERSION < 3.2. The example you show will work, for instance, in this case. However, pxsnet is correct:

Code:
if [[ "$MODEL" =~ "HP Compaq 6910p"* ]]
is the more correct version. Your version simply will not work in Bash 3.2.

To be honest, there is no reason in this case to use extended regex, anyway.

Code:
if [[ "$MODEL" == "HP Compaq 6910p"* ]]
will work in either instance and save you the headache.
 
  


Reply

Tags
bash, scripting, variable


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
Bash Script Variable Problem jimmy512 Programming 5 05-11-2007 07:38 AM
Bash Awk Variable problem _hadi_ Programming 5 12-13-2006 12:25 AM
Bash if, elif, else not working Blackout_08 Programming 7 06-09-2006 10:28 AM
bash scripting question - elif ?? xscousr Programming 2 08-12-2003 09:56 AM
Bash Variable Problem decates Linux - Newbie 10 01-28-2003 10:47 PM


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

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