debugging 设陷并读取$BASH_COMMAND,但使用是/否

t1rydlwq  于 2022-11-14  发布在  其他
关注(0)|答案(1)|浏览(126)

我正在调试模式下运行一个简单的脚本。

#!/bin/bash
trap 'read -p "run: $BASH_COMMAND"'  DEBUG
command 1
command 2



 **Current output:** 
run: command 1 <press enter and the command executes>
run: command 2 <press enter and the command executes>

但我想在每次执行前循环询问yes/no
预期输出:

run: command 1 yes/no? <input 'yes' + enter and the command executes>
run: command 2 yes/no? <input 'yes' + enter and the command executes>

我试过了

trap [['read -p "run: $BASH_COMMAND" && "continue [y/n]" ' ; echo $REPLY)" == [Yy]* ]] && echo Continuing || echo Stopping DEBUG

但我想不通。
基本上,我尝试在trap/debug命令中执行两次读取操作,在第二次读取时,我希望在执行之前执行逻辑操作。
谁能给我指一下正确的方向吗?可能是进程替换

ljsrvy3e

ljsrvy3e1#

也许像这样

#! /bin/bash

confirm() {
    read -rp "run: $BASH_COMMAND, continue [y/n]: "
    if [[ "$REPLY" == [Yy]* ]]; then
        echo Continuing
    else
        echo Stopping DEBUG
        exit
    fi
}

trap confirm  DEBUG
command 1
command 2

相关问题