'Add a waiting point in a heredoc block running on a remote server
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
)"
Solution 1:[1]
You are trying to allocate a tty (-t) but not reading from stdin.
This provides a clue to a solution:
username=root
host=blah
ssh -t $username@$host '
## 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 . . ." </dev/tty
done
## Quit server
exit
'
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"
Solution 2:[2]
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 \)
ssh -t $username@$host "$(cat<<-'EOF_REMOTE_BASH'
read -s -n 1 -p "Press any key to continue . . ."
EOF_REMOTE_BASH
)"
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
