我试图添加一个等待点到我的代码,然后可以手动恢复/解锁。遗憾的是,它并没有像预期的那样起作用。我猜是由于heredoc的工作方式(=stdin)。
有没有人建议另一个仍然使用heredoc的替代方案,因为它使代码非常清晰或任何类似的语法服务于相同的目的。
username=root
host=blah
ssh -t $username@$host <<-EOF_REMOTE_BASH
## set options
set -x
printf ">>> Connected to %s@%sn" "$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
)"
您正在尝试分配一个tty (-t
),但没有从stdin读取。这提供了一个解决方案的线索:
username=root
host=blah
ssh -t $username@$host '
## set options
set -x
printf ">>> Connected to %s@%sn" "$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 . . ." </dev/tty
done
## Quit server
exit
'
关于保留heredoc的方法,请参见:如何在Bash中将heredoc值赋给变量?
你可以这样做:ssh -t $username@host "$cmds"
一种变通方法是将脚本作为ssh参数传递:
(如果您在脚本中定义变量,通常最好使用引号禁用扩展:'EOF_REMOTE_BASH'
,在这种情况下,您不会在脚本中引用变量:"$line"
,删除)
ssh -t $username@$host "$(cat<<-'EOF_REMOTE_BASH'
read -s -n 1 -p "Press any key to continue . . ."
EOF_REMOTE_BASH
)"