0

I'm trying to run a shell script to build a docker image, but always getting a error that I cannot correct:

Neomind_dev     | build.sh: line 3: syntax error near unexpected token `$'\r''
'eomind_dev     | build.sh: line 3: `fi

but the code is super simple:

if [ ! "$(ls /usr/local/fusion)" ]; then 
    echo aaa
fi 
cp /usr/local/fusion/target/fusion-Neomind-dev.war /usr/local/tomcat/webapps/fusion.war 

can someone help me?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • 1
    As an aside (the linked duplicate is on-point for your real problem): Note that using `ls` in scripts is not good practice. If you want to detect whether `/usr/local/fusion` exists, `if [ -e /usr/local/fusion ]; then` or `if test -e /usr/local/fusion; then` will do so more efficiently (without spawning off a subshell and then invoking an external executable within it). – Charles Duffy Jan 17 '22 at 16:41

1 Answers1

1

Run

dos2unix your_script.sh

or

sed -i 's/\r//' your_script.sh
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Debugger
  • 494
  • 5
  • 18
  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section _Answer Well-Asked Questions_, and therein the bullet point regarding questions that "have been asked and answered many times before". – Charles Duffy Jan 17 '22 at 16:43
  • I run the script in dockerfile like this "CMD /bin/bash build.sh" Should i pass this sed -i after the bash? – Lucas Fellipe Mondini Pereira Jan 17 '22 at 16:44
  • @LucasFellipeMondiniPereira, this should be done _on the source files_ that Docker is reading -- that includes `build.sh` -- _before_ Docker starts. – Charles Duffy Jan 17 '22 at 16:46
  • @LucasFellipeMondiniPereira, ...how to change the file type depends on which editor you're using. For example, in vim, `:set fileformat=unix` will stop it from using DOS/Windows CRLF newlines and make it switch to UNIX-format LFs. If these files are from source control, all major SCMs (git, svn, cvs, etc) all let you control which line ending type text files are checked out with. – Charles Duffy Jan 17 '22 at 16:46
  • Please add some explanation to your answer such that others can learn from it – Nico Haase Jan 17 '22 at 16:47
  • 1
    @NicoHaase, on a duplicate it would be better if the answer was just deleted -- that way the explanation on the answers on the preexisting duplicate can be used, or extended as-needed. We don't need answers covering the same ground spread across many questions when they could be consolidated to have a single focus for edits/voting/comments/other quality enhancement efforts. – Charles Duffy Jan 17 '22 at 16:48
  • @CharlesDuffy i'm using vscode; i'm actually coding in windows and just running the script on the container, wich is a linux – Lucas Fellipe Mondini Pereira Jan 17 '22 at 16:49
  • 1
    @LucasFellipeMondiniPereira, so the next question is how to make VS Code on Windows save your files in UNIX format. I bet if you search you'll find an answer elsewhere on this site. – Charles Duffy Jan 17 '22 at 16:49
  • 1
    @LucasFellipeMondiniPereira, ...and indeed, it _is_ already asked-and-answered: [How can I make all line endings (EOLs) in all files in Visual Studio Code UNIX-like?](https://stackoverflow.com/questions/48692741/how-can-i-make-all-line-endings-eols-in-all-files-in-visual-studio-code-unix) – Charles Duffy Jan 17 '22 at 16:51