LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 08-08-2017, 01:08 AM   #1
iorih0304
LQ Newbie
 
Registered: Jul 2017
Posts: 26

Rep: Reputation: Disabled
shell script : if and variable


Hi all.
I write a shell script to check the bios version as follow:
Code:
#!/bin/bash
# Bios version check
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "BIOS version check..."
bios_ver=`dmidecode -s bios-version`

echo "Version : $bios_ver"
echo "Expected vesion : 080015"
echo "--------------------------------------"

if [ "${bios_ver}" == "080015" ]; then
        echo "Bios test : pass"
else
        echo "Bios test : fail"
fi
but the result shows fail:
Code:
BIOS version check...
Version : 080015
Expected version : 080015
--------------------------------------
Bios test : fail
I think this problem is the variable bios_ver, but I have no idea how to fix.

Thanks.
 
Old 08-08-2017, 01:29 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
The syntax is different from other programming languages.

Code:
if [ "${bios_ver}" = "080015" ]; then
...
Or

Code:
if [ "${bios_ver}" -eq "080015" ]; then
...
The first example is if you are going to treat it as a string. The second is if you are working with integers.

See "man test".
 
1 members found this post helpful.
Old 08-08-2017, 01:34 AM   #3
iorih0304
LQ Newbie
 
Registered: Jul 2017
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thank you for your help.
I try second example, it works!
 
Old 08-08-2017, 02:25 AM   #4
dejank
Member
 
Registered: May 2016
Location: Belgrade, Serbia
Distribution: Debian
Posts: 229

Rep: Reputation: Disabled
It will pass no matter if you compare string or number, because in your case those are same, if you do not use curly braces. Also, you do not need quoting around variable and that number in test.

Just checked here with few things included:

Code:
dejan@ddeb:~$ bios_version=080015
dejan@ddeb:~$ if [[ $bios_version = 080015 ]] ; then  echo "pass"; else echo "fail"; fi
pass
dejan@ddeb:~$ if [[ ${bios_version} = 080015 ]] ; then  echo "pass"; else echo "fail"; fi
pass
dejan@ddeb:~$ if [[ "${bios_version}" = 080015 ]] ; then  echo "pass"; else echo "fail"; fi
pass
dejan@ddeb:~$ if [[ "${bios_version}" = "080015" ]] ; then  echo "pass"; else echo "fail"; fi
pass
dejan@ddeb:~$ if [ "${bios_version}" = "080015" ] ; then  echo "pass"; else echo "fail"; fi
pass
dejan@ddeb:~$ if [ "${bios_version}" == "080015" ] ; then  echo "pass"; else echo "fail"; fi
pass
dejan@ddeb:~$ if [ "${bios_version}" -eq "080015" ] ; then  echo "pass"; else echo "fail"; fi
pass
dejan@ddeb:~$
All are pass. Also, output of demidecode on my comp includes letter, so you might want to compare string, not integer for portability of your script.

Last edited by dejank; 08-08-2017 at 02:43 AM.
 
1 members found this post helpful.
Old 08-09-2017, 02:04 AM   #5
iorih0304
LQ Newbie
 
Registered: Jul 2017
Posts: 26

Original Poster
Rep: Reputation: Disabled
Sorry , I have another question
I write a shell script to check the MAC address as follow:
Code:
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

mac_address_sys=`ifconfig | grep 'HWaddr' | sed 's/^.*HWaddr //g'`
if [ "${mac_address_sys}" = "00:0c:29:b3:f7:54" ]; then
        echo "Check OK!"
else
        echo "Fail"
        echo "mac_address_sys is: "$mac_address_sys
fi
But the result is:
Code:
Fail
mac_address_sys is: 00:0c:29:b3:f7:54
I take it as a string, but it doesn't work.

Last edited by iorih0304; 08-09-2017 at 03:01 AM.
 
Old 08-09-2017, 02:23 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,617
Blog Entries: 3

Rep: Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871Reputation: 2871
You are probably getting leading or trailing whitespace inside your string.

Try this instead:

Code:
mac_address_sys=$(ifconfig | sed '/HWaddr/!d; s/^.*HWaddr //; s/ *$//; q;')
The grep is not needed if you have sed.

"/HWaddr/!d" deletes all incoming lines without the string HWaddr

Those that get through are subjected to two substitutions.

And it goes without saying that what's left gets printed, since the -n option was not used.

Then sed quits after that so that in effect only the first interface is used.

Last edited by Turbocapitalist; 08-09-2017 at 03:03 AM. Reason: close quote
 
1 members found this post helpful.
Old 08-09-2017, 03:01 AM   #7
iorih0304
LQ Newbie
 
Registered: Jul 2017
Posts: 26

Original Poster
Rep: Reputation: Disabled
Thank you for your comment. It really explain why my code doesn't work.
I try your code, it can work.
Thanks a lot.
 
Old 08-09-2017, 03:03 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,510

Rep: Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552
you may try to use a delimiter:
Code:
echo "mac_address_sys is: '$mac_address_sys'"
and you will see if there was something strange before/after that address.
 
1 members found this post helpful.
Old 08-09-2017, 03:05 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,510

Rep: Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552
Quote:
Originally Posted by Turbocapitalist View Post
You are probably getting leading or trailing whitespace inside your string.

Try this instead:

Code:
mac_address_sys=$(ifconfig | sed '/HWaddr/!d; s/^.*HWaddr //; s/ *$//; q;')
The grep is not needed if you have sed.
awk is a bit more readable:
Code:
mac_address_sys=$(ifconfig | awk '/HWaddr/ {print $NF; exit}')
 
2 members found this post helpful.
Old 08-09-2017, 03:42 AM   #10
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,635

Rep: Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202
Just for fun, a grep solution.
Code:
mac_address_sys=$(ifconfig | grep -o "..:..:..:..:..:..")
 
1 members found this post helpful.
Old 08-09-2017, 05:59 AM   #11
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,510

Rep: Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552Reputation: 5552
and a shorter sed:
Code:
ifconfig | sed -n '/HWaddr/{s/.*ddr //;s/ *$//;p;q}'
 
1 members found this post helpful.
Old 08-09-2017, 07:31 AM   #12
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,635

Rep: Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202Reputation: 2202
and a shorter grep
Code:
ifconfig | grep -oP "(?>..:){5}.."
 
1 members found this post helpful.
Old 08-09-2017, 10:10 PM   #13
iorih0304
LQ Newbie
 
Registered: Jul 2017
Posts: 26

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
awk is a bit more readable:
Code:
mac_address_sys=$(ifconfig | awk '/HWaddr/ {print $NF; exit}')
ifconfig | sed -n '/HWaddr/{s/.*ddr //;s/ *$//;p;q}'
It looks more readable. Thanks.

Quote:
Originally Posted by allend View Post
Just for fun, a grep solution.
Code:
mac_address_sys=$(ifconfig | grep -o "..:..:..:..:..:..")
In this case , the code is more simple to solve.
 
  


Reply


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
[SOLVED] $variable in shell script RudraB Programming 4 06-28-2013 04:48 AM
Shell Script and Dynamic variable xanthium Programming 11 07-12-2011 06:05 AM
pass variable from one shell script into another shell script xskycamefalling Programming 9 10-03-2009 01:45 AM
Shell script --cannot assign variable-- ralvez Programming 6 02-24-2006 04:56 PM
expanding variable in shell script dipenchaudhary Programming 8 02-08-2006 05:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:51 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration