bash 脚本破坏 echo 中的变量
我正在尝试编写一个脚本来读取远程计算机的主机名,然后在以下命令中使用该结果。但是,该变量似乎已损坏或发生其他情况。
这是正在发生的情况的示例:
sbaker@eye004:~/workspace/fire_trunk$ REMOTE_HOSTNAME="`ssh $REMOTE 'hostname'`"
sbaker@eye004:~/workspace/fire_trunk$ echo "before $REMOTE_HOSTNAME after"
打印(注意前缀空格): " after sbaker-PC"
sbaker@eye004:~/workspace/fire_trunk$ echo $REMOTE_HOSTNAME
打印: "sbaker-PC"
我想知道为什么这个变量似乎无法使用并且做了奇怪的事情(如果后一个词比前一个词长,它会写在字符的顶部)。 我希望第一个回显打印:“before sbaker-PC after”
。
我只是在这里做了一些愚蠢的事情吗? 我在 ubuntu 11 上使用 bash。
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 characters).
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将其通过
od -c
,您会发现它实际上返回sbaker-PC\r
。末尾的 CR 导致它在回显文本的其余部分之前将光标返回到第一列,从而模糊了“之前”。至于为什么它添加\r
,也许另一端提供主机名的文件是用DOS行结尾(CRLF)而不是*nix行结尾(LF)保存的。If you put it through
od -c
you'll see that it's actually returningsbaker-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).