Search code examples
bashcommand-linescriptingsh

Syntax error: "fi" unexpected (expecting "then") in bash script


I try to do the script:

#!/bin/bash
IP='192.168.1.1'
fping -c1 -t300 $IP 2>/dev/null 1>/dev/null
if [ "$?" = 0 ]
then
    echo "Host found"
else
    echo "Host not found"
fi

and i turn it:

pi@raspberrypi ~ $ sh /home/pi/sh/test.sh

/home/pi/sh/test.sh: 9: /home/pi/sh/test.sh: Syntax error: "fi" unexpected (expecting "then")

where is the problem?


Solution

  • You can try

    $ dos2unix /home/pi/sh/test.sh
    

    and run it again.