read value from file and compare, why syntax error?

Bo Berglund bo.berglund at gmail.com
Tue Dec 15 14:55:11 UTC 2020


On Mon, 14 Dec 2020 01:15:23 +0100, Ralf Mardorf via ubuntu-users
<ubuntu-users at lists.ubuntu.com> wrote:

>>Are if-then-else constrructs not allowed in bash? I'm sure they are...
>
>They are, but yours usage of "else" makes no sense at all.
>
>By the way "case in" is way faster, so when "if else" makes sense, it
>could be better to use "case in" instead.
>
>On Sun, 13 Dec 2020 18:08:20 -0500, Paul Smith wrote:
>>I used = not == because the former is POSIX conforming
>
>!= probably would be a better choice, since
>if-then-(goto)exit-else-continue very seldom is a reasonable control
>flow.
>
>Actually
>
>if [ "$NEXTDAY" = "$LASTDAY" ]; then
>  echo "Already created jobs for this day ( $NEXTDAY )!"
>  exit
>else
>  echo "$NEXTDAY" > "$LOGFILE"
>fi
>
>doesn't require "else" at all, since
>
>if [ "$NEXTDAY" = "$LASTDAY" ]; then
>  echo "Already created jobs for this day ( $NEXTDAY )!"
>  exit
>fi
>echo "$NEXTDAY" > "$LOGFILE"
>
>does the same. You might say that the "else" makes it easier to read,
>but this is only true, if the control flow isn't thought-out in the
>first place.

You are right! The construct I had there was because I added the extra
saving of the date in order to block dual executions by mistake and
while testing I used the original script so I was weary to exit as
soon as I passed through the new commands every time...

Now the thing looks like this:

LOGFILE="makeatjobs.log"
NEXTDAY=`date --date="tomorrow" +%Y-%m-%d`
NEXTDAYSHRT=`date --date="tomorrow" +%Y%m%d`
read LASTDAY < "$LOGFILE"
if [ "$NEXTDAY" = "$LASTDAY" ]
then
  echo "Already created jobs for this day ( $NEXTDAY )!"
  exit
fi
echo "$NEXTDAY" > "$LOGFILE"

# main part of script follows like this:
if [[ "$DAYNR" == @(2|3|4|5|6) ]]; then
  #Previous day is a weekday so do:
--- do stuff
fi
etc...

The semicolon on the wrong side of then was caused by an example on
stackoverflow I read...
But now moved then down one line to ease readability.

Thanks.


-- 
Bo Berglund
Developer in Sweden





More information about the ubuntu-users mailing list