ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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 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.
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:
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:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.