1745866 Members
4233 Online
108723 Solutions
New Discussion юеВ

if ! not working in ksh

 
SOLVED
Go to solution
dictum9
Super Advisor

if ! not working in ksh

This is 11.0, N class machine.

I can get the following to work on the command line if I type it in, but not in a script. In a script, it complains about the "!".
The script starts with #!/bin/ksh.
Why is that, is there a bug?


if ! grep "Synchronizing" $STATE
then
echo not
else
echo yes
fi
11 REPLIES 11
Tim Nelson
Honored Contributor
Solution

Re: if ! not working in ksh

most likely need some quoting...

other option

if grep "Syncronizing" $STATE
then
echo yes
else
echo no
fi

Tim Nelson
Honored Contributor

Re: if ! not working in ksh

Now that I think more..

I do not think you can use "if !"

typically you would either "if true" or "if false". Not "if not true."

James R. Ferguson
Acclaimed Contributor

Re: if ! not working in ksh

Hi:

This should be:

if [ `grep -c "Synchronizing" $STATE` -eq 0 ]
...

That is, it appears that you want to test whether or not there are any occurances of "Synchronizing" in the (file) ${STATE}.

Another way is to examine the return value of grep (zero meaning that there *is* a match):

# grep "Synchronizing" ${STATE} && echo "yes" || echo "no"

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: if ! not working in ksh

Hi (again):

Ooops. The last suggestion is best done with the '-q'uiet switch (since you don't want the output; only the success or failure of the match:

# grep -q Synchronizing ${STATE} && echo yes || echo no

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: if ! not working in ksh

>Tim: I do not think you can use "if !"

Right, it is only available in [ ] or conditional expressions in [[ ]].
Or !(pattern-list).
And of course in arithmetic expressions in (( )) or let.
Peter Nikitka
Honored Contributor

Re: if ! not working in ksh

Hi,

this feature is not available in the standard ksh; in the ksh93 version, it is.
So if you want to try, use
/usr/dt/bin/dtksh
as the shell for your script.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
dictum9
Super Advisor

Re: if ! not working in ksh

I ended up not using "!" and reversing the if statement.

Still not real clear as to why it works in shell but not in the script.

OldSchool
Honored Contributor

Re: if ! not working in ksh

"Still not real clear as to why it works in shell but not in the script...."

well, it would appear that the shell you work in is not ksh.

easy test, type in /bin/ksh and then enter the command. does it work?
Daavid Turnbull
Frequent Advisor

Re: if ! not working in ksh

It is shell specific so it may depend on what you have as your #! line at the top which I suspect points at a different shell.

"man test" is your friend for this sort of stuff.

I tend to be a bit old school and tend to do something like:

grep -q "some string" "some file"
if [ $? -ne 0 ]
then
echo "some string not found"
fi


Because to me it makes it clear that it is the exit code that is being Analyzed in the conditional.
Behold the turtle for he makes not progress unless he pokes his head out.