1

Suppose that I have a program make_ndx that outputs via the -o switch. I would like to call myprogram from a bash shell script and output two files, out1.ndx and out2.ndx. (myprogram takes a string as input to the -o switch and automatically appends the file extension .ndx to that string.) So, I have written a bash shell script:

#!/bin/bash

make_ndx -o out1
make_ndx -o out2

I obtain two output files: out1.ndx and out2.ndx. But I would like to instead obtain out1.ndx and out2.ndx.

It seems like the character in corresponds to a newline or return character (possibly \n or \r?), because when I copy and paste the file name to a text file (or to this post), out1.ndx appears as

out1
.ndx

(Note that, for the purpose of this post, I am using another character () to represent the square symbol that I see in my SSH GUI, since when I copy the actual character from the SSH GUI to here, I simply obtain a newline character.)

How can I write my bash shell script so that the newline character does not appear in the filename of the first output file? The same problem occurs when I put an extra space between the calls to make_ndx:

#!/bin/bash

make_ndx -o out1

make_ndx -o out2

Thanks for your time.

1 Answer 1

2

There seem to be two possibilities:

  1. You're passing \r as part of the filename. This shouldn't normally happen. It could happen if you have a file with mismatched EOL characters. Windows uses a CRLF pair to end a line in a text file; Unix uses only LF. Depending on how you edit the file, you can manage to get CRLF in there, and that will break all kinds of things.

    Another variant of this is that if out1 is actually coming from a variable (make_ndx -o "$out1"), you may have captured a LF in the variable. Doing echo -n "$out1" | xxd -p will let you know if you have; check if it ends in 0a.

  2. make_ndx is buggy. The command doesn't get passed \r, its inserting it internally. Nothing you can do from a bash script (well, other than mv to fix the name). If you have source to make_ndx, you could fix it yourself, else you'll need to contact whoever supports it.

You can check for mixed line endings a bunch of ways. For example, if you use xxd to take a hex dump of the bash script, 0x0d is CR (\r). 0x0a is LF (\n). You should not see any CRs in the file.

2
  • I indeed found some CRs in the file. I think that it was because the file was created in Windows Notepad (not a good idea!). Thanks for your time.
    – Andrew
    Aug 25, 2012 at 21:45
  • 1
    @Andrew I believe Wordpad on Windows can handle Unix-style line endings. But even better, you could install gVim on Windows. Or install flip on your Linux box, and make sure to do a flip -u on text files coming from Windows.
    – derobert
    Aug 28, 2012 at 16:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .