我有两个要合并的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“,因为这是脚本其余部分中的变量名。
发布于 2014-04-20 19:36:06
使用数组保存现有的可执行文件,然后使用很少使用的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"https://stackoverflow.com/questions/23181192
复制相似问题