在远程服务器上运行的Heredoc块中添加等待点
我正在尝试为我的代码添加一个等待点,然后可以手动恢复/未盖住。可悲的是,它无法正常工作。我想由于Heredoc的工作原理(= Stdin)。
有人可以建议另一种仍在使用Heredoc的替代方案,因为它使代码非常清晰,或者任何类似的语法都具有相同的目的。
username=root
host=blah
ssh -t $username@$host <<-EOF_REMOTE_BASH
## set options
set -x
printf ">>> Connected to %s@%s\n" "$username" "$host"
## loop and run logic
# this is sample loop only
for i in $(seq 1 2 20); do
## some code
# more code
# ...
# ...
# ...
## Wait until unblocked manually
# NOT WAITING!
read -s -n 1 -p "Press any key to continue . . ."
done
## Quit server
exit
EOF_REMOTE_BASH
编辑1:
username=root
host=blah
ssh -t $username@$host "$(cat<<-EOF_REMOTE_BASH
## prep file
src_file=/tmp/test
cat <<- 'EOF' > ${src_file}
10
20
30
EOF
## Loop over file, wait at each iteration
while IFS="" read -r line || [ -n "\$line" ]; do
echo "\$line"
read -s -n 1 -p "Press any key to continue . . ." && printf "\n"
done <"${src_file}"
EOF_REMOTE_BASH
)"
I am trying to add a waiting point to my code which can then be resumed/unblocked manually. Sadly it is not working as expected. I guess due to how heredoc works (=stdin).
Could someone suggest another alternative still using heredoc given it keeps the code very clear or any similar syntax serving the same purpose.
username=root
host=blah
ssh -t $username@$host <<-EOF_REMOTE_BASH
## set options
set -x
printf ">>> Connected to %s@%s\n" "$username" "$host"
## loop and run logic
# this is sample loop only
for i in $(seq 1 2 20); do
## some code
# more code
# ...
# ...
# ...
## Wait until unblocked manually
# NOT WAITING!
read -s -n 1 -p "Press any key to continue . . ."
done
## Quit server
exit
EOF_REMOTE_BASH
Edit.1:
username=root
host=blah
ssh -t $username@$host "$(cat<<-EOF_REMOTE_BASH
## prep file
src_file=/tmp/test
cat <<- 'EOF' > ${src_file}
10
20
30
EOF
## Loop over file, wait at each iteration
while IFS="" read -r line || [ -n "\$line" ]; do
echo "\$line"
read -s -n 1 -p "Press any key to continue . . ." && printf "\n"
done <"${src_file}"
EOF_REMOTE_BASH
)"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试分配TTY(
-T
),但不是从STDIN读取的。这提供了解决方案的线索:
有关保留Heredoc的方法,请参见:如何将HEREDOC值分配给bash中的变量? /a>
然后您可以做:
ssh -t $ username@host“ $ cmds”
You are trying to allocate a tty (
-t
) but not reading from stdin.This provides a clue to a solution:
For methods to retain the heredoc, see: How to assign a heredoc value to a variable in Bash?
Then you can do:
ssh -t $username@host "$cmds"
解决方法是将脚本作为ssh参数传递:(
如果您在脚本中定义变量,通常最好用引号禁用扩展:
'eof_remote_bash'
,在这种情况下,您不引用引用脚本中的变量:“ \ $ line”
,删除\)A workaround is to pass the script as a ssh argument :
(In case you define variables in the script, it's usually better to disable expansion with quotes :
'EOF_REMOTE_BASH'
, in which case you don't quote variables inside the script :"\$line"
, remove \)