0

I am trying to write a simple script to print some of the variables extracted from another file. With all the variables defined, the aim is to print out the following line:

-bad 0.0 -aswap -AMX -decim $decim -dspfvs $dspfvs -grpdly $grpdly \

So the complications here is I need to print out not only the variables, but many other symbols, including - . \ and I would need a line break at the end.

I have tried

echo "  -bad 0.0 -aswap -AMX -decim $decim -dspfvs $dspfvs -grpdly $grpdly  \\"

which apparently didn't work and got some weird output like this:

\rpdly 67ecim 1792

where 67 and 1792 were my variables $grpdly and $decim

Thanks in advance for your help!

  • Consider using printf instead of echo? – SiKing Sep 14 '16 at 19:42
0

This doesn't explain everything, but at least part of the problem is that you have carriage returns at the end of their values. This means that it's printing something like this:

  -bad 0.0 -aswap -AMX -decim 1792<CR>
 -dspfvs something<CR>
 -grpdly 67<CR>
 \\

...except that each "line" is printed on top of the previous one, because they're separated by carriage returns rather than linefeeds. Now, if you compare what I just described to your output, it won't quite match (mine gives " \pdly 6792swap -AMX -decim 1792"), so I think there's another in there somewhere for some reason.

The cause of this is probably that the file you're reading from is in DOS/Windows format, which uses a carriage return followed by linefeed at the end of each line; unix just uses linefeed, and tends to treat the carriage return as part of the content of the line, e.g. reading it into variables at the end ... just like you're getting. One option is to convert the file to unix format with dos2unix or some similar program. Another is to remove the carriage returns as you read from the file, something like this:

set decim = `cat decimfile | tr -d '\r'`

(with whatever actual command you're using instead of the cat part.) Or, you can clean the variables after reading them with:

set decim = `echo $decim | tr -d '\r'`

Note that there are a number of different conventions for how to "write" a carriage return; I've been using "<CR>", but tr understands "\r", and some other programs will use "^m" or "^M".

  • Thanks Gordon. I included | tr -d '\r' for all my variables and the problem is solved like magic! – Koni Sep 15 '16 at 15:26
  • And thanks for taking time to explain the carriage returns. It is very helpful. – Koni Sep 15 '16 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.