2

I made an upstart script which breaks my expectations entirely.

stupid-test.conf

script
  VALUE="PONG"
  echo "START" >> /var/log/stupid-test.log
  if [ "$VALUE" == "PONG" ]; then
    echo "GOOD PONG" >> /var/log/stupid-test.log
  fi
  if [ "$VALUE" != "PONG" ]; then
    echo "BAD PONG" >> /var/log/stupid-test.log
  fi
  if [ "$VALUE" == "PING" ]; then
    echo "BAD PING" >> /var/log/stupid-test.log
  fi
  if [ "$VALUE" != "PING" ]; then
    echo "GOOD PING" >> /var/log/stupid-test.log
  fi
  echo >> /var/log/stupid-test.log
end script

stupid-test.log

START
GOOD PING

Naturally, if you execute the exact same commands in bash, it works perfectly:

START
GOOD PONG
GOOD PING

This appears to work for anything you can put in VALUE--I've tried integers and strings.

skeggse
  • 6,103
  • 11
  • 57
  • 81
  • 1
    The equality operator is `=`, not `==`, but some shells allow the latter as an extension. – Barmar Dec 13 '13 at 04:01
  • According to http://www.gnu.org/software/bash/manual/bashref.html, the equality operators are `==` and `!=`. – skeggse Dec 13 '13 at 04:02
  • That said, that does fix the problem...thanks! – skeggse Dec 13 '13 at 04:03
  • Also, why does that differ from when I run it as root in a tty? – skeggse Dec 13 '13 at 04:03
  • 1
    My guess is that `upstart` is using `sh` rather than `bash`. When bash is invoked with that name, it disables most of its extensions. – Barmar Dec 13 '13 at 04:04
  • From the bash reference you linked to: _‘=’ should be used with the **test** command for POSIX conformance._ – Barmar Dec 13 '13 at 04:07
  • Actually, my comment seems to be wrong, as this is not one of the extensions that's disabled in bash POSIX mode. So I guess upstart isn't using bash in the first place. – Barmar Dec 13 '13 at 04:14

1 Answers1

5

Use = rather than == when performing equality tests. == is a bash extension, and it's not available in the shell that upstart runs. Upstart runs all scripts using /bin/sh -e. On Ubuntu systems, /bin/sh is dash, not bash.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Wow. Is this documented anywhere? Did I just miss this very crucial piece of information? Thanks! – mooreds Jan 06 '15 at 21:04
  • It's in the `bash` man page: _= may be used in place of == for strict POSIX compliance._ – Barmar Jan 06 '15 at 21:11
  • If your question is where it's documented that `upstart` doesn't use `bash`, I don't know. It's just clear from the symptoms. – Barmar Jan 06 '15 at 21:16
  • Thanks to your hints, I tracked down where the upstart manual specifies its shell: http://upstart.ubuntu.com/cookbook/#develop-scripts-using-bin-sh and updated the answer. Thanks! – mooreds Jan 07 '15 at 23:07