1

Running into a weird issue while running a docker container (Ubuntu). I copy in some scripts, those scripts should run read a file line by line and execute a command.

When I type out the commands in the shell, it works as I expect. When I have the exact commands in a .sh file, it fails. I've tried every iteration of While read, IFS, changing file permissions, checking spaces, delimiters, etc. Cant seem to make it work.

I'm sure is something simple, but I have yet to find it.

My script:

while read LINE;
    do echo "$LINE";
done < "/megacmd/syncs"

Contents of syncs file:

$ cat syncs
sourcedir1 destdir1
sourcedir2 destdir2
sourcedir3 destdir3

Output when running the script:

# pwd
/megacmd
# ls
exclude.sh  excludes  init.sh  sync.sh  sync2.sh  sync3.sh  sync4.sh  syncs
# ./sync.sh
: No such filecannot open /megacmd/syncs
#

Why is there no space between filecannot?

Finally, when I'm manually writing this, it works fine:

# while read LINE;
> do echo "$LINE";
> done < "/megacmd/syncs"
sourcedir1 destdir1
sourcedir2 destdir2
sourcedir3 destdir3
#

edit1:

# ./sync.sh
: No such filecannot open /megacmd/syncs
# file syncs
syncs: ASCII text
#
markp-fuso
  • 28,790
  • 4
  • 16
  • 36
Gene Parmesan
  • 211
  • 2
  • 8

1 Answers1

1

@chepner was correct.

My script file was not saved with the correct line ending. A quick change in Notepad++ fixed it

Gene Parmesan
  • 211
  • 2
  • 8
  • 2
    [Character encoding](https://en.wikipedia.org/wiki/Character_encoding) is different from line endings. I suggest you correct your answer to say "not saved with the right line endings". – Shane Bishop Mar 26 '22 at 22:56