天天看點

SHELL程式設計——選擇語句case和select一、選擇語句case二、選擇語句select

目錄

一、選擇語句case

1、文法

2、示例:選擇參數腳本

二、選擇語句select

1、文法

2、示例:選擇系統版本

一、選擇語句case

1、文法

case $arg in

      pattern1)

      語句1

      ;;

      pattern2)

      語句2

      ;;

      *)

      語句3

      ;;

esac

2、示例:選擇參數腳本

#!/bin/bash
case $1 in
   monitor_log)
   monitor_log
   ;;
   archive_log)
   archive_log
   ;;
   *)
   echo "Usage:{$0 monitor_log|archive_log|help}"
   ;;
esac
   
           

二、選擇語句select

1、文法

select一般用于選擇菜單的建立,可以配合PS3來做菜單的列印輸出資訊

select 變量 in 字元串

do 

     語句

done

2、示例:選擇系統版本

#!/bin/bash
PS3="What you like most of the open source system?"
select i in CentOS RedHat Ubuntu
do
   echo "Your Select System:" $i
done