Home > Back-end >  Add a waiting point in a heredoc block running on a remote server
Add a waiting point in a heredoc block running on a remote server

Time:05-15

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

CodePudding user response:

A workaround is to pass the script as a ssh argument :

ssh -t $username@$host "$(cat<<-EOF_REMOTE_BASH
    read -s -n 1 -p "Press any key to continue . . ."
EOF_REMOTE_BASH
)"

CodePudding user response:

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"

  • Related