0
typeset -i STARTTIME=`expr $STARTHOUR \* 60 \* 60 + $STARTMINUTE \* 60 + $STARTSECOND`
echo $STARTTIME
typeset -i ENDTIME=`expr $ENDHOUR \* 60 \* 60 + $ENDMINUTE \* 60 + $ENDSECOND`
echo $ENDTIME

typeset -i ELAPSED=`expr $ENDTIME - $STARTTIME`
    typeset -i PREVIOUSTIME=`cat previouscompletion${wf}.txt`

    if [$ELAPSED -ne $PREVIOUSTIME]; then
    echo $ELAPSED > previouscompletion${wf}.txt
    fi

Above is an if statement I'm trying to get to run within a bash script on an AIX 6.1 server.

What it's giving me when I attempt to run it though, is the correct numbers in the echoes, but once it hits the if statement it croaks and gives me

./wf_zabbix_test.sh: line 46: [8: command not found

I've gotten case blocks to work on AIX, but only when comparing string variables, but I can't get any of these comparisons to work with integers. Sadly the AIX manuals are sorely lacking in information, and everything I've been able to Google seems directed more towards use in other Linux distributions.

Any help, even just a link to a page with how to do a darn if statement in AIX, would be greatly appreciated.

Kyle
  • 15
  • 3

1 Answers1

0

[ is not a syntax for the if but a command. ] is (and must be) its last parameter (and that is why a ; is necessary after it if you put then on the same line -- as you did. As any command, it returns a value and if just looks at this return value to do the then (with a 0 returned) or the else (otherwise).

Also, as any command, there must be at least one space between the command and the first parameter ($ELAPSED).

About $PREVIOUSTIME], you are concatenating a character ] after $PREVIOUSTIME, so the command [ sees only one parameter instead of 2 ($PREVIOUSTIME and ]).

Just add spaces.

if [ $ELAPSED -ne $PREVIOUSTIME ]
then
    echo $ELAPSED > previouscompletion$wf.txt
fi

Just to be clear and show that is a parameter: if [ $ELAPSED -ne $PREVIOUSTIME "]" or if [ $ELAPSED -ne $PREVIOUSTIME ']' would also be syntactically OK.

syme
  • 447
  • 1
  • 6
  • 12
  • Adding the spaces worked for me, and got the other if statements in the rest of the script flowing as well. Is there a good place to go to look further into the differences between syntax vs. commands? Seeing as I had absolutely no idea that the brackets were anything other than simply containing the logical test. Thanks! – Kyle Sep 19 '14 at 20:36
  • The only link I could give you is: http://www.tldp.org/LDP/abs/html/special-chars.html (its other pages are very interesting too, and I like the examples). I have learnt a lot from this myself. – syme Sep 19 '14 at 20:47
  • @Kyle: Try the [Bash Reference Manual](http://www.gnu.org/software/bash/manual/html_node/index.html); it should also be available by typing `info bash`. – Keith Thompson Sep 19 '14 at 20:49