cancel
Showing results for 
Search instead for 
Did you mean: 

Re: If Then Else Help

 
SOLVED
Go to solution
Advisor

If Then Else Help

Hello All,
I have a script that isn't working and being a noob at this, I need the help of experts.
Below is the script;

#!/bin/sh
MSGTXT="`omnistat |awk '{print $1,$2,$3}'"
echo $MSGTXT
if [$MSGTXT="No currently running"]
then
omnisv stop
sleep 30
cp -p /opt/omni/* /destination
cp -p /var/opt/omni/* /destination
cp -p /etc/opt/omni/* /destination
omnisv start
echo IT WORKS
else
echo SESSION OPEN
fi

Thanks.
J.
6 REPLIES 6
Acclaimed Contributor
Solution

Re: If Then Else Help

Hi:

The second line is mangled. You need matching back-ticks or better yet:

MSGTXT=$(omnistat|awk '{print $1,$2,$3}')

The notation '$( ... )' is modern POSIX syntax or the archaic back-ticks. Both forms execute the enclosed command(s) and collect the output.

Regards!

...JRF...
Advisor

Re: If Then Else Help

Thank You Sir, So it should look like;

#!/bin/sh
MSGTXT=$(omnistat|awk '{print $1,$2,$3}')
:echo $MSGTXT
if [$MSGTXT="No currently running"]
then
omnisv stop
sleep 30
cp -p /opt/omni/* /destination
cp -p /var/opt/omni/* /destination
cp -p /etc/opt/omni/* /destination
omnisv start
echo IT WORKS
else
echo SESSION OPEN
fi
Acclaimed Contributor

Re: If Then Else Help

Hi (again):

> So it should look like;

Well, drop the colon before the word 'echo'. You can do a half-decent syntax check with:

# sh -n filename

...where 'filename' is whatever you call your script. If no output appears, then the script is syntactically correct as far as the shell can determine.

Regards!

...JRF..
Valued Contributor

Re: If Then Else Help

One last thing as a Maybe-Just-In-Case. Change the line

if [$MSGTXT="No currently running"]

to be

if [ "$MSGTXT" = "No currently running" ]

Close to a quarter-century of experience with HP's shells have taught me to be Very Very Careful when doing string comparisons, so I'm quite liberal with the use of double-quotes to delimit all character strings inside my scripts.
There's no place like 127.0.0.1

HP-Server-Literate since 1979
Advisor

Re: If Then Else Help

Nice thanks, Don't know where the colon came from, but nontheless, it's not there.
I did the sh -n with no errors, however, I keep getting the "SESSION OPEN" even though there isn't any open.

Any ideas?
J.
Advisor

Re: If Then Else Help

# ./BkupScript
IT WORKS


Thanks all!