[comp.unix.programmer] IF syntax

jon@brahms.udel.edu (Jon Deutsch) (11/15/90)

OK, second try at the question:

I'm using sh to impliment an IF statement.  Can't get it to work.
Here's the code:


#!/bin/sh
if [$temp != '']  then
        echo 'remote-caller' >> .people;
else
	echo 'inside-caller' >> .people;
fi


I keep getting an ELSE UNEXPECTED.  
So, I tried an ELIF, but no go.
Any answers?
(as you can see, I'm new to sh!)
thanx


       X-------------------+--------------+-----------------------X
       |  |   |\       |>jon@brahms.udel.edu<|  "For my 2 cents,  |
       | \|on |/eutsch |>>-----------------<<|  I'd pay a dollar" |
       X------+--------------------+--------------------+---------X

harsha@ksr.com (Paul Harsha) (11/15/90)

jon@brahms.udel.edu (Jon Deutsch) writes:



>#!/bin/sh
>if [$temp != '']  then
>        echo 'remote-caller' >> .people;
>else
>	echo 'inside-caller' >> .people;
>fi

>I keep getting an ELSE UNEXPECTED.  

You need to put the "then" on a seperate line

#!/bin/sh
if [$temp != '']  
then
        echo 'remote-caller' >> .people;
else
        echo 'inside-caller' >> .people;
fi

works just fine.

-Paul

stetner@.bnr.ca (Douglas Stetner) (11/15/90)

In article <15796@brahms.udel.edu> jon@brahms.udel.edu (Jon Deutsch) writes:
>
>OK, second try at the question:
>
>I'm using sh to impliment an IF statement.  Can't get it to work.
>Here's the code:
>
>
>#!/bin/sh
>if [$temp != '']  then
>        echo 'remote-caller' >> .people;
>else
>	echo 'inside-caller' >> .people;
>fi
>
Try (note quotes and spaces):

#!/bin/sh
if [ "$temp" != "" ]
then
	echo 'remote-caller' >> .people;
else
	echo 'inside-caller' >> .people;
fi

--
--------------------------------------------------------------------------------
Douglas G. Stetner, 4S75  |  Bell-Northern Research  | My opinions are my own.
      stetner@bnr.ca      |  P.O. Box 3511, Stn. C   | (613) 828-6321  (home)
 ..!uunet!bnrgate!stetner |  Ottawa, ON, CANADA      | (613) 763-8396  (work)

cechew@bruce.cs.monash.OZ.AU (Earl Chew) (11/15/90)

In <965@ksr.com> harsha@ksr.com (Paul Harsha) writes:

>#!/bin/sh
>if [$temp != '']  
>then
>        echo 'remote-caller' >> .people;
>else
>        echo 'inside-caller' >> .people;
>fi

>works just fine.

#!/bin/sh
if [ $temp != '' ]
then
        echo 'remote-caller' >> .people;
else
        echo 'inside-caller' >> .people;
fi

works better when temp != '' :-)

Earl
-- 
Earl Chew, Dept of Computer Science, Monash University, Australia 3168
EMAIL: cechew@bruce.cs.monash.edu.au PHONE: 03 5655447 FAX: 03 5655146
----------------------------------------------------------------------

himacdon@maytag.uwaterloo.ca (Hamish Macdonald) (11/15/90)

>>>>> In article <965@ksr.com>, harsha@ksr.com (Paul Harsha) writes:

Paul> You need to put the "then" on a seperate line

Paul> #!/bin/sh
Paul> if [$temp != '']  
Paul> then
Paul>         echo 'remote-caller' >> .people;
Paul> else
Paul>         echo 'inside-caller' >> .people;
Paul> fi

How about 'can put the "then" on a separate line':

if [ "$temp" != "" ]; then

works just fine.  The semicolon terminates the list of statements
forming the condition of the if.

I think there is a problem if you don't double-quote your variable or
leave white space around the "[" and "]" too:

1>@maytag[3]% echo a${temp}a
aa
1>@maytag[4]% if [$temp != '']; then echo blug; else echo bleah; fi
blug

It should echo "bleah"

1>@maytag[12]% if [ "$temp" != "" ]; then echo blug; else echo bleah; fi
bleah

Hamish.
--
--------------------------------------------------------------------
himacdon@maytag.uwaterloo.ca                 watmath!maytag!himacdon

cheewai@spinifex.eecs.unsw.oz (Wai Yeung) (12/02/90)

In article <15796@brahms.udel.edu>, jon@brahms.udel.edu (Jon Deutsch) writes:
> 
> #!/bin/sh
> if [$temp != '']  then
>         echo 'remote-caller' >> .people;
> else
> 	echo 'inside-caller' >> .people;
> fi
> 
The "then" has to be on the next line. Also, for [ ... ] to check for an
empty string, it isn't necessary to compare with "" (or '').

The following should work.
#
#!/bin/sh
if [ "$temp" ] # Note the quotes
then
    echo 'remote-caller' >> .people
else
    echo 'inside-caller' >> .people
fi
#

Chee Wai