You could use extdebug
:
shopt -s extdebug
trap '
read -n1 -p "run \"$BASH_COMMAND\"? " answer <> /dev/tty 1>&0
echo > /dev/tty
[[ $answer = [yY] ]]' DEBUG
cmd1
cmd2
...
For reference, the zsh
equivalent would be:
TRAPDEBUG() {
read -q "?run \"$ZSH_DEBUG_CMD\"? " || setopt errexit
echo > /dev/tty
}
cmd1
cmd2
...
More portably:
run() {
printf %s "run $*? " > /dev/tty
read answer < /dev/tty
case $answer in
[yY]*) "$@";;
esac
}
run cmd1
run cmd2
run cmd3 > file
Beware that in cmd3 > file
, the file
will be truncated even if you say n
. So you may want to write it:
run eval 'cmd3 > file'
Or move the eval
to the run
function as in:
run() {
printf %s "run $*? " > /dev/tty
read answer < /dev/tty
case $answer in
[yY]*) eval "$@";;
esac
}
run cmd1
run 'cmd2 "$var"'
run 'cmd3 > file'
Another portable one, but with even more limitations:
xargs -pL1 env << 'EOF'
cmd1 "some arg"
cmd2 'other arg' arg\ 2
ENV_VAR=value cmd3
EOF
It only works for commands (ones found in $PATH
), and arguments can only be strings (no variable or any shell structure, though xargs understand some forms of quoting), and you can't have redirections, pipes...
There's a DEBUG trap that can be called before every command is run
eg
trap 'echo -e "\nStarted at: $(date)\n"' DEBUG
So if I do that:
$ trap 'echo -e "\nStarted at: $(date)\n"' DEBUG
$ pwd
Started at: Thu Aug 18 11:59:33 EDT 2016
/home/sweh
$ echo hello
Started at: Thu Aug 18 11:59:35 EDT 2016
hello
$ sleep 100
Started at: Thu Aug 18 11:59:37 EDT 2016
It's not rewriting the prompt, but you can see how it can be made to output stuff before every comamnd. You can make the trap function as complicated as you need.
Best Answer
Try this :
Check http://homepage3.nifty.com/tsato/xvkbd/