Operating System - HP-UX
1828214 Members
2314 Online
109975 Solutions
New Discussion

Re: Shell Script Quick Help

 
SOLVED
Go to solution
Steven Chen_1
Super Advisor

Shell Script Quick Help

Hi,

Would anyone help with this short script as why it is not getting through?

---------------------------------------
The script is to check Oracle listener whether it is up, if not, start it, if yes, exit.

It runs under Oracle, thus lsnrctl can start right there.

****************************
lsnrctl status > /disk3/test/t1
aaa=`grep 'The command completed successfully' /disk3/test/t1`
bbb='The command completed successfully'
ccc='TNS:no listener'

if [$aaa=$bbb]; then
exit
else
if [$aaa=$ccc]; then
lsnrctl start
fi
fi
exit

******************************

Error:

listen_test[6]: [=The: not found.
listen_test[9]: [=TNS:no: not found.
---------------------------------------

Very appreciated.

Steven
Steve
7 REPLIES 7
Patrick Wallek
Honored Contributor
Solution

Re: Shell Script Quick Help

Try this:

if [ "$aaa" = "$bbb" ] ; then
exit
else
if [ "$aaa" = "$ccc" ] ; then
lsnrctl start
fi
fi
exit

Note the extra spaces and quotes. What shell are you using for this? If POSIX, do a 'man sh_posix' for information on the syntax for the if statement. It is very syntax dependent.
Rodney Hills
Honored Contributor

Re: Shell Script Quick Help

Be sure their are spaces around the "[" and "]". Also it is best to enclose your variables in double quotes. Example-

if [ "$aaa" = "$bbb" ] ; then

HTH

-- Rod Hills
There be dragons...
Patrick Wallek
Honored Contributor

Re: Shell Script Quick Help

You probably also should enclose your values for bbb and ccc in double quotes ( " ) instead of single quotes ( ' ). For readability I would also modify aaa to be:

aaa=$(grep "The command completed successfully" /disk3/test/t1)

It will work exactly the same and is much more readable than using the back-ticks ( ` ).
A. Clay Stephenson
Acclaimed Contributor

Re: Shell Script Quick Help

You have several problems:

if [$aaa=$bbb]
should be:
if [ "${aaa}" = "${bbb}" ]
then
especialy note the white space after '[' and before ']'. Also, your variables should be protected bu quotes so that null strings don't clobber test.


You will also find this test to be much simpler:

lsnrctl status > tmpfile
grep -q "This command completed sucessfully" tmpfile
STAT=${?}
if [[ ${STAT} -eq 0 ]]
then
echo "OK"
else
echo "Bad"
fi

The -q option says only report the status of the grep; 0 = found.

The '[[ ]]' uses the POSIX and Korn shell's internal test whereas '[ ]' spawns the external test command. The internal is more efficient although you can continuey to use the external.
If it ain't broke, I can fix that.
Chris Watkins_1
Respected Contributor

Re: Shell Script Quick Help

Argh!
So hard to reply first here :-)
As mentioned... you have two troubles.

Double quote the variables, and space around the brackets.
ie...
[ "${aaa} = "${bbb}" ]

...will work just fine.
Not without 2 backups and an Ignite image!
Chris Watkins_1
Respected Contributor

Re: Shell Script Quick Help

NO points here please.
A typo in the previous post, in my haste to get a response in.

[ "${aaa}" = "${bbb}" ]

missed a doublequote on the previous response.
Not without 2 backups and an Ignite image!
Seth Parker
Trusted Contributor

Re: Shell Script Quick Help

Steven,

Clay's script should be the only one that actually works, even though everyone else has offered you good advice on syntax.

The reason I say this is because you're checking for "The command completed successfully" using grep on a file. Since you're just running a plain grep, you'll *only* get matches. So, $aaa can *never* match $ccc.

Back to Clay's script. All he's doing is checking to see if there was a match and then doing something with that information. And that's all you really need here. Just modify the "OK" and "Bad" results to do whatever you actually want and you should be fine.

Hehe, having said all this, I sure hope I'm not missing something obvious!!!

Regards,
Seth