4

I don't want to have another .sh file with a script and going to add a shell script code promptly in the Dockerfile. However, I receive an error message during the image build:

/bin/sh: syntax error: unexpected end of file (expecting "do")

Here is the part of the Dockerfile with a script:

RUN cat .env | while [ read LINE ] \
do \
    envName=$(echo "$LINE" | grep -o "^[^\=]*") \
    echo "PassEnv $envName" >> httpd.conf \
done

Any ideas what is wrong?

Ivan Ovcharenko
  • 372
  • 4
  • 14

3 Answers3

4

Remove the square brackets [], and use ; at the end of lines because docker joins lines when sees your \'s, so the commands get melt.

This is working for me:

RUN cat .env | while read LINE ;\
    do \
      envName=$(echo "$LINE" | grep -o "^[^\=]*"); \
      echo "PassEnv $envName" >> httpd.conf ;\
    done
Robert
  • 33,429
  • 8
  • 90
  • 94
4

I got this issue when I forgot to make sure my files I created on Windows and COPYd to Linux container had LF (instead of CRLF) line endings. Converting the file to LF line endings worked

mo.
  • 4,165
  • 3
  • 34
  • 45
  • It would've been even more helpful if you included "HOW" you converted! – mukesh.kumar Dec 12 '21 at 15:50
  • Thanks for the feedback. Most text editors, and many standalone tools, can do this. The instructions will vary based on the tool you use. I happened to use sublime text to do my conversion, but any further detail is best reserved for [separate questions](https://stackoverflow.com/questions/27810758/how-to-replace-crlf-with-lf-in-a-single-file/27814403) on specifically how to do that. – mo. Dec 13 '21 at 02:19
1

The while construct expects do to be on a separate line (or, to appear after a semicolon). So you probably want:

RUN cat .env | while [ read LINE ]; \
do \
    envName=$(echo "$LINE" | grep -o "^[^\=]*") \
    echo "PassEnv $envName" >> httpd.conf \
done

Note the semicolon on the first line.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112