LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 10-28-2007, 01:13 PM   #1
christianunix
Member
 
Registered: Oct 2007
Posts: 40

Rep: Reputation: 15
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
 
Old 10-28-2007, 01:45 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 97
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
 
Old 10-28-2007, 04:20 PM   #3
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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
 
  


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
php: character \ in an input eantoranz Programming 3 09-27-2006 11:03 PM
input japanese character maurolinux Linux - Newbie 2 02-14-2006 07:14 AM
Japanese Character Input atheist Linux - Software 1 07-10-2004 11:52 PM
chinese character input and display -- need help carboncopy Slackware 5 07-06-2004 08:12 PM
winkey mapped to multiple character input g-off Linux - General 0 02-24-2004 02:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:05 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