且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SSH定界符:bash提示符

更新时间:2023-02-20 08:46:14

如果你不需要任何来自客户端的变量,为什么不尝试 - 和的ssh -t 可能是有用的。

 导出客户端=我CMDS = $(猫<< CMD
CD下载/
阅读-e -p输入该文件的路径:FILEPATH
回声\\ $ FILEPATH
EVAL FILEPATH =\\ $ FILEPATH回声下载\\ $ FILEPATH至$客户端
CMD
)SSH本地主机-t$ CMDS

请注意,如果你用双引号唯一的问题是逃跑了,你不打算
使用 单引号在你的脚本,那么你可以UST做到这一点:

 的ssh -t $ SERVER
#脚本,转义。
如果你想获得一个本地定义变量#,
回声客户端是'$ CLIENT'。

I am attempting to write a shell script which SSHs into a server and then prompts the user to enter a file/folder.

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.

When I was using double quotes, the prompt worked fine. However, now that I am using heredoc, the prompt no longer works.

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:

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.

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'."
'