Albin N
Albin N

Reputation: 1401

Conditionals not being tested properly

I've got this script that runs every time my Ubuntu machine wakes up and goes to sleep.

#!/bin/sh
if [ "${1}" == "pre" ]; then
  # Do the thing you want before suspend here, e.g.:
  echo "we are suspending at $(date)..." > /home/albin/stuff/suspend_test
elif [ "${1}" == "post" ]; then
  # Do the thing you want after resume here, e.g.:
  echo "...and we are back from $(date)" >> /home/albin/stuff/suspend_test
else
  echo ".2..neither pre or post . $1 .   $(date)" >> /home/albin/stuff/suspend_test
fi

This is found in the output file:

.2..neither pre or post . pre .   tis 11 sep 2018 20:44:42 CEST
.2..neither pre or post . post .   tis 11 sep 2018 20:45:06 CEST
.2..neither pre or post . pre .   tis 11 sep 2018 22:12:52 CEST
.2..neither pre or post . post .   ons 12 sep 2018 06:55:21 CEST
.2..neither pre or post . pre .   ons 12 sep 2018 06:55:22 CEST
.2..neither pre or post . post .   ons 12 sep 2018 06:55:43 CEST
.2..neither pre or post . pre .   ons 12 sep 2018 07:13:28 CEST
.2..neither pre or post . post .   ons 12 sep 2018 07:14:00 CEST

I've checked multiple guides on how to write bash conditionals without finding any issues. The $1 variable is available but ignored in the if statements

Upvotes: 1

Views: 54

Answers (3)

Gordon Davisson
Gordon Davisson

Reputation: 125758

You are using == instead of = inside [ ], but that's a bashism. Recent versions of Ubuntu use dash as /bin/sh, and it does not support ==; it gets an error, which is interpreted as the test failing:

$ if [ foo == foo ]; then echo "The strings match"; else echo "The strings do *not* match"; fi
dash: 1: [: foo: unexpected operator
The strings do *not* match

Solution: switch to =

$ if [ foo = foo ]; then echo "The strings match"; else echo "The strings do *not* match"; fi
The strings match

If you're going to use /bin/sh instead of /bin/bash, you really need to watch out for bashisms. There's a good page on this in the Ubuntu wiki. Either that, or switch to /bin/bash.

P.s. If you're going to use bash extensions, I'd recommend using [[ ]] instead of [ ] for conditionals -- it fixes most of the syntactic oddities of [ ], and adds some useful new capabilities like pattern and regular expression matching.

Upvotes: 6

webcitron
webcitron

Reputation: 142

Probably caller of this script passing you other arguments than post or pre. Change log writing to use >> instead of > (to prevent overwriting log all time) and try add echo “${1}” >> log at first line, then you can see what arguments you got in your script

Upvotes: 0

apatniv
apatniv

Reputation: 1856

Mostly likely, I think you got your redirection output wrong on line 4.

Must be :

echo "we are suspending at $(date)..." >> /home/albin/stuff/suspend_test

instead of

echo "we are suspending at $(date)..." > /home/albin/stuff/suspend_test

Also, I think $1 is neither equal to 'pre' or 'post'. From looking at the output you posted, I can't understand from why these strings "tis" and "ons" have come from.

Upvotes: 0

Related Questions