LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-18-2009, 06:35 AM   #1
jovie
Member
 
Registered: May 2006
Posts: 54

Rep: Reputation: 15
shell script; equate two strings


Hi

Can someone give the the correct syntax to check if two strings are the same in a shell script?

Below is the script I have so far and below that, what I get when I run it while turning the sreensaver on/off. At the moment the if statement evaluates as true whatever value is in $run.

Many thanks for any help

Code:
#!/bin/sh

count=0				# Initialise a counter
while [ $count = 0 ]		# Set up a loop control
do				# Begin the loop

sleep 1

#is the screensaver running
run=$(gnome-screensaver-command -q)

echo $run

if [ '$run'=="The screensaver is inactive The screensaver is not inhibited" ]
then
	echo "not running"
else
	echo "running"
fi

done				# End of loop

Quote:
[ ~]$ /tmp/test.sh
The screensaver is inactive The screensaver is not inhibited
not running
The screensaver is inactive The screensaver is not inhibited
not running
The screensaver is active The screensaver is not inhibited
not running
The screensaver is active The screensaver is not inhibited
not running
The screensaver is inactive The screensaver is not inhibited
not running
^C
[ ~]$
 
Old 12-18-2009, 07:12 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
str1="string"
str2="string"
case "$str1" in
  $str2 ) echo "same" ;;
  *) echo "not same";;
esac
 
Old 12-18-2009, 07:29 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
In the test statement ([...]), use "=" for string comparison----not "=="

You never change the loop counter---how does the loop exit?
 
Old 12-18-2009, 11:25 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by pixellany View Post
In the test statement ([...]), use "=" for string comparison----not "=="
Also you need to seperate the "=" with spaces, and use double quotes when substituting variable values:
Code:
[ "$run" = "The screensaver is inactive The screensaver is not inhibited" ]
 
Old 12-18-2009, 11:57 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by pixellany View Post
In the test statement ([...]), use "=" for string comparison----not "=="
It's very rare that you write anything I disagree with, pixellany, but in this case, AFAIK, = and == are equivalent with == being the bash operator for which bash allows = to be used as a synonym, in the interests of POSIX compliance. The GNU Bash Reference seems to support my view where it says in 6.4 Bash Conditional Expressions: "string1 == string2 True if the strings are equal. ‘=’ may be used in place of ‘==’ for strict posix compliance".
 
Old 12-18-2009, 01:03 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by catkin
AFAIK, = and == are equivalent with == being the bash operator for which bash allows = to be used as a synonym, in the interests of POSIX compliance.
Yeah, bash lets you get away with using either (even in POSIX mode), but a more strict shell (eg dash) might not accept ==.
 
Old 12-18-2009, 10:56 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
I had a reason for saying "=", but now I have forgotten...... The real distinction is between "-eq" and "="------"-eq" won't work with strings.
 
Old 12-19-2009, 01:59 AM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by pixellany View Post
I had a reason for saying "=", but now I have forgotten......
i hope you don't mind. that's probably because if you use '[[' in bash, the second string is parsed as a pattern. maybe if we are comparing 2 plain strings, '=' in common sense should act faster.

based on my tests, in bash, '[[' does things faster than '['. maybe it's because arguments in '[[' are immediately parsed, rather than being interpreted first just like a command then passed to the '[' command.

for example, try this code in bash:

Code:
a='string with spaces and $0me special chars'
[[ $a = 'any' ]]  # will this produce an error?
now let's compare the 2 types of test commands using 'time'

Code:
a='a long string with spaces . . . . . . . . . .'
time for (( i = 1; i <= 50000; i++ )); do [ "$a" = 'any' ]; done
time for (( i = 1; i <= 50000; i++ )); do test "$a" = 'any'; done  # just similar to '['
time for (( i = 1; i <= 50000; i++ )); do [[ $a = 'any' ]]; done
which of them do you think should be the fastest? it's probably '[['

Last edited by konsolebox; 12-19-2009 at 02:00 AM.
 
Old 12-19-2009, 03:14 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by konsolebox View Post
which of them do you think should be the fastest? it's probably '[['
It was, ~40% less time than the worst case "test" version.
Code:
c:~$ a='a long string with spaces . . . . . . . . . .'
c:~$ time for (( i = 1; i <= 50000; i++ )); do [ "$a" = 'any' ]; done

real    0m1.235s
user    0m1.190s
sys     0m0.035s
c:~$ time for (( i = 1; i <= 50000; i++ )); do test "$a" = 'any'; done  # just similar to '['

real    0m1.183s
user    0m1.139s
sys     0m0.042s
c:~$ time for (( i = 1; i <= 50000; i++ )); do [[ $a = 'any' ]]; done

real    0m0.743s
user    0m0.724s
sys     0m0.019s
 
Old 12-19-2009, 03:44 AM   #10
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by catkin View Post
It was, ~40% less time than the worst case "test" version.
i didn't compare the results by percentage before. that's pretty big .

i shouldn't have doubted what test command i should use before.

Last edited by konsolebox; 12-19-2009 at 03:46 AM.
 
Old 12-19-2009, 04:15 AM   #11
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by konsolebox View Post
i didn't compare the results by percentage before. that's pretty big .

i shouldn't have doubted what test command i should use before.
It sounds more dramatic as "the test version takes 66% longer than the [[ ]] version" Lies, damn lies and statistics!
 
Old 12-19-2009, 04:28 AM   #12
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by catkin View Post
It sounds more dramatic as "the test version takes 66% longer than the [[ ]] version" Lies, damn lies and statistics!
lies? ... err.. who lied?
 
Old 12-19-2009, 08:12 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by konsolebox View Post
lies? ... err.. who lied?
Nobody. I had used statistics to give two very different but valid results based on the same data -- 40% and 66% -- thus showing how statisticians can choose amongst valid results, the better to support one argument or another. This phenomenon is referenced in the old witticism, Lies, damned lies, and statistics. Sorry for not giving the link the first time.
 
Old 12-21-2009, 08:11 AM   #14
jovie
Member
 
Registered: May 2006
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks for all the help.

My main mistake was not putting $run in quotes when I echo'd it. There was a new line character in there which I couldn't see so even when the syntax was right the strings didn't match.

Quote:
[]$ run=$(gnome-screensaver-command -q)
[]$ echo $run
The screensaver is inactive The screensaver is not inhibited
[]$ echo "$run"
The screensaver is inactive
The screensaver is not inhibited
[]$
 
  


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
compare strings in shell scripting culin Linux - General 10 01-24-2013 01:33 AM
[SOLVED] How to concatenate strings in Shell kofucii Linux - Newbie 2 12-12-2009 05:07 AM
comparing 2 strings in shell script dhanabalanb Programming 3 08-01-2007 01:17 PM
Shell Script Strings - trim revof11 Programming 3 11-30-2006 09:46 AM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 AM.

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