LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 09-15-2016, 10:59 AM   #1
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

Rep: Reputation: Disabled
Help on string comparison in bash script...


Hello All

I'm a mediocre level bash scripter. Following code is making me crazy. I cant understand what I'm doing wrong. I just want to make sure the parameter passed to script is either '264' or '265'.

The problem is in first if statement. The second works fine. Whats wrong in first one?

Code:
#!/bin/bash

echo "Type 1:"
if [[ "$1" != "264" || "$1" != "265" ]] ; then
    echo "Parameter should be 264 or 265"
else
    echo "Correct"
fi

echo "Type 2:"
if [[ "$1" == "264" || "$1" == "265" ]] ; then
    echo "Correct"
else
    echo "Parameter should be 264 or 265"
fi
Result is like this:
Code:
$ ./strcomp.sh abc
Type 1:
Parameter should be 264 or 265
Type 2:
Parameter should be 264 or 265

$ ./strcomp.sh 264
Type 1:
Parameter should be 264 or 265
Type 2:
Correct

$ ./strcomp.sh 265
Type 1:
Parameter should be 264 or 265
Type 2:
Correct
Why != in first if statement comparsion is not working?

Thanks
 
Old 09-15-2016, 11:19 AM   #2
hazel
LQ Guru
 
Registered: Mar 2016
Location: Harrow, UK
Distribution: LFS, AntiX, Slackware
Posts: 7,614
Blog Entries: 19

Rep: Reputation: 4460Reputation: 4460Reputation: 4460Reputation: 4460Reputation: 4460Reputation: 4460Reputation: 4460Reputation: 4460Reputation: 4460Reputation: 4460Reputation: 4460
I think you need && in that first statement, not ||.

Suppose $1 is 254. Then the first half of the statement is false. But the second half is true because 254 != 255. Similarly if $1 is 255, the first half becomes true (255 != 254) but the second half becomes false.

Since you have used or logic, either half-statement being true will give an error message. With and logic, you will only get an error if both halves are true.
 
2 members found this post helpful.
Old 09-15-2016, 12:01 PM   #3
mangya
Member
 
Registered: Jul 2015
Distribution: CentOS
Posts: 89

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by hazel View Post
I think you need && in that first statement, not ||.

Suppose $1 is 254. Then the first half of the statement is false. But the second half is true because 254 != 255. Similarly if $1 is 255, the first half becomes true (255 != 254) but the second half becomes false.

Since you have used or logic, either half-statement being true will give an error message. With and logic, you will only get an error if both halves are true.
Hmmm... now I got it. Thanks for the explanation.
 
  


Reply



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
script for executing a program and doing string comparison b-neva Programming 9 05-07-2009 03:52 PM
ksh script - string comparison sharathkv25 Programming 3 09-19-2008 01:30 AM
string comparison in bash davimint Programming 4 01-23-2008 06:36 PM
bash string comparison noir911 Programming 1 01-25-2006 06:37 PM

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

All times are GMT -5. The time now is 11:54 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