Linux shell脚本编程提供了select in语句,这是 Shell 独有的一种循环语句,非常适合终端(Terminal)这样的交互场景,它可以根据用户的设置显示出带编号的菜单,用户通过输入不同的编号就可以选择不同的菜单,并执行与菜单对应的功能,这是C、C++、Java、Python 等编程语言中是没有的。
select 变量名 in 值1 [值2 ……值n]
do
语句或命令1
[……]
[语句或命令n]
done
在do 和 done之间,我们可以用if或case语句根据变量名的值执行相应语句或命令,实现不同的功能。
我们会询问你喜欢吃哪种水果,并显示4个水果选项:apple、banana、orrange、peach,以及一个退出循环的选项exit
如果你的选择4个水果中的一个,将会显示你选择的水果名称
如果你选择了是exit,将会看到提示bye,并结束循环。
代码如下:
echo "What is your favourite fruit?"
select f in "apple" "banana" "orrange" "peach" "exit"
do
if [[ $f == "exit" ]]; then
echo "bye"
break
else
echo "You have selected $f"
fi
done
# csdn @ edu in ~ [22:32:22]
$ echo "What is your favourite fruit?"; select f in "apple" "banana" "orrange" "peach" "exit"; do; if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) apple 2) banana 3) orrange 4) peach 5) exit
?# 1
You have selected apple
?# 4
You have selected peach
?# 5
bye# csdn @ edu in ~ [22:32:52]
我们可以看到zsh 根据select in循环语句提供的值显示出了五个菜单项:
1) apple 2) banana 3) orrange 4) peach 5) exit
每个菜单项前有1个数字。
然后显示提示符 ?# 让我们输入菜单项前的数字来选择相应的菜单项。
我们先输入了1,所以显示You have selected apple
接着我们输入4,所以显示You have selected peach
最后我们输入5,这是退出循环的选项,于是我们看到了bye,并退回到了命令行状态。
# csdn @ edu in ~ [22:46:42]
$ bash
[csdn ~]$ echo "What is your favourite fruit?"; select f in "apple" "banana" "orrange" "peach" "exit"; do; if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
bash: syntax error near unexpected token `;'
[csdn ~]$ echo "What is your favourite fruit?"; select f in "apple" "banana" "orrange" "peach" "exit"; do if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) apple
2) banana
3) orrange
4) peach
5) exit
#? 1
You have selected apple
#? 3
You have selected orrange
#? 5
bye
[csdn ~]$
我们可以看到bash 和zsh 对脚本的支持不太一样。
刚才在zsh中执行正常的脚本,在bash中执行时出错:
bash: syntax error near unexpected token `;'
我们把 do 语句后面的; 去掉后,脚本可以执行了。
根据select in循环语句提供的值显示出了五个菜单项:
我们可以看到bash 像zsh 一样,根据select in循环语句提供的值显示出了五个菜单项:
1) apple
2) banana
3) orrange
4) peach
5) exit
每个菜单项前有1个数字。
但与zsh的横向显示方式不同,bash采用的是纵向显示方式。
然后显示提示符#? 让我们输入菜单项前的数字来选择相应的菜单项。
bash的这个提示符#? 跟zsh显示的提示符字符顺序是相反的,zsh显示的提示符字符是?#。
我们先输入了1,所以显示You have selected apple
接着我们输入3,所以显示You have selected orrange
最后我们输入5,这是退出循环的选项,于是我们看到了bye,并退回到了命令行状态。
在上面的实例1中,如果我们输入的字符不属于菜单项前的数字,代码会怎么执行呢?
# csdn @ edu in ~ [23:03:43]
$ echo "What is your favourite fruit?"; select f in "apple" "banana" "orrange" "peach" "exit"; do; if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) apple 2) banana 3) orrange 4) peach 5) exit
?# a
You have selected
?# 6
You have selected
?# 3
You have selected orrange
?# 5
bye# csdn @ edu in ~ [23:04:12]
$
# csdn @ edu in ~ [23:04:12]
$ exec bash
[csdn ~]$ echo "What is your favourite fruit?"; select f in "apple" "banana" "orrange" "peach" "exit"; do; if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
bash: syntax error near unexpected token `;'
[csdn ~]$ echo "What is your favourite fruit?"; select f in "apple" "banana" "orrange" "peach" "exit"; do if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) apple
2) banana
3) orrange
4) peach
5) exit
#? b
You have selected
#? 9
You have selected
#? 3
You have selected orrange
#? 5
bye
[csdn ~]$
可以看到,不管zsh还是bash,如果我们输入的字符不属于菜单项前的数字,shell并不会进行干预,代码仍然会执行,并由if语句根据$f的值进行处理。
所以为了实现预期的效果,我们需要自己对用户输入的字符的有效性进行检查和处理。
我们通过下面的代码,给select in 传递了36个值来测试。
# csdn @ edu in ~ [23:14:29]
$ echo "What is your favourite fruit?"; select f in 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z exit; do; if [[ $f == "exit" ]]; then echo "bye"; break; else echo "
You have selected $f"; fi; done
What is your favourite fruit?
1) 1 3) 3 5) 5 7) 7 9) 9 11) b 13) d 15) f 17) h 19) j 21) l 23) n 25) p 27) r 29) t 31) v 33) x 35) z
2) 2 4) 4 6) 6 8) 8 10) a 12) c 14) e 16) g 18) i 20) k 22) m 24) o 26) q 28) s 30) u 32) w 34) y 36) exit
?# 1
You have selected 1
?# 18
You have selected i
?# m
You have selected
?# 36
bye# csdn @ edu in ~ [23:15:12]
$
这次在zsh也是采用纵向多列的方式来显示菜单。
# csdn @ edu in ~ [23:15:12]
$ exec bash
[csdn ~]$ echo "What is your favourite fruit?"; select f in 1 2 3 4 5 6 7 8 9 a b c d e f g h i j k l m n o p q r s t u v w x y z exit; do if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) 1 3) 3 5) 5 7) 7 9) 9 11) b 13) d 15) f 17) h 19) j 21) l 23) n 25) p 27) r 29) t 31) v 33) x 35) z
2) 2 4) 4 6) 6 8) 8 10) a 12) c 14) e 16) g 18) i 20) k 22) m 24) o 26) q 28) s 30) u 32) w 34) y 36) exit
#? 9
You have selected 9
#? 20
You have selected k
#? k
You have selected
#? 36
bye
[csdn ~]$
这次bash显示菜单的方式与zsh一致。
可以看到,当传递给 select in 的值超过9个时,不管zsh还是bash会以10、11……这种方式给值赋予菜单值。
我们可以先将要传递给select in 的值定义并存储到一个数组a中,
再把数组a传递给 select in,
当用户输入数值后,对用户输入的数值进行判断:
# csdn @ edu in ~ [23:48:42]
$ a=(apple banana orrange peach);echo "What is your favourite fruit?"; select f in $a; do; if [[ $f -gt ${#a[*]} ]] || [[ $f < 1 ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) apple 2) banana 3) orrange 4) peach
?# 1
You have selected apple
?# 4
You have selected peach
?# 0
bye# csdn @ edu in ~ [23:48:52]
$
对于 if [[ $f -gt ${#a[*]} ]] || [[ $f < 1 ]]
$f > ${#a[*]}
$ a=(apple banana orrange peach);echo "What is your favourite fruit?"; select f in $a; do; if [[ $f > ${#a[*]} ]] || [[ $f < 1 ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) apple 2) banana 3) orrange 4) peach
?# 1
bye# csdn @ edu in ~ [23:13:38]
$
即使我们输入1,代码也会显示bye并跳出循环返回命令行。
这是因为$f 和 ${#a[*]} 在这里进行的是字符串比较。
# csdn @ edu in ~ [23:48:31]
$ a=(apple banana orrange peach);echo "What is your favourite fruit?"; select f in $a; do; if [[ $f -gt ${#a[*]} ]] || [[ $f -lt 1 ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) apple 2) banana 3) orrange 4) peach
?# 1
bye
同样,我们输入1,代码也会显示bye并跳出循环返回命令行。
这是因为$f 和1在进行的是数值比较。
在上面的实例1和实例2中,我们都设置了exit值给用户选择退出循环,如果我们在脚本中没有作这方面的设置,用户该如何退出循环呢?
有两种方法:
Ctrl+C
或
Ctrl+D
# csdn @ edu in ~ [23:26:14]
$ exec bash
[csdn ~]$ echo "What is your favourite fruit?"; select f in "apple" "banana" "orrange" "peach" "exit"; do; if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
bash: syntax error near unexpected token `;'
[csdn ~]$ echo "What is your favourite fruit?"; select f in "apple" "banana" "orrange" "peach" "exit"; do if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) apple
2) banana
3) orrange
4) peach
5) exit
#? 3
You have selected orrange
#? ^C
按Ctrl+C退出,屏幕上会显示^C。
(二)以bash中用Ctrl+D
[csdn ~]$ echo "What is your favourite fruit?"; select f in "apple" "banana" "orrange" "peach" "exit"; do if [[ $f == "exit" ]]; then echo "bye"; break; else echo "You have selected $f"; fi; done
What is your favourite fruit?
1) apple
2) banana
3) orrange
4) peach
5) exit
#? 1
You have selected apple
#?
[csdn ~]$
按Ctrl+D退出,屏幕上不会显示出来,更美观一些。
当我们需要对多个数值分别做不同的处置时,用if语句太麻烦了,我们可以使用case...esca语句配合使用。
我们将实例1修改如下:
a=(apple banana orrange peach)
echo "What is your favourite fruit?"
select f in ${a[*]}
do
case $f in
apple) echo 1 apple;;
banana) echo 2 banana;;
orrange) echo 3 orrange;;
peach) echo 4 peach;;
*) echo bye
break;;
esac
done
使用case...esca语句来对用户选择的fruit进行分别处理,如果用户输入的值不在数组a中,将显示bye并退出循环。
# csdn @ edu in ~ [22:37:41]
$ a=(apple banana orrange peach);echo "What is your favourite fruit?"; select f in $a; do; case $f in; apple) echo 1 apple;; banana) echo 2 banana;; orrange)
echo 3 orrange;;peach) echo 4 peach;; *)echo bye; break; esac; done
What is your favourite fruit?
1) apple 2) banana 3) orrange 4) peach
?# 1
1 apple
?# 4
4 peach
?# 5
bye# csdn @ edu in ~ [22:37:53]
$
[csdn ~]$ a=(apple banana orrange peach);echo "What is your favourite fruit?"; select f in $a; do; case $f in; apple) echo 1 apple;; banana) echo 2 banana;; orrange) echo 3 orrange;;peach) echo 4 peach;; *)echo bye; break; esac; done
bash: syntax error near unexpected token `;'
[csdn ~]$ a=(apple banana orrange peach);echo "What is your favourite fruit?"; select f in $a; do case $f in; apple) echo 1 apple;; banana) echo 2 banana;; orrange) echo 3 orrange;;peach) echo 4 peach;; *)echo bye; break; esac; done
bash: syntax error near unexpected token `;'
[csdn ~]$
bash对;的处理
[csdn ~]$ echo 'a=(apple banana orrange peach)' > a.sh
[csdn ~]$ echo 'echo "What is your favourite fruit?"' >> a.sh
[csdn ~]$ echo 'select f in ${a[*]}' >> a.sh
[csdn ~]$ echo do >> a.sh
[csdn ~]$ echo 'case $f in' >> a.sh
[csdn ~]$ echo 'apple) echo 1 apple;;' >> a.sh
[csdn ~]$ echo 'banana) echo 2 banana;;' >> a.sh
[csdn ~]$ echo 'orrange) echo 3 orrange;;' >> a.sh
[csdn ~]$ echo 'peach) echo 4 peach;;' >> a.sh
[csdn ~]$ echo '*) echo bye' >> a.sh
[csdn ~]$ echo 'break;;' >> a.sh
[csdn ~]$ echo esac >> a.sh
[csdn ~]$ echo done >> a.sh
[csdn ~]$
[csdn ~]$ cat a.sh
a=(apple banana orrange peach)
echo "What is your favourite fruit?"
select f in ${a[*]}
do
case $f in
apple) echo 1 apple;;
banana) echo 2 banana;;
orrange) echo 3 orrange;;
peach) echo 4 peach;;
*) echo bye
break;;
esac
done
[csdn ~]$
[csdn ~]$ . a.sh
What is your favourite fruit?
1) apple
2) banana
3) orrange
4) peach
#? 1
1 apple
#? 3
3 orrange
#? 5
bye
[csdn ~]$
注意:
对于数组a=(apple banana orrange peach)'
- 在zsh中,只需要使用$a 就可以获得所有元素的值
- 在bash中,使用$a 只能获得第1个元素的值,要获得所有元素的值,需要使用${a[*]}或${a[@]}