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 |
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.
|
 |
10-28-2007, 01:13 PM
|
#1
|
Member
Registered: Oct 2007
Posts: 40
Rep:
|
the character * as the input in the unix script?
the name of the variable that takes the user input is called "operator". What can I do to enable it to take * as the input?
Code:
read operator
if [ $operator = \* ]; then
read operator
if [ $operator = "\*" ]; then
read operator
if [ $operator = '\*' ]; then
none of the the above works!! please help me
|
|
|
10-28-2007, 01:45 PM
|
#2
|
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109
Rep:
|
Hi.
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'
Dave
|
|
|
10-28-2007, 04:20 PM
|
#3
|
Member
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735
Rep:
|
Hi.
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
Best wishes ... cheers, makyo
|
|
|
All times are GMT -5. The time now is 05:55 PM.
|
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
|
|