Syntax error near unexpected token "done"-- while read line

iamjane

I keep getting this error: syntax error near unexpected token 'done' but I don't understand why.

I tried adding dos2unix *.sh after #!/bin/sh, but that just gave me an error that said no such file or directory in addition to the "done" error.

This is a .sh file. I'm very new to writing scripts. Help?

I'm running

sh thisfile.sh program_input input

on linux

edit I added some quotations around the variables- same error

#!/bin/sh
fst=$1
input=$2
while read line
do
    result=$(cat "$line" | program "$fst")
    if [ "$result" = "" ];
    then
        printf "$line\t=>\t *none* 0\n"
    else
        printf "$line\t=>\tyes\n"
    fi
done < "$input"

"$input" is just four rows of words like "they" "can" "fish" "they" "can" "take" "table"

If I run cat "$line" | program "$fst" it works fine

note if I take out everything in the loop and just printf $line it gives the same "done" syntax error

ilkkachu

syntax error near unexpected token 'done' is the error Bash gives when it sees the reserved word done without a matching do before it. It has nothing to do with quotes, but most likely a lot to do with your file having DOS/Windows-style CRLF line endings. The carriage return (CR, \r) is seen by the shell as a regular character, so it doesn't see the reserved word do, but do\r instead. On the other hand, on the last line it does recognize done, since it's separated from the line end, and the CR there with that space.

Run the script file itself through dos2unix. Don't add the dos2unix command in the script file.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Related Related

HotTag

Archive