在远程服务器上运行的Heredoc块中添加等待点

发布于 2025-01-29 10:59:08 字数 1238 浏览 3 评论 0原文

我正在尝试为我的代码添加一个等待点,然后可以手动恢复/未盖住。可悲的是,它无法正常工作。我想由于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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里°也失望 2025-02-05 10:59:08

您正在尝试分配TTY(-T),但不是从STDIN读取的。
这提供了解决方案的线索:

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
'

有关保留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:

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"

软的没边 2025-02-05 10:59:08

解决方法是将脚本作为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
)"

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
)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文