Multiple files created with a script, only the last one

I had a rather strange problem when creating text files using linux shell scripts. The situation is as follows:

On my Synology Disk Station, I run sh-script. It accesses the local mySQL database using only a read-only SQL user. There are several calls (one line per call), and each of them writes the output to a CSV file in different places.

The .sh script looks like this:

/some/path/create_lists.sh mysql --arguments < /path/to/script1.sql > /path/to/outfile1.csv mysql --arguments < /path/to/script2.sql > /path/to/outfile2.csv mysql --arguments < /path/to/script3.sql > /path/to/outfile3.csv mysql --arguments < /path/to/script4.sql > /path/to/outfile4.csv 

Using my Windows PC I want to access these files.

In principle, this already works fine, but somehow only the MSI MSI file is directly read only by the last CSV file created. In the above example, outfile4.csv will be the only readable file. When exchanging lines 3 and 4, outfile3.csv will be readable. For outfile4.csv, a new file will be created with the same name, which cannot be opened by MS Excel. However, Notepad ++ can open it.

In Windows Explorer, it will look like this:

 \\myNAS\path\to outfile4.csv (working) outfile4.csv (not working) 

How can there be two files with the same name? And the one that works and the other does not?

+5
source share
1 answer

While writing this question, I solved it myself, but I still want to publish it for others to see.

Here is the solution: I wrote the scripts on my Windows PC using Notepad ++. I saved them in a separate script folder on my NAS. Launching their NAS apparently worked, but there was one small detail that caused the problem. Carriage Return.

Although Windows uses carriage return (\ r) and , line (\ n) results in \ r \ n for a new line, a unix-based system uses only line (\ n)

By writing a script on a Windows machine, I basically wrote:

 mysql --arguments < /path/to/script1.sql > /path/to/outfile1.csv\r\n mysql --arguments < /path/to/script2.sql > /path/to/outfile2.csv\r\n mysql --arguments < /path/to/script3.sql > /path/to/outfile3.csv\r\n mysql --arguments < /path/to/script4.sql > /path/to/outfile4.csv 

Reading the file on Linux led to the following:

 mysql --arguments < /path/to/script1.sql > /path/to/outfile1.csv?\n mysql --arguments < /path/to/script2.sql > /path/to/outfile2.csv?\n mysql --arguments < /path/to/script3.sql > /path/to/outfile3.csv?\n mysql --arguments < /path/to/script4.sql > /path/to/outfile4.csv 

I found out about this by specifying the contents of the contained folder via SSH, which gave me:

 ls -alh /path/to ... outfile4.csv ... outfile4.csv? 

And we have it! It is for this reason that the last file works, while others do not. This is also the reason why there may be two files with the "same" name. Windows simply does not display a question mark, which causes a lot of confusion.

So, if anyone ever comes across this problem, he can stumble on this topic and save some time. I think this is common knowledge for most people, but I'm still new to Linux, so I had to study it hard :)

+5
source

All Articles