Alain
Alain

Reputation: 722

Bash script giving me a different result on reboot?

I worked on a Bash script for the last day or so and running and debugging it directly on the shell.
The final script will be executed when the Ubuntu server gets rebooted.
I have started testing this, but my script gives me a different result then what I was expected. I have narrowed it down to an "or condition" and rewrote a more simpler script to test this anomaly:

A call to this script has been made in /etc/rc.local, with a redirection of the output to a log file (log/reboot.log).

I have this in my script (as a test):

 #!/bin/bash 
YESTERDAY=20131103
SYS_DATE=20131104
LAST_START=20131104

if [[ $LAST_START = $YESTERDAY || $LAST_START = $SYS_DATE ]];
then
    echo "is equal"
else
   echo "is not equal"
fi

Executing in the shell I get "is equal" (the right answer). After the reboot in the log I get "is not equal".

Could someone tell me why?

Upvotes: 1

Views: 134

Answers (1)

oz123
oz123

Reputation: 28888

I am guessing here,

But do you realize your /bin/sh is not your SHELL. In UBUNTU and Debian, /bin/sh is DASH, your login shell is BASH. So it might be related to your syntax of [[ ]] which is BASH.

Did you right in your top of the script:

#!/bin/sh

or

#!/bin/bash

[[

The [[ builtin is a bashism, and has somewhat better-defined semantics than [ (a.k.a. test). However, it is still quite reasonable to use [ instead, and portable scripts must do so. Note that argument handling is not quite the same; as above, use = rather than ==.

See here:

https://wiki.ubuntu.com/DashAsBinSh

the right way to do stuff here

The best solution would be actually to put your script in /etc/init.d and link it to run level 6. Which is the run level executed when rebooting. You should consider reading man 8 init when you have got some spare time. It will help you understand how your system is starting and shuting down.

Upvotes: 3

Related Questions