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 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? |
Sorry guys... think I fixed it... lately it seems that no matter what the question, grep is the answer.
Code:
DMID=/usr/bin/dmidecode |
Even though you fixed it already with a different approach, I'm still curious:
Quote:
Quote:
Linux Archive |
# 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 |
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*" ]] |
Quote:
That's the point. =) have var in lhs quoted is always suggested. |
Quote:
Code:
if [[ "$MODEL" =~ "HP Compaq 6910p"* ]] To be honest, there is no reason in this case to use extended regex, anyway. Code:
if [[ "$MODEL" == "HP Compaq 6910p"* ]] |
All times are GMT -5. The time now is 03:29 PM. |