read value from file and compare, why syntax error?

Ralf Mardorf kde.lists at yahoo.com
Mon Dec 14 00:15:23 UTC 2020


On Mon, 14 Dec 2020 00:00:39 +0100, Bo Berglund 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.

Don't get me wrong, I'm writing "dirty" scripts myself all the times,
but since the OP mentioned to run "this on top of a larger script", it
might be really odd to "goto" exit or else to continue. Writing "dirty"
scripts is ok, if they aren't large, but large scripts should be
written thought-out, to avoid issues and to keep them readable.




More information about the ubuntu-users mailing list