I am trying to create a event handler for icing. My Bash script looks like this right now:

# Event Handler for loggin out inactive RDP users

# $3 is $SERVICEATTEMPT$ (1 through 4)
# $4 is $SERVICDOWNTIME$ (0 no Downtime, >0 Downtime active)
# $5 is SRV29 
# $6 is $host.name$ (1 through 4)

if  [ "$4" > 0 ];
 echo "in downtime, exit";
 then exit

case "$1" in
echo "ok!"
echo "warning!"
echo "unknown!"
echo "critical!"

When I execute this script without my if statement at the top, everything works fine. But I want to check if $4 is greater than 0. This test condition always returns true and no matter what I enter inside this condition it always results in "in downtime, exit".

So even if [ "hello" = "hallo" ] it will go inside and exit right away. I also tried pretty much every variation with quotes, without, double brackets... and so on. I am obviously doing something wrong. Can anyone spot it?

Thanks in advance!

  • 1
    Don't use > inside [...], it means redirection there. In some shells, you can use \>, or better to use standard one -gt. – cuonglm Mar 24 '17 at 17:04
  • @cuonglm thanks, didn't actually know! But I also tried -gt which still results in the same beaviour – mike.b93 Mar 24 '17 at 17:08
  • You stated that # $4 is $SERVICDOWNTIME$ (1 through 4), so it's always true, isn't it? – cuonglm Mar 24 '17 at 17:11
  • @cuonglm this is a copy & paste error I made. it actually is 0 when no downtime is active. Also, right now I still execute the script by hand so the var does not get polled for this test – mike.b93 Mar 24 '17 at 17:13
  • Please fix the if statement's syntax first. the then must be attached like as: if [ "$4" -gt 4 ]; then – user218374 Mar 24 '17 at 17:16

For testing integers you will want to use:

-eq  #Is equal
-ne  #Is not equal
-lt  #Less than
-le  #Less than or equal
-gt  #Greater than
-ge  #Greater than or equal

So your test statement should read:

if  [ "$4" -gt 0 ];

Additionally, your if statement is missing the then so it should be corrected to:

if  [ "$4" -gt 0 ]; then

See man test for more test options.

  • I'd add that the > is probably redirecting output and creating a file called 0 in the current directory. – Wolf480pl Mar 24 '17 at 17:04
  • @Wolf480pl yes you are right. @Timothy: I still get the same behavior with -gt.. my echo gets displayed and the script exits – mike.b93 Mar 24 '17 at 17:10
  • For debugging you can temporarily add a flag to the first line of your script. #!/bin/bash -x may help you to see what is actually happening and where the failure is. – Timothy Martin Mar 24 '17 at 17:15
  • i removed my echo inside the if statement. now it works. Seems that the syntax has to be if [ "$4" -gt 0 ]; then which would mean that I can not put an echo inside.. (right?) – mike.b93 Mar 24 '17 at 17:19
  • 1
    @TimothyMartin ah man that was a uber Friday brain fart haha. of course that works like that. thanks for your help :) – mike.b93 Mar 24 '17 at 17:35

Your if statement is all whacked off.

if [ "$4" -gt 0 ]; then
   # stuff to do when $4 > 0
   # stuff to do when $4 <= 0
  • @jlliagre Yes you are right on both counts. Regarding quoting the 0, they are a result of involuntary quoting of shell commands!. – user218374 Mar 24 '17 at 17:45
  • FWIW, in American English "whacked off" means something other than "malformed". :) – Tony Mar 24 '17 at 23:17
if  [ ... ];
    echo "in downtime, exit";
    then exit

As the other answers mention, the then keyword is in the wrong place in this if condition. (It's not missing, from the point of view of the shell language. If it were, that would be a syntax error.)

What happens here, is that both commands before the then are taken as part of the condition, and the return value of the whole lot is used to choose if the part after then runs. With commands separated by semicolons, only the exit status of the last command matters.

Since echo will probably always succeed, the conditional branch always runs. Something like if true; false; then echo something; fi would never run the echo though. I would suggest putting the then keyword on the same line as the condition, to avoid accidentally adding something between them.

With if, using multiple commands in the condition is likely to be just confusing, but with while it might have it's uses: we could have something like this:

while value=$(somecommand) ; [ "$value" != 0 ] ; do
    echo "do some work with $value"

Both the assignment and test run on each iteration of the loop.

  • Thank you for the explanation. I think now I understand what went wrong here. – mike.b93 Mar 25 '17 at 10:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.