top of page

Shell script error; syntax error near unexpected token `fi`;

Working on a very simple containerized python flask application (base image ubuntu), my requirement was to configure running of the python file during startup of the application using environment variable.

I was building this using PyCharm on Windows 10 OS with WSL 2 enabled and of-course, docker installed.

Created a simple bash file with just below code - 

#!/bin/bash
if [ $PROCESS_MODE == "main" ]
then
 cd /usr/bin
 python3 /main.py
else
 cd /usr/bin
 python3 /alternate_main.py
fi

When I containerized the application and started docker run, it threw below error 

syntax error near unexpected token `fi''


Seems to be very common problem faced over the internet, but no matter what I tried, it did not resolve the issue (tried on Powershell, Ubuntu on WSL 2, changing encodings etc.) 

I got inside the container using docker run -it --entrypoint=/bin/bash and created a new bash file using vim with exact same code and it worked.


After some more investigations, realized that files created in DOS/Windows use carriage return (\r) and line feed (\n) for line endings. However, files in Unix/Linux solely use line feed. 

Therefore, when transferring a file from one system to another, we need to convert these files.

After doing this, it worked like a charm! I then modified the docker file to convert all bash files using dos2unix.

You can find complete code here on github.

1 view0 comments

Comments


bottom of page