在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Linux/ Shell select 循環(huán)
Shell 輸入/輸出重定向
Shell 循環(huán)類型
Shell是什么?
Shell 特殊變量
Shell 算術(shù)運(yùn)算符示例
Shell 關(guān)系運(yùn)算符示例
Shell 替代
Shell 函數(shù)
Shell 條件語句
Shell 聯(lián)機(jī)幫助
Shell 數(shù)組/Arrays
Shell 布爾運(yùn)算符范例
Shell
Shell if...elif...fi 語句
Shell case...esac 語句
Shell 使用Shell變量
Shell 文件測(cè)試符例子
Shell 基本運(yùn)算符
Korn Shell 運(yùn)算符
Shell 字符串運(yùn)算范例
Shell while 循環(huán)
Shell 引用機(jī)制
Shell if...else...fi 語句
Shell select 循環(huán)
C Shell運(yùn)算符
Shell 循環(huán)控制break/continue
Shell for循環(huán)
Shell until 循環(huán)
Shell if...fi語句

Shell select 循環(huán)

select 循環(huán)提供了一個(gè)簡(jiǎn)單的方法來創(chuàng)建一個(gè)編號(hào)的菜單,用戶可從中選擇。它是有用的,當(dāng)你需要從列表中選擇,要求用戶選擇一個(gè)或多個(gè)項(xiàng)目。

語法

select var in word1 word2 ... wordN
do
   Statement(s) to be executed for every word.
done

var是一個(gè)變量,word1 到 wordN是由空格分隔的字符(字)序列的名稱。每次for循環(huán)的執(zhí)行,變量var的值被設(shè)置為下一個(gè)單詞的列表中的字,由 word1 到wordN。

對(duì)于每一個(gè)選擇的一組命令將被執(zhí)行,在循環(huán)中。這個(gè)循環(huán)在ksh,并已被改編成的bash。這不是在sh。

例子:

下面是一個(gè)簡(jiǎn)單的例子,讓用戶選擇的首選飲品:

#!/bin/ksh

select DRINK in tea cofee water juice appe all none
do
   case $DRINK in
      tea|cofee|water|all) 
         echo "Go to canteen"
         ;;
      juice|appe)
         echo "Available at home"
      ;;
      none) 
         break 
      ;;
      *) echo "ERROR: Invalid selection" 
      ;;
   esac
done

select 循環(huán)的菜單看起來像下面這樣:

$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
#? juice
Available at home
#? none
$

您可以更改顯示的提示選擇循環(huán)通過改變變量PS3如下:

$PS3="Please make a selection => " ; export PS3
$./test.sh
1) tea
2) cofee
3) water
4) juice
5) appe
6) all
7) none
Please make a selection => juice
Available at home
Please make a selection => none
$