3

Look at these two files:

They look to me entirely identical, but File 1 has 41 bytes and File 2 has 44 bytes.

Now, I would not care about 3 extra chars, however, I think these chars are also responsible to a strange error: when I clone and run these files, File 1 runs OK while File 2 returns an error on line 3: "./BasicCheck.sh: line 3: cd: case0 : No such file or directory".

So: what are these extra chars? Why do they cause errors? And what should I do to fix these errors?

My guess is that they are newlines - perhaps File 1 has Linux-based newlines and File 2 has Windows-based newlines. Is it correct? And why should it cause such errors?

  • 8
    Don't make us chase links. Post the text here, and highlight the differences. – glenn jackman Mar 19 '19 at 15:18
  • 2
    @glennjackman that's exactly the point - there are no visible differences. If I copy the text from both links, they will appear exactly the same and there will be no way to find out the difference. This is why I give the links to the original files. – Erel Segal-Halevi Mar 19 '19 at 15:19
  • 6
    Run both scripts through od -c filename and you'll be shown the exact characters in the files. If one script has DOS-style newlines (with a 3 line file, that would account for the extra 3 bytes) then I would have expected a different error message. Or specifically, the same error message, but "garbled". – glenn jackman Mar 19 '19 at 15:21
  • 2
    Please also show exactly how you invoke these scripts. – glenn jackman Mar 19 '19 at 15:21
  • If it's a newline issue then the directory you're trying to cd into looks like $filename\r with an extra invisible character. But I'd expect the error message to start with : No such file or directory because of the carriage return in the middle of the error message. – glenn jackman Mar 19 '19 at 15:23
  • 1
    One file has lines terminated by cr/lf (dos mode), the other just by lf (unix mode). The extra chars are carriage returns / \r / CR / ascii 13. –  Mar 19 '19 at 15:28
  • @glennjackman the "od" tip was very useful - I did not know about it - it immediately showed the extra "\r"! – Erel Segal-Halevi Mar 19 '19 at 15:32
  • 1
    The dos2unix command is your friend to fix such files. You should investigate the settings for your editor to save files in "unix format". – glenn jackman Mar 19 '19 at 15:42
  • When I download the two linked files I see two totally different HTML files. If you're going to link to external files, link directly to the files in question; not web pages! Or better, as @glennjackman says, don't post links at all. – Rich Mar 19 '19 at 15:49

1 Answers1

11

The file command points out the issue:

% file file*
file1: Bourne-Again shell script, ASCII text executable
file2: Bourne-Again shell script, ASCII text executable, with CRLF line terminators

The second script is in "DOS" format. and so will not work as expected. You can convert it with dos2unix and the results are now the same

% dos2unix file2
dos2unix: converting file file2 to Unix format ...

% ls -l file1 file2
-rw-r--r-- 1 sweh sweh 41 Mar 19 11:23 file1
-rw-r--r-- 1 sweh sweh 41 Mar 19 11:25 file2

When you try to use a DOS format file with Unix commands, there is a trailing control-M at the end of every line. So a line such as

a=b

will really set a to b followed by control-M, shorthand ^M:

a=b^M

Every command will be similarly impacted; eg a simple line with the two-character command ls would try to run the three-character command ls<control-M>, which doesn't exist.

Rich
  • 823