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 |
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.
|
 |
12-18-2009, 06:35 AM
|
#1
|
Member
Registered: May 2006
Posts: 54
Rep:
|
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
[ ~]$
|
|
|
|
12-18-2009, 07:12 AM
|
#2
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
str1="string"
str2="string"
case "$str1" in
$str2 ) echo "same" ;;
*) echo "not same";;
esac
|
|
|
12-18-2009, 07:29 AM
|
#3
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
In the test statement ([...]), use "=" for string comparison----not "=="
You never change the loop counter---how does the loop exit?
|
|
|
12-18-2009, 11:25 AM
|
#4
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,813
|
Quote:
Originally Posted by pixellany
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" ]
|
|
|
12-18-2009, 11:57 AM
|
#5
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by pixellany
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".
|
|
|
12-18-2009, 01:03 PM
|
#6
|
Senior Member
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,813
|
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 ==.
|
|
|
12-18-2009, 10:56 PM
|
#7
|
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809
|
I had a reason for saying "=", but now I have forgotten.....  . The real distinction is between "-eq" and "="------"-eq" won't work with strings.
|
|
|
12-19-2009, 01:59 AM
|
#8
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
Quote:
Originally Posted by pixellany
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.
|
|
|
12-19-2009, 03:14 AM
|
#9
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by konsolebox
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
|
|
|
12-19-2009, 03:44 AM
|
#10
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
Quote:
Originally Posted by catkin
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.
|
|
|
12-19-2009, 04:15 AM
|
#11
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by konsolebox
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!
|
|
|
12-19-2009, 04:28 AM
|
#12
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
Quote:
Originally Posted by catkin
It sounds more dramatic as "the test version takes 66% longer than the [[ ]] version"  Lies, damn lies and statistics!
|
lies? ... err.. who lied?
|
|
|
12-19-2009, 08:12 AM
|
#13
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by konsolebox
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.
|
|
|
12-21-2009, 08:11 AM
|
#14
|
Member
Registered: May 2006
Posts: 54
Original Poster
Rep:
|
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
[]$
|
|
|
|
All times are GMT -5. The time now is 09:45 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
|
|