0

I'm trying to check if a file size is less than 9999bytes, but I have no idea why it breaks right here..

if compgen -G "$jdir0/"*.srt 
then

for f in "$jdir0"/*.srt
do
    jsubtitle="$(basename $f)"
    filesize=$(du -b "$jdir0/$jsubtitle" | cut -f1)
        if [ "$filesize" -lt 9999 ] then
           echo "less"
        fi   #line 443
        if [ $filesize -gt 10000 ] then......etc,etc...



    **line 443: syntax error near unexpected token `fi'**

I've checked The variable $filesize, and it contains only numbers, no spaces, I've also tried without " " around the variable $filesize (and 9999) in the if statement. I've also tested double [[, but no..

This is statement is inside another if statement inside a "for f in.." loop, but that shouldn't affect this?

2
  • 1
    Missing semicolon before then. shellcheck.net is good at spotting common mistakes like this. – Gordon Davisson Aug 25 '20 at 20:12
  • 1
    geee.. I literally have 100 if statement in the code, and never thought about it, and still I'm using both ; as a delimiter and have some places with then on the next line, and here I missed it for 5 hours.. sometimes you really can... ahhhhh.. thx for the help everybody – JoBe Aug 25 '20 at 20:45
5

I think your problem is that you have then on the same line as the conditional. It either needs to be on the next line, or have a semicolon before it.

Solution: if [ "$filesize" -lt 9999 ]; then

2

The then in your if needs to either go onto its own line or be preceeded by a ;:

    if [ "$filesize" -lt 9999 ]; then
       echo "less"
    fi
1

In:

if [ "$filesize" -lt 9999 ] then
           echo "less"
fi

That then is passed as an extra argument to the [ command, not as the then keyword that would end the list of condition commands in the:

if
   condition-commands
then
   then-commands
〚elif
   more-condition-commands
then
   more-then-commands〛
〚else
   else-commands〛
fi

statement.

It becomes more obvious if you replace the [ command with another simple command with a less unusual name such as echo:

if echo whatever then
  echo "less"
fi

Don't be fooled by stackexchange's syntax highlighting that identifies then as a keyword there even if the shell doesn't consider it as such here in an argument to a simple command like echo or [. You'll notice it does the same in:

echo then
[ then = alors ]

The fi is unexpected because we need a then first. That then needs to be delimited somehow. So:

if [ "$filesize" -lt 9999 ]; then
   echo "less"
fi

Or:

if [ "$filesize" -lt 9999 ]
then echo "less"
fi

Technically,

if [ "$filesize" -lt 9999 ] & then
   echo "less"
fi
if ([ "$filesize" -lt 9999 ]) then
   echo "less"
fi

would also work in bash from a syntax point of view but that would not be equivalent. The first one would run the [ command in background (and we would just test that it has been successfully started in background), while the second would run it in a subshell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.