简体   繁体   English

SSH heredoc:bash提示符

[英]SSH heredoc: bash prompt

I am attempting to write a shell script which SSHs into a server and then prompts the user to enter a file/folder. 我正在尝试编写一个SSH脚本到服务器的shell脚本,然后提示用户输入文件/文件夹。

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? 我该怎么做才能获得与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: 如果没有,是否有任何方法我布置我的脚本,以便提示确实工作,而不用双引号和转义包装所有内容,如下所示:

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
"

If you don't need any variables from the client, why not try - and ssh -t might 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'."
'

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。

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. 要使它与heredoc一起使用,应该足以调用bash或要在远程服务器上使用的任何shell。 Like so: 像这样:

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. 请注意,您可能希望在FILEPATH上转义$ ,因为当前将由本地shell而不是远程shell进行插值。

This one seems to work: 这个似乎工作:

T=$(tty) ; bash <<XXX
echo Hi "$T"
read p <$T
echo p: \$p
echo Bye
XXX

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM