合并bash代码块以提供选择

合并bash代码块以提供选择,bash,Bash,我想合并两块bash代码。首先检查您是否已安装“get-iplayer/get_-iplayer”,如果未安装,则提示安装- if [[ -x "/usr/bin/get-iplayer" ]] then player="/usr/bin/get-iplayer" elif [[ -x "/usr/bin/get_iplayer" ]] then player="/usr/bin/get_iplayer" elif [[ -x "/usr/local/bin/get_iplayer" ]] t

我想合并两块bash代码。首先检查您是否已安装“get-iplayer/get_-iplayer”,如果未安装,则提示安装-

if [[ -x "/usr/bin/get-iplayer" ]]
then player="/usr/bin/get-iplayer"
elif [[ -x "/usr/bin/get_iplayer" ]]
 then player="/usr/bin/get_iplayer"
elif [[ -x "/usr/local/bin/get_iplayer" ]]
then player="/usr/local/bin/get_iplayer"
else echo "$0: Error: 'get-iplayer' or 'get_iplayer' is not installed. Please install it." >&2
 exit 1
fi
现在我想添加一个功能来选择使用哪一个,实际上我已经安装了所有3个,但是我想使用/usr/local/bin中的一个,就像这样-

{
    read -n1 -p "$(tput setaf 5)
            get-iplayer = a,
            get_iplayer = b,
            new get_iplayer = c,
            quit = q? [a/b/c/q] " abcq
    echo; echo "$(date +%Y-%m-%d\ %H:%M:%S) Answer: $abcq" >> $log
   case "$abcq" in
        [a]* ) /usr/bin/get-iplayer & echo;;
        [b]* ) /usr/bin/get_iplayer & echo;;
        [c]* ) /usr/local/bin/get_iplayer & echo;;
            [q]* ) echo; exit;;
        * )
     esac
 }
但是我怎么做呢?我一直坐在这里想弄明白,但一事无成


您必须能够看到所选择的内容,最终必须称之为“player”,因为这是脚本其余部分中的变量名

使用数组保存现有可执行文件,然后使用很少使用的
select
命令选择:

iplayers=()
for possible in /usr/bin/get-iplayer /usr/bin/get_iplayer /usr/local/bin/get_iplayer; do
    [[ -x $possible ]] && iplayers+=("$possible")
done
if (( ${#iplayers[@]} == 0 )); then
    echo "$0: Error: 'get-iplayer' or 'get_iplayer' is not installed. Please install it." >&2
    exit 1
fi

PS3="Select an iplayer: "
select choice in "${iplayers[@]}"; do
    [[ -n $choice ]] && break
done

echo "you chose: $choice"