【发布时间】:2013-08-02 16:29:15
【问题描述】:
这个有点难以解释。考虑变量all、first、last 和some:
a="apple mcintosh"
b="banana plantain"
c="coconut cashew"
all="$a $b $c"
first="$a"
last=$c"
some="$a $c"
这是我所拥有的:
echo "What do you want to install?"
echo "all, first, last, or some?"
read userinput
假设用户键入all,他的输入将被视为一个变量的名称:我希望下一个命令是pacman -S $all(相当于pacman -S apple mcintosh banana plantain coconut cashew)。同样,如果用户同时键入first 和last,则下一个命令必须是pacman -S $first $last(实际上应该执行pacman -S apple mcintosh coconut cashew)。
我使用case/esac 将userinput 转换为变量,但我正在寻找一种更灵活、更优雅的解决方案,因为这种方法不允许多个输入。
case $userinput in
all) result="$all";;
first) result="$first";;
*) exit;;
esac
pacman -S $result
【问题讨论】:
标签: bash variables case indirection pacman-package-manager