Linux 期望“做”的 shell 脚本错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5491634/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 03:28:35  来源:igfitidea点击:

shell script error expecting "do"

linuxbashshell

提问by Jordan

#!/bin/sh
while true ; do
echo "WTF"
done

This is giving a syntax error: syntax error: unexpected end of file (expecting "do")

这是一个语法错误:语法错误:文件意外结束(期待“做”)

I also tried:

我也试过:

#!/bin/sh
while :
do
echo "WTF"
done

采纳答案by Aaron H.

I suspect line endings.

我怀疑行尾。

Try:

尝试:

hexdump -C yourscript.sh 

And look for 0d 0asequences. You can strip \r(0d) with the trcommand:

并寻找0d 0a序列。您可以使用以下命令去除\r( 0d) tr

cat yourscript.sh | tr -d '\r' >> yournewscript.sh

回答by Eric Bernier

Give this a try:

试试这个:

#!/bin/sh
while [ true ]
do
    echo "WTF"
done

Please pay special attention to the spaces in the line 'while [ true ]'

请特别注意 'while [ true ]' 行中的空格