1

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
)"
Enissay
  • 4,969
  • 3
  • 29
  • 56

2 Answers2

1

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
)"
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • I have added Edit.1 which matches my use case. For some reason you example works, standalone BUT not when integrated into my code (see edit above) – Enissay May 15 '22 at 14:37
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"

jhnc
  • 11,310
  • 1
  • 9
  • 26