0

I am using Windows Subsystem for Linux (wsl) to run a bash script to copy a database file from a remote server to my local PC with the following command:

ssh administrator@192.168.X.X "mysqldump -uroot -ppassword databaseName" > databaseName.sql

The command works and copies the db perfectly, but as soon as I put another line below it:

ssh administrator@192.168.X.X "mysqldump -uroot -ppassword databaseName" > databaseName.sql
sleep 5

the copied db gets a dot . (but the dot is in the middle of the line) at the end of the name like: databaseName.sql. <-- But like I said, this second dot is a bit higher up, in the middle of the line.

When I look at the file name on the server before it gets copied, it is: 'databaseName.sql'$'\r'

Now the problem goes away when I add a ; at the end of the ssh line like:

ssh administrator@192.168.X.X "mysqldump -uroot -ppassword databaseName" > databaseName.sql;
sleep 5

But then I get a bash message that says line 1: $'\r': command not found. Apart from this message everything else works fine, since the error does not interrupt the rest of the script. But I would like to know why this happens without the ; at the end, since the file is unusable with the dot at the end.

Some error searching is leading me to think that there may be a space added to the end of the line when the file gets dumped or something like that...

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Alfa Bravo
  • 1,961
  • 2
  • 25
  • 45

1 Answers1

2

Your script is using Windows-like line-endings characters (CR+LF, ASCII sequence 0x0d 0x0a). The 0x0d ASCII character, also represented as \r, is not recognized by Linux to be part of a new-line sequence, so it includes it in your filename when this one is just preceding it.

Your script may have been created by a Windows editor, with by default may haved saved it in CRLF format. However, the vast majority of text editors (on any platflorm) have an option to control this line-ending format (sometimes by giving an OS platform: Window = CRLF, Unix = LF, Mac = CR), so you have to choose the Unix / LF option for further editings.

For existings files like your script, there are many ways to convert them to unix-like characters, in order to fix your error.

yolenoyer
  • 8,797
  • 2
  • 27
  • 61
  • Ah ok, I did create it in VSCode and you are right, I can just change it to LF which I did and this immediatly fixed the issue. Thanks a lot. – Alfa Bravo Jul 16 '20 at 05:56