1833877 Members
1548 Online
110063 Solutions
New Discussion

scripts error help

 
SOLVED
Go to solution
Kenn Chen
Advisor

scripts error help

Why i can't use if = command to compare the output ?? Check below scripts. Thanks.
============================================
if $server eq "Started" then
echo "OK"
fi
Cyber Zen
6 REPLIES 6
Stefan Farrelly
Honored Contributor
Solution

Re: scripts error help


To do a test is must be inside square brackets;

if [ "$server" = "Started" ]
then
echo OK
else
echo NO
fi
Im from Palmerston North, New Zealand, but somehow ended up in London...
Praveen Bezawada
Respected Contributor

Re: scripts error help

Hi
For string comparision you have to use quotes

if [ "$A" = "String" ];then
.
.
.
fi

...BPK...

Re: scripts error help

Hi Idris,
this is depending in which case you wan?t to use this script. for your simple case
if ["$server" = "started"]
then
echo ok
else
if ["$server" = ""]
echo nostatus
else
NO
fi
fi
the world of unix is beautifull
Jdamian
Respected Contributor

Re: scripts error help

The objet after the "if" must return a value:
=0 --> OK (true)
<>0 --> FAIL (false)

Long time ago, the "test" command was used (see manual pages of test command).

if test $X -eq $Y ???

Now shells have got buit-in this command and uses alternate notation (the braket).

if [ $X = $Y ] ???

If you use double brackets the notation for some operators as AND, OR, ... has changed. See the following example:

if [ $X = 3 -a $Y = 2 ] ???

if [[ $X = 3 && $Y = 2 ]] ???
Tim D Fulford
Honored Contributor

Re: scripts error help

Further to everything above, I think it is worth explaining the difference between = & -eq;

-eq is Numerical equals e.g
if [ $var -eq 0 ]
then
.....

= is equivalent ASCII e.g.
if [ $var = hello ]
then

there is a similar distinction between
-lt & <
-gt & >
-le & <=
-ge & >=
but obviously seeing if a character is greater than an ASCII character is less intuative

You can get all the above from man ksh...

Tim
-
Fred Martin_1
Valued Contributor

Re: scripts error help

There are two correct methods, see man test
fmartin@applicatorssales.com