Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OR operator in tcsh 3

Status
Not open for further replies.

Ascalonian

Programmer
Jan 4, 2008
264
US
I am so lost trying to use the OR operator in my script. I have tried everything and can't seem to get it to work. I am using Cygwin on windows, and my default shell is tcsh (echo $0).

I have tried the following...

Code:
#!/bin/sh
echo "Enter: 1,2 or 3"
read choice

if [$choice -eq "1" -o $choice -eq "2"]; then
    echo "Both 1 and 2"
fi
This tells me: [: missing ']'

So then I tried this:
Code:
#!/bin/sh
echo "Enter: 1,2 or 3"
read choice

if [$choice -eq "1"] -o [$choice -eq "2"]; then
    echo "Both 1 and 2"
fi

Got the same response. So I tried something else

Code:
#!/bin/sh
echo "Enter: 1,2 or 3"
read choice

if [[$choice -eq "1" -o $choice -eq "2"]]; then
    echo "Both 1 and 2"
fi

I am getting syntax issues.

How do I write the syntax to use an or statement here?

As an FYI, this regular if statement works fine:

Code:
#!/bin/sh
echo "Enter: 1,2 or 3"
read choice

if [$choice -eq "1"]; then
    echo "Got 1"
fi
 
Use spaces:
if [ $choice -eq "1" ]; then

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That was exactly it... I didn't have a space after the [ and before the ]. Man. Thanks for the help!
 
In case you were wonder why there is such an insane space requirement...

Originally (in early Unix shells) condition tests were done using /usr/bin/test, so your code would have read:

Code:
if test $choice -eq "1"; then

However this didn't look as pretty as other languages with their if (condition) constructs, so someone decided to update test by symlinking it to /usr/bin/[ and making it expect a terminating ] parameter if it was invoked that way.

You'll most likely still find /usr/bin/test and /usr/bin/[ on your system, however these days, if you type whence -v test or whence -v [ it'll tell you that they are now implemented as shell builtins for efficiency reasons.

So that's why you need spaces around them, they are just commands with a list of space-separated parameters like any other command you would run in your shell.

As a corollary, the more modern shells also support [[ condition ]] builtins which have more features and use logical operators like || and &&. These have no /usr/bin/[[ equivalent.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Is there a way to put multiple OR statements on a line? For example, I have this line:

Code:
if [ $choice1 -eq "1" -o $choice2 -eq "1" -o $choice2 -eq "2" ]; then
...
fi

It tells me there are too many arguments in that statement. I greatly appreciate any help.
 
You could just be getting invalid syntax due to some variables not being defined; to be save you should surround them in double quotes like this:

Code:
if [ "$choice1" -eq "1" -o "$choice2" -eq "1" -o "$choice2" -eq "2" ]; then

Does that work? Personally I'd stick to "=" (i.e. string comparison) instead of "-eq" (numeric comparison) just in case the variables do not contain numbers, which "-eq" requires.

Annihilannic
[small]tgmlify - code syntax highlighting for your tek-tips posts[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top