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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-08-2017, 01:08 AM
|
#1
|
LQ Newbie
Registered: Jul 2017
Posts: 26
Rep: 
|
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.
|
|
|
08-08-2017, 01:29 AM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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.
|
08-08-2017, 01:34 AM
|
#3
|
LQ Newbie
Registered: Jul 2017
Posts: 26
Original Poster
Rep: 
|
Thank you for your help.
I try second example, it works!
|
|
|
08-08-2017, 02:25 AM
|
#4
|
Member
Registered: May 2016
Location: Belgrade, Serbia
Distribution: Debian
Posts: 229
Rep: 
|
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.
|
08-09-2017, 02:04 AM
|
#5
|
LQ Newbie
Registered: Jul 2017
Posts: 26
Original Poster
Rep: 
|
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.
|
|
|
08-09-2017, 02:23 AM
|
#6
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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 kakistocrat; 08-09-2017 at 03:03 AM.
Reason: close quote
|
|
1 members found this post helpful.
|
08-09-2017, 03:01 AM
|
#7
|
LQ Newbie
Registered: Jul 2017
Posts: 26
Original Poster
Rep: 
|
Thank you for your comment. It really explain why my code doesn't work.
I try your code, it can work.
Thanks a lot.
|
|
|
08-09-2017, 03:03 AM
|
#8
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,624
|
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.
|
08-09-2017, 03:05 AM
|
#9
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,624
|
Quote:
Originally Posted by Turbocapitalist
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.
|
08-09-2017, 03:42 AM
|
#10
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,575
|
Just for fun, a grep solution.
Code:
mac_address_sys=$(ifconfig | grep -o "..:..:..:..:..:..")
|
|
1 members found this post helpful.
|
08-09-2017, 05:59 AM
|
#11
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,624
|
and a shorter sed:
Code:
ifconfig | sed -n '/HWaddr/{s/.*ddr //;s/ *$//;p;q}'
|
|
1 members found this post helpful.
|
08-09-2017, 07:31 AM
|
#12
|
LQ 5k Club
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,575
|
and a shorter grep
Code:
ifconfig | grep -oP "(?>..:){5}.."
|
|
1 members found this post helpful.
|
08-09-2017, 10:10 PM
|
#13
|
LQ Newbie
Registered: Jul 2017
Posts: 26
Original Poster
Rep: 
|
Quote:
Originally Posted by pan64
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
Just for fun, a grep solution.
Code:
mac_address_sys=$(ifconfig | grep -o "..:..:..:..:..:..")
|
In this case , the code is more simple to solve. 
|
|
|
All times are GMT -5. The time now is 02:07 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|