سؤال

I have a Cat command within a for loop. The for loop goes through a bunch of folders.

Within my code, I basically commented everything out, except the cat command and the outer loop. Once I comment out the loop, the cat command works. Everything works when I remove the cat command too and what's in it.

The error I get is: syntax error: unexpected end of file. It always refers to the last line, being the done in this case.

#!/bin/sh
cd $HOME/filedirectory
for k in *; do
    cat > $HOME/mongodb/bin/script1.js << EOF
       ##commented out stuff
    EOF
done
هل كانت مفيدة؟

المحلول

You need to remove space before EOF in closing:

#!/bin/sh
cd $HOME/filedirectory
for k in *; do
    cat > $HOME/mongodb/bin/script1.js << EOF
       ##commented out stuff
EOF
done

As per man bash:

If the redirection operator is <<-, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-docu- ments within shell scripts to be indented in a natural fashion.

So you can change the heredoc operator to <<-. then you can indent both the heredoc and the delimiter with tabs. (no spaces)

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top