ProgrammingThis 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.
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.
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
[ ~]$
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".
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.
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
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.
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
[]$
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.