If Condition

Hi,

I am trying to execute this command, but is it not working, says "`;' unexpected"

eval $lgrep $SAM_CMD ; if [$? -ne 0] ; then ; echo "No Error" ; fi

What i want is, return the command output, if it is non zero, say "No Error".

Thanks, John.

a couple of things. what are the values of the $lgrep and $SAM_CMD?
and logic for most UNIX commands is that a return of 0 is good so a -ne (not equal) compare to 0 should have a error message of "Error".
Now what you need to watch is that the value of $? is from the last command run before the check.so if you have a string of commands then it may not be what is expected.

Yes, you got it right. The values are just the command to look out for errors, and if there are any, i would get a couple of rows listing the error.

However, if i run those command in prompt, if there are no errors, i get the next prompt.

Space characters missing near the square brackets? And extra semi-colon. And shouldn't the compare be "-eq 0" if there is no error?

eval $lgrep $SAM_CMD ; if [ $? -eq 0 ] ; then echo "No Error" ; fi

Nope, i tried, still the same ";" error. It says "`;' unexpected"

Take the ";" out from between the then and the echo
if [$? -ne 0] ; then echo

This is the command i am running :

eval $lgrep $SAM_CMD ; if [ $? -eq 0 ] ; then echo "No Error" ; fi

but it gives me the following error:

LdapMonitor.ksh[78]: [0:  not found

[CODE]
eval $lgrep $SAM_CMD ; if [ $? -eq 0 ] ; then echo "No Error" ; fi
change to
eval $lgrep $SAM_CMD ; rslt=$? ; if [ "$rslt." = "0." ]; then echo "No Error"; fi

The original error message came from the extra semi-colon after "then".

Please post all relevant parts of the script, or the whole script.
Please post the command and the lines which set the variables.

It think I get your logic but I can't think of a simple grep which would return exit status zero when it doesn't find something. Obviously "grep -v" is irrelevant in this context.

eval $lgrep $SAM_CMD ; if [ $? -ne 0 ] ; then echo "No Error" ; fi

Btw. If this is commercial code it is rarely ever necessary or desirable to use "eval".
In portable Shell scripts I have seen commands like echo executed as ECHO where ECHO is a function which decides which syntax to use.

You can do the conditional test as follows

 [ "$?" -ne 0 ] && echo "No Error" 

. I'm not sure what you're doing with the first part but this code here reads "Test exit status to be not equal to 0 and if that evaluates to true, echo 'No Error'. I prefer the conditional test like above as opposed to if statements. Keeps code cleaner imho.

&& says do this code if the code to the left is true. || says do this if it fails or is not true. Good for one liners instead of worrying about if then syntax. Hope that helps a little for the future :slight_smile:

DC Slick -

What is the complete command?

eval $lgrep $SAM_CMD ; if [ "$?" -ne 0 ] && echo "No Error"; fi

---------- Post updated at 12:13 PM ---------- Previous update was at 12:12 PM ----------

Aix Guy -

I tried running it, but it is giving me error message, even when the command has an output.

---------- Post updated at 12:14 PM ---------- Previous update was at 12:13 PM ----------

Methyl -

The condition has changed, i want an message when output is null.

Also, eval is because i am using pipe within variables.

Please expand the variables so I can see what is in them. It could be something in them that the parse is not getting right.

If you could tell us what $lgrep and $SAM_CMD represent that would help so much. Give us an example of what those variable values are. That's what aix-guy means by expand the variables. With the test [ "$?" -ne 0 ] && echo "No Error" you don't need the IF. So

 eval "$lgrep" "$SAM_CMD"; [ "$?" -ne 0 ] && echo "No Error" 

is all you need.

Also, eval is because i am using pipe within variables.

Please post the whole script and sample command calls (if relevant). Perhaps there is a better way?

ok.

This is the complete Command, which i would like to run, am i am running it in shell script, and that is the reason, i have used variables.

/opt/ldap/lgrep "ADD | MOD | DEL" /var/app/dir/slapd-ED/logs/access | /opt/ldap/lgrep -v "err=0 | err=50 | err=20 | err=65"]

Because I have several command, i put this is variables :
# Variables:

ED_Logs_Dir=/var/app/dir/slapd-ED/logs/acces
lgrep=/opt/ldap/lgrep
Access_OPS="\"ADD | MOD | DEL\""
Access_Err="\"err=0 | err=550 | err=20 | err=65\""
ED_CMD="$Access_OPS $ED_Logs_Dir/access | $lgrep -v $Access_Err"

#Command :

eval $lgrep $ED_CMD ; rslt=$? ; if [ "$rslt." = "0." ]; then echo "No Error"; fi

Output:

[28/Jan/2011:14:39:30 -0500] conn=1568702 op=15 msgId=52 - MOD dn="uid=acp10,ou=internal,ou=People,dc=eis,dc=xyz,dc=com"
[28/Jan/2011:14:39:30 -0500] conn=1568702 op=15 msgId=52 - RESULT err=50 tag=103 nentries=0 etime=0
No Error

ED_Logs_Dir=/var/app/dir/slapd-ED/logs/acces #access has a typo
lgrep=/opt/ldap/lgrep
Access_OPS="\"ADD | MOD | DEL\""
Access_Err="\"err=0 | err=550 | err=20 | err=65\""
ED_CMD="$Access_OPS $ED_Logs_Dir/access | $lgrep -v $Access_Err" #the variable $ED_Logs_Dir already contains access so you don't need it here again

The reference to $ED_Logs_Dir in your last line expands to '/var/app/dir/slapd-ED/logs/acces/access' I'm sure that's unintended. So change your last line to

ED_CMD="$Access_OPS $ED_Logs_Dir | $lgrep -v $Access_Err"

There was a type, i have corrected it.

Also. there is separate variable : $Access_Err

ED_Logs_Dir=/var/app/dir/slapd-ED/logs
lgrep=/opt/ldap/lgrep
Access_OPS="\"ADD | MOD | DEL\""
Access_Err="\"err=0 | err=550 | err=20 | err=65\""
ED_CMD="$Access_OPS $ED_Logs_Dir/access | $lgrep -v $Access_Err"

If this command is complex, could you give me an example by illustrating simple command, and they i would try to emulate in this.

(Possibly controversial post, it's getting late here).

What is the problem with just putting the entire command into a shell script? To my mind all the variables and eval are not needed because there is no variation posted.

#!/usr/bin/sh
/opt/ldap/lgrep "ADD | MOD | DEL" /var/app/dir/slapd-ED/logs/access | /opt/ldap/lgrep -v "err=0 | err=50 | err=20 | err=65"
1 Like

I have something like 25 commands to put in the script, and and use variables in case it will be easier for others to update it.

Any i could use direct command.

Could you suggest on the error message?

Please show examples, highlighting the command parameters which can change.
I would be surprised if the command itself is a variable. (Still trying to get rid of "eval").

Btw. Obfuscating the code does not make it easy for the next person.