shell script error expecting “do”

2020-05-17 11:36发布

#!/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

标签: linux bash shell
2条回答
欢心
2楼-- · 2020-05-17 11:53

I suspect line endings.

Try:

hexdump -C yourscript.sh 

And look for 0d 0a sequences. You can strip \r (0d) with the tr command:

cat yourscript.sh | tr -d '\r' >> yournewscript.sh
查看更多
Deceive 欺骗
3楼-- · 2020-05-17 11:59

Give this a try:

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

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

查看更多
登录 后发表回答