当在bash stdout或stderr重定向,其实我重定向到一个文件 [英] When redirecting in bash to stdout or stderr, I actually get redirected to a file
问题描述
我节省通过ssh远程上执行一个命令的输出,但我也执行一些回声。为了看清屏幕上的信息,而不是将它们保存到我想他们重定向变量。
我曾尝试>&功放; 1>和2,但在每种情况下一个文件名为无论是1创建或2
这是扩大引号或转义字符的问题?
I'm saving the output of one command executed on a remote via ssh, but I also execute some echos. In order to see the messages on the screen and not save them to the variable I want to redirect them. I have tried >&1 and >&2 but in each case a file is created with the name either "1" or "2" Is this an issue of expanding quotes or escaping characters?
OUTPUT=$(sshpass -p password ssh user@ip 'echo "Message1" >&2 ;
su -lc "./rootscript.sh" >&2;
echo "$?" ')
echo "su output is: $OUTPUT"
输出:
在屏幕上没有任何一个文件名为2文本密码内(假设消息1被覆盖)程序仍在等待输入密码,即使没有提示,所以当我输入输出还是不错的:
Nothing on screen, a file named "2" with the text "Password" inside (assuming Message 1 was overwritten) The program still waits for a password, even without a prompt, so when I enter it the output is good:
su output is: 0
我怎样才能让我的消息,在屏幕上显示?
How can I get my messages to show on screen?
推荐答案
@JonathanLeffler抓住了问题的时候了 - 你没有使用bash,但是tcsh的。
@JonathanLeffler caught the problem right away -- you're not using bash, but tcsh.
tcsh中有它自己的语法使用bash是不相容的。
tcsh has its own syntax incompatible with bash.
这并不不过没关系:因为 SSH
和 sshpass
返回远程命令的退出code,这是你应该做的是:
It doesn't matter though: since ssh
and sshpass
return the remote command's exit code, this is how you should be doing it:
if sshpass -p password ssh user@ip 'echo "Message1"; su -lc "./rootscript.sh"'
then
echo "The command succeeded (exit code $?)"
else
echo "The command failed (exit code $?)"
fi
如果你真的想在远程shell中运行bash中的东西,你可以做使用达菲查尔斯的code对于小的修改:
If you really wanted to run something in bash on the remote shell, you could do use Charles Duffy's code with a minor change:
# v-- shell specified here
output=$(sshpass -p password ssh user@ip bash << 'EOF'
echo "Message1" >&2
su -lc "./rootscript.sh" >&2
echo "$?"
EOF
)
这篇关于当在bash stdout或stderr重定向,其实我重定向到一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!