开发者

bash script clobbering variable in echo

I am trying to write a script which reads the hostname of the remote machine and then uses that result in following commands. However, the variable seems to be corrupted or something.

Here is an example of what is happening:

sbaker@eye004:~/workspace/fire_trunk$ REMOTE_HOSTNAME="`ssh $REMOTE 'hostname'`"
sbaker@eye004:~/workspace/fire_trunk$ echo "before $REMOTE_HOSTNAME after"

prints (note the prefixing whitespace): " after sbaker-PC"

sbaker@eye004:~/workspace/fire_trunk$ echo $REMOTE_HOSTNAME

prints: "sbaker-PC"

I am wondering why the variable seems unusable and doing weird things (if the after word is longer than the before word, it writes over the top of the c开发者_如何学编程haracters). I would expect the first echo to print: "before sbaker-PC after".

Am I just doing something stupid here? I am using bash on ubuntu 11.


If you put it through od -c you'll see that it's actually returning sbaker-PC\r. The CR at the end is causing it to return the cursor to the first column before echoing the rest of the text, obscuring the "before". As for why it's adding \r, perhaps the file giving the hostname on the other side was saved with DOS line endings (CRLF) instead of *nix line endings (LF).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜