LinuxQuestions.org
Visit Jeremy's Blog.
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 02-26-2015, 03:13 PM   #1
linuxmantra
Member
 
Registered: Dec 2013
Posts: 112

Rep: Reputation: Disabled
Value comparison in bash shell script


I need to compare a value with different fixed value. Below is the code concept I implemented in my bash script but its giving me error.
Here "a' is the nameserver value that I extract using command line from /etc/resolv.conf. I did not put command here.

#! /bin/bash
a=1.2.3.4

if [ $a == 1.2.3.4 || 5.6.7.8 || 8.9.1.4]
then
echo " You have correct ntpserver"
else
echo"wrong ntpserver"

fi

Am I comparing the value correctly or any suggestion?

Thank you
 
Old 02-26-2015, 06:58 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,628

Rep: Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568
There is no "==" in bash and the || (or) should separate conditions, not values[1].

Also, you forgot a space between echo and "wrong ntpserver"

Try this:
Code:
#! /bin/bash
a="1.2.3.4"
if [ "$a" = "1.2.3.4" ] || [ "$a" = "5.6.7.8" ] || [ "$a" = "8.9.1.4" ]; then
 echo " You have correct ntpserver"
else
  echo "wrong ntpserver"
fi
[1]Edited, see post #5.

Last edited by Didier Spaier; 02-27-2015 at 01:25 AM.
 
Old 02-26-2015, 07:46 PM   #3
Tadaen
Member
 
Registered: Sep 2005
Distribution: Arch
Posts: 210

Rep: Reputation: 39
Don't know where you get that idea. You must be running some crazy version of bash. I have several scripts that rely on "==" or "!=".

Most typically to run the script one way if root, another if regular user

Code:
if [[ "$USER" == root ]] ; then
  do root stuff here
else
  do user stuff here
fi
works great.

Last edited by Tadaen; 02-26-2015 at 07:48 PM.
 
Old 02-26-2015, 09:01 PM   #4
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 491Reputation: 491Reputation: 491Reputation: 491Reputation: 491
A slightly more compact option:
Code:
#!/bin/bash

a="1.2.3.4"

case "$a" in
	1.2.3.4|5.6.7.8|8.9.1.4)
		echo " You have correct ntpserver"
	;;
	*)
		echo "wrong ntpserver"
	;;
esac

exit 0
 
Old 02-27-2015, 12:38 AM   #5
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2 on Lenovo Thinkpad W520
Posts: 7,628

Rep: Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568Reputation: 2568
Quote:
Originally Posted by Tadaen View Post
Don't know where you get that idea. You must be running some crazy version of bash. I have several scripts that rely on "==" or "!=".
Yes, I stand corrected.

However, to keep the script portable I recommend avoiding bashisms, i.e. write the scripts using only the syntax defined in the POSIX specification of the Shell Command Language whenever possible and in this case it is easy.
 
  


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] Bash script time comparison with one being a text string NetEng1 Linux - Software 4 09-05-2013 10:26 PM
[SOLVED] bash script mysql output variable comparison dominic.rsa Programming 2 12-02-2011 12:59 AM
Need File Name comparison help on Korn Shell Script confusiac437 Programming 7 07-01-2011 02:14 PM
[SOLVED] Bash script: comparison with sequence of numbers? dbrazeau Programming 11 03-22-2011 12:22 PM
Image Comparison With Bash Script smcallis Programming 4 06-30-2004 12:53 PM

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

All times are GMT -5. The time now is 06:58 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration