我正在制作一个向用户提供命令行的bash脚本.
cli代码是这样的:
#!/bin/bash
cmd1() {
echo $FUNCNAME: "$@"
}
cmd2() {
echo $FUNCNAME: "$@"
}
cmdN() {
echo $FUNCNAME: "$@"
}
__complete() {
echo $allowed_commands
}
shopt -qs extglob
fn_hide_prefix='__'
allowed_commands="$(declare -f | sed -ne '/^'$fn_hide_prefix'.* ()/!s/ ().*//p' | tr '\n' ' ')"
complete -D -W "this should output these words when you hit TAB"
echo "waiting for commands"
while read -ep"-> "; do
history -s $REPLY
case "$REPLY" in
@(${allowed_commands// /|})?(+([[:space:]])*)) $REPLY ;;
\?) __complete ;;
*) echo "invalid …Run Code Online (Sandbox Code Playgroud)