(一)常见使用例子
#!/bin/bash
PS3='Choose your favorite vegetable: ' # 设置提示符字串.
#+如果不是PS3则不会显示提示符直接显示 "#?"
echo
select vegetable in "beans" "carrots" "potatoes" "onions" "rutabagas"
do
echo
echo "Your favorite veggie is $vegetable."
echo "Yuck!"
echo
break # 如果这里没有 'break' 会发生什么?
#+ 若没有了break会循环显示提示符PS3并选择
done
exit 0
执行效果:
simon@kami:~/12.12.2015$ ./10-29.sh
1) beans #自动显示 1) 2) 3) 4) 5)
2) carrots
3) potatoes
4) onions
5) rutabagas
Choose your favorite vegetable: 1
#若没有设置PS3提示符效果如下
# #?
Your favorite veggie is beans.
Yuck!
(二)没有 in list #从命令行参数读取
#!/bin/bash
PS3='Choose your favorite vegetable: '
echo
choice_of()
{
select vegetable
# [in list] 被忽略, 所以'select' 使用传递给函数的参数.
do
echo
echo "Your favorite veggie is $vegetable."
echo "Yuck!"
echo
break
done
}
choice_of beans rice carrots radishes tomatoes spinach
# $1 $2 $3 $4 $5 $6
# 传递给choice_of() 的参数
# 若choice_of 后直接写"$@"则从命令行参数读入
#+ choice_of "$@"
exit 0
阅读(965) | 评论(0) | 转发(0) |