准备两个文件:
File1: env.bash
_buildsh_prompt()
{
COMPREPLY=()
local cur=${COMP_WORDS[COMP_CWORD]};
local cmd=${COMP_WORDS[COMP_CWORD-1]};
local opts=(-a -b -c all clean)
local a_opts=(a_1 a_2 a_3)
local b_opts=(b_1 b_2 b_3)
local c_opts=(C1 C2 C3 C4)
local module_opts=(all all_32 clean)
local path=$(pwd)
local SRC_BUILD_DIR=$(pwd)
case $cmd in
-a)
COMPREPLY=( $(compgen -W '${a_opts[@]}' -- $cur) )
;;
-b)
COMPREPLY=( $(compgen -W '${b_opts[@]}' -- $cur) )
;;
-c)
COMPREPLY=( $(compgen -W '${c_opts[@]}' -- $cur) )
;;
*)
COMPREPLY=( $(compgen -W '${opts[@]}' -- $cur) )
;;
esac
}
complete -F _buildsh_prompt build_test.sh
File2: build_test.sh
while getopts "a:b:c:" opt
do
case $opt in
a)
if [ "$OPTARG" = "a_1" ];then
echo "get in a_1 function"
elif [ "$OPTARG" = "a_2" ];then
echo "get in a_2 function"
elif [ "$OPTARG" = "a_3" ];then
echo "get in a_3 function"
else
echo "Error: Invalid -c $OPTARG"
exit 1
fi
;;
b)
if [ "$OPTARG" = "b_1" ];then
echo "get in b_1 function"
elif [ "$OPTARG" = "b_2" ];then
echo "get in b_2 function"
elif [ "$OPTARG" = "b_3" ];then
echo "get in b_3 function"
else
echo "Error: Invalid -b $OPTARG"
exit 1
fi
;;
c)
if [ "$OPTARG" = "C1" ];then
echo "get in C1 function"
elif [ "$OPTARG" = "C2" ];then
echo "get in C2 function"
elif [ "$OPTARG" = "C3" ];then
echo "get in C3 function"
else
echo "Error: Invalid -b $OPTARG"
exit 1
fi
;;
\?)
exit 1
;;
esac
done
开始测试
- source env.bash
- ./build_test.sh [tab]
- ./build_test.sh -a [tab]