Linux - NewbieThis 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
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.
Something's not right there.
If you're doing a comparison, use '==' instead of '='.
If you're doing an assignment, use 'operator' instead of '$operator'
Quoting will definitely help. Old-style "test" as "[" requires quoting and dereference symbol "$", whereas the new "[[" relaxes some of those restrictions. The first few parts of this show a clever scheme for doing the testing (found on this forum, but I regret that I do not recall the poster):
Code:
#!/bin/sh -
# @(#) s1 Demonstrate testing, quoting.
echo "Version used (local command version):"
version sh
# demonstrate the framework.
# "OK" means good; "KO" means bad, error, knockout.
# assignment is basically same as "read i", or "read operator".
echo
echo " i <- 1, expect OK, do not need quoting"
i=1
[ $i = 1 ] && echo OK || echo KO
echo
echo " i <- 2, expect KO, demonstrate testing framework"
i=2
[ $i = 1 ] && echo OK || echo KO
echo
echo " i <- *, special characters need quoting, expect error, KO"
i="*"
echo "i is $i"
[ $i = * ] && echo OK || echo KO
echo
echo " i <- *, quoted, if content unknown, best to quote, expect OK"
[ "$i" = "*" ] && echo OK || echo KO
echo
echo " i <- *, single quotes are more powerful, protects variable, expect KO"
[ '$i' = '*' ] && echo OK || echo KO
echo
echo " i <- *, double on variable, single on special, expect OK"
[ "$i" = '*' ] && echo OK || echo KO
echo
echo " i <- *, no dereferencing $, expect KO"
[ i = "*" ] && echo OK || echo KO
echo
echo " i <- *, double [[ allow omission of $, use of ==, expect OK"
[[ i == * ]] && echo OK || echo KO
echo
echo " simulate a read from keyboard (bash), expect OK"
read operator <<<*
echo " keyboard simulated input is \"$operator\""
[ "$operator" = "*" ] && echo OK || echo KO
exit 0
Producing:
Code:
% ./s1
Version used (local command version):
GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu)
i <- 1, expect OK, do not need quoting
OK
i <- 2, expect KO, demonstrate testing framework
KO
i <- *, special characters need quoting, expect error, KO
i is *
./s1: line 26: [: too many arguments
KO
i <- *, quoted, if content unknown, best to quote, expect OK
OK
i <- *, single quotes are more powerful, protects variable, expect KO
KO
i <- *, double on variable, single on special, expect OK
OK
i <- *, no dereferencing $, expect KO
KO
i <- *, double [[ allow omission of $, use of ==, expect OK
OK
simulate a read from keyboard (bash), expect OK
keyboard simulated input is "*"
OK
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.