shell script error expecting "do"

41,411

Solution 1

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

Solution 2

Give this a try:

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

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

Share:
41,411

Related videos on Youtube

Jordan
Author by

Jordan

My name is Jordan and I write the codes.

Updated on June 04, 2021

Comments

  • Jordan
    Jordan almost 3 years
    #!/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
    
  • jwir3
    jwir3 about 13 years
    Ah, whoops... beat me to it. ;)
  • Jordan
    Jordan about 13 years
    I see 0d 0a sequences but your line to strip them out with the tr command didn't work for me. I used vi after that didn't work and its working fine now! thanks
  • Ofer
    Ofer about 13 years
    No problem. If you are uploading the script to your linux/unix box via FTP, you can select ascii/text format, and FTP will handle the end of line conversion for you. If, on the other hand, you are creating the file some other way, tr is a good tool to strip unwanted characters.
  • Josh Graham
    Josh Graham about 5 years
    @Jordan That command did not work for me if I tried to name the new script the same name as the old script. If you use a different name, delete the old file, then rename the new file to the old file name it will work.
  • viveknaskar
    viveknaskar almost 4 years
    This command worked for me as I had messed up the line endings. Thanks!

Related