read value from file and compare, why syntax error?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

read value from file and compare, why syntax error?

Bo Berglund
I am baffled by this error being displayed when I run this script
command which is meant to allow only one execution per day...

I have this on top of a larger script that works flawlessly without my
extra lines to stop dual execution for a day.
The section from LOGFILE= to fi are added into the old script in order
to make it exit with an error message if it already ran that day:

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

I get the following error on the else line:

./makeatjobs: line 8: syntax error near unexpected token `else'
./makeatjobs: line 8: `else'

Why is there a syntax error on the keyword else?????
Are if-then-else constrructs not allowed in bash? I'm sure they are...


--
Bo Berglund
Developer in Sweden


--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: read value from file and compare, why syntax error?

Paul Smith-2
On Mon, 2020-12-14 at 00:00 +0100, Bo Berglund wrote:
> if [ "$NEXTDAY" == "$LASTDAY" ] then;

this is wrong: the semicolon should be before the "then" not after it:

  if [ "$NEXTDAY" = "$LASTDAY" ]; then

(I used = not == because the former is POSIX conforming).

The "condition" of an if-statement can be an arbitrarily complex
command to run, so it needs a semicolon to end it.

I'm not sure why you got the error you did; it seems like bash should
have been able to provide a better one.


--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: read value from file and compare, why syntax error?

ubuntu-users mailing list
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.

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: read value from file and compare, why syntax error?

Paul Smith-2
On Mon, 2020-12-14 at 01:15 +0100, Ralf Mardorf via ubuntu-users wrote:
> By the way "case in" is way faster, so when "if else" makes sense, it
> could be better to use "case in" instead.

case is often not faster in reality, since most shells have "[" (and
"test") as builtins.  However there are certainly great reasons to use
case: it's much more flexible for one thing.  Definitely case is
something every shell programmer should have in their toolbox!

However, just to be clear for the OP, if and case are not really
interchangeable.

The if condition is a _command_ that the shell runs, and the condition
is true if the command succeeds (exits with a 0 exit code) and the
condition is false if the command fails (exits with a non-0 exit code).

case does string matching.


So, if statements have the syntax:

  if <command>; then
    ...

(same with while <command>; do) and case statements have the syntax:

  case <string> in
    ...

If your if-condition is a string comparison then case can be
substituted, but if your if-condition is anything else then case won't
help.


--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: read value from file and compare, why syntax error?

ubuntu-users mailing list
On Sun, 13 Dec 2020 19:36:01 -0500, Paul Smith wrote:
>If your if-condition is a string comparison then case can be
>substituted, but if your if-condition is anything else then case won't
>help.

My apologies, I should have mentioned this.

On Sun, 13 Dec 2020 18:08:20 -0500, Paul Smith wrote:
>I'm not sure why you got the error you did; it seems like bash should
>have been able to provide a better one.

Maybe because "else" makes no sense at all, even with the semicolon at
the correct position.

There's absolutely no need to use "else", if the if-condition is
false.

I suspect that you and I agree that

if [ "$NEXTDAY" = "$LASTDAY" ]; then
  echo "Already created jobs for this day ( $NEXTDAY )!"
  exit
else
  echo "$NEXTDAY" > "$LOGFILE"
fi

results in the same as

if [ "$NEXTDAY" = "$LASTDAY" ]; then
  echo "Already created jobs for this day ( $NEXTDAY )!"
  exit
fi
echo "$NEXTDAY" > "$LOGFILE"

does. There are tools available to check scripts against errors and to
provide alternatives, maybe one or the other tool is able to detect the
OP's chain of thought. I'm not surprised that bash doesn't provide a
good pointer, since "else" with or without the semicolon in the right
position, is absurd.

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: read value from file and compare, why syntax error?

Little Girl
In reply to this post by Bo Berglund
Hey there,

Bo Berglund wrote:

>I am baffled by this error being displayed when I run this script
>command which is meant to allow only one execution per day...

>I get the following error on the else line:
>
>./makeatjobs: line 8: syntax error near unexpected token `else'
>./makeatjobs: line 8: `else'
>
>Why is there a syntax error on the keyword else?????

The syntax error is actually on this line:

        if [ "$NEXTDAY" == "$LASTDAY" ] then;

All you need to do is move the semicolon into the correct position,
like this, so that Bash can find the "then" token that it needs:

        if [ "$NEXTDAY" == "$LASTDAY" ]; then

Your script should work smoothly once you do that.

--
Little Girl

There is no spoon.

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: read value from file and compare, why syntax error?

Bo Berglund
In reply to this post by ubuntu-users mailing list
On Mon, 14 Dec 2020 01:15:23 +0100, Ralf Mardorf via ubuntu-users
<[hidden email]> 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


--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users