LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   BASH if/elif variable problem (https://www.linuxquestions.org/questions/programming-9/bash-if-elif-variable-problem-655715/)

GNUJoshua 07-14-2008 03:36 PM

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?

GNUJoshua 07-14-2008 04:30 PM

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


FranDango 07-14-2008 05:34 PM

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

pxsnet 07-14-2008 08:18 PM

# 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

chrism01 07-14-2008 11:26 PM

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*" ]]

pxsnet 07-14-2008 11:38 PM

Quote:

Originally Posted by chrism01 (Post 3214705)
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.

jcookeman 07-15-2008 07:01 AM

Quote:

Originally Posted by chrism01 (Post 3214705)
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.


All times are GMT -5. The time now is 03:29 PM.