0

I am writing a shell script for a file and gidme "file Syntax error: "fi" unexpected (expecting "then") ", Please can you help me? I think my syntax is correct, it gives me an error in the last if.

     #!/bin/sh
if [ ! "$1" = "" ];then
    CRONSCRIPT=$1
else
    CRONSCRIPT=cron.php
fi
MODE=""
if [ ! "$2" = "" ] ;then
    MODE=" $2"
fi
PHP_BIN=`which php`
INSTALLDIR=`echo $0 | sed 's/cron\.sh//g'`
if [ "$INSTALLDIR" != "" -a "`expr index $CRONSCRIPT /`" != "1" ];then
    if ! ps auxwww | grep "$INSTALLDIR$CRONSCRIPT$MODE" | grep -v grep 1>/dev/null 2>/dev/null;then
        $PHP_BIN $INSTALLDIR$CRONSCRIPT$MODE &
    fi
else
    if  ! ps auxwww | grep "$CRONSCRIPT$MODE" | grep -v grep | grep -v cron.sh 1>/dev/null 2>/dev/null;then
        $PHP_BIN $CRONSCRIPT$MODE &
    fi
fi
  • 2
    I can't reproduce this when I copy-paste your code into a new file. Can you please check your script for [carriage returns](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings)? – that other guy Oct 08 '21 at 19:07
  • Do you use `sh` or `bash`? Please fix your tag. – Cyrus Oct 08 '21 at 19:17
  • 3
    Side note: Have a look at [Correct Bash and shell script variable capitalization](https://stackoverflow.com/q/673055/6770384) and https://www.shellcheck.net/. – Socowi Oct 08 '21 at 19:18
  • `fi` is unexpected if your script has DOS line endings, because the parser sees a possible command name `then\r` rather than the keyword `then`. – chepner Oct 11 '21 at 14:04

0 Answers0