SSH heredoc:bash 提示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13934280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SSH heredoc: bash prompt
提问by
I am attempting to write a shell script which SSHs into a server and then prompts the user to enter a file/folder.
我正在尝试编写一个 shell 脚本,该脚本通过 SSH 连接到服务器,然后提示用户输入文件/文件夹。
ssh $SERVER <<EOF
cd downloads/
read -e -p "Enter the path to the file: " FILEPATH
echo $FILEPATH
eval FILEPATH="$FILEPATH"
echo "Downloading $FILEPATH to $CLIENT"
EOF
I am using heredoc instead of double quotes after the SSH to execute these commands because my shell script is rather large and I don't want to be escaping every double quote.
我在 SSH 之后使用 heredoc 而不是双引号来执行这些命令,因为我的 shell 脚本相当大,我不想转义每个双引号。
When I was using double quotes, the prompt worked fine. However, now that I am using heredoc, the prompt no longer works.
当我使用双引号时,提示工作正常。但是,现在我正在使用heredoc,提示不再有效。
What can I do to get the prompt to work with heredoc? And if not, is there any way I layout my script so that the prompt does work without wrapping everything in double quotes and escaping, like so:
我该怎么做才能得到使用 heredoc 的提示?如果没有,有没有什么办法可以让我的脚本布局,这样提示就可以工作,而无需将所有内容都用双引号括起来并转义,如下所示:
ssh $SERVER "
cd downloads/
read -e -p \"Enter the path to the file: \" FILEPATH
echo $FILEPATH
eval FILEPATH=\"$FILEPATH\"
echo \"Downloading $FILEPATH to $CLIENT\"
exit
"
采纳答案by Faiz
If you don't need any variables from the client, why not try - and ssh -tmight be useful.
如果您不需要来自客户端的任何变量,为什么不尝试 - 并且ssh -t可能会有用。
export CLIENT=me
CMDS=$(cat <<CMD
cd downloads/
read -e -p "Enter the path to the file: " FILEPATH
echo $FILEPATH
eval FILEPATH="$FILEPATH"
echo "Downloading $FILEPATH to $CLIENT"
CMD
)
ssh localhost -t "$CMDS"
Note that if your only issue with double-quotes is escaping, and you do not plan on
using 'single quotes in your script, then you can ust do this:
请注意,如果双引号的唯一问题是转义,并且您不打算在'脚本中使用单引号,那么您可以这样做:
ssh -t $SERVER '
# your script, unescaped.
# if you want access to a locally defined variable,
echo "CLIENT is '$CLIENT'."
'
回答by kdubs
this works, tab completion on the host works.
这有效,主机上的选项卡完成工作。
var=$(cat<<EOF
read -e -p Path pname;
echo $pname;
hostname;
echo $pname;
cd $pname;
pwd;
touch THIS ;
exit;
EOF
)
ssh -t NODE $var
on mine this creates the file THIS in the prompted for directory.
在我的上,这会在提示输入的目录中创建文件 THIS。
回答by Chris
To make it work with a heredoc, it should be sufficient to invoke bash, or whatever shell you want to use on the remote server. Like so:
为了使它与heredoc一起工作,调用bash,或者你想在远程服务器上使用的任何shell应该就足够了。像这样:
ssh $SERVER bash <<EOF
cd downloads/
read -e -p "Enter the path to the file: " FILEPATH
echo $FILEPATH
eval FILEPATH="$FILEPATH"
echo "Downloading $FILEPATH to $CLIENT"
EOF
Note that you probably want to escape the $on FILEPATH, since currently that will be interpolated by the local shell rather than the remote shell.
请注意,您可能想要转义$on FILEPATH,因为目前它将由本地 shell 而不是远程 shell 插入。
回答by full.stack.ex
This one seems to work:
这个似乎有效:
T=$(tty) ; bash <<XXX
echo Hi "$T"
read p <$T
echo p: $p
echo Bye
XXX

