语法错误:bash 脚本中的“fi”意外(期望“then”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30037627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Syntax error: "fi" unexpected (expecting "then") in bash script
提问by Kwiatkowski
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?
问题出在哪儿?
回答by Diego Torres Milano
You can try
你可以试试
$ dos2unix /home/pi/sh/test.sh
and run it again.
并再次运行它。
回答by tivn
Most probably this is because carriage-return \r
in your script. Try run this command to clean-up your script. Just run once. Original file will be backed up.
这很可能是因为\r
脚本中的回车。尝试运行此命令来清理您的脚本。只跑一次。将备份原始文件。
perl -pi.bak -e 's/\r$//' /home/pi/sh/test.sh
回答by Andrea Mauro
If you are editing the script file with Notepad++ on windows you can convert the EOL from the program menu with
如果您在 Windows 上使用 Notepad++ 编辑脚本文件,您可以使用以下命令从程序菜单中转换 EOL
Edit => EOL Conversion => Unix (LF)