編寫可接受選項和參數的shell腳本
處理簡單選項
假設腳本名為
test.sh
,選項例如
-a -b -c
的形式。
#!/bin/bash
# extracting command line options as parameters
#
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option" ;;
-c) echo "Found the -c option" ;;
*) echo "$1 is not an option" ;;
esac
shift
done
運作:
輸出結果:
Found the -a option
Found the -b option
Found the -c option
-d is not an option
分離參數和選項
假如選項與參數例如
-a -b -c -- test1 test2 test3
的形式。
#!/bin/bash
# extracting options and parameters
echo
# 識别選項
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option";;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
# 分離參數
count=
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + ]
done
運作:
./test.sh -a -b -c -- test1 test2 test3
輸出結果:
Found the -c option
Found the -a option
Found the -b option
Parameter #1: test1
Parameter #2: test2
Parameter #3: test3
處理帶值的選項
#!/bin/bash
# extracting command line options and values
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option";;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-c) echo "Found the -c option";;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count + ]
done
運作:
輸出結果:
Found the -a option
Found the -b option, with parameter value test1
-d is not an option
處理複雜的選項
getopt
指令可以接受一系列任意形式的指令行選項和參數,并自動将它們轉換成适當的格式。它的指令格式如下:
getopt optstring parameters
其中
optstring
定義了指令行有效的選項字母,還定義了哪些選項字母需要參數值。首先,在
optstring
中列出你要在腳本中用到的每個指令行選項字母。然後,在每個需要參數值的選項字母後加一個冒号。
getopt
指令會基于你定義的
optstring
解析提供的
parameters
。
getopt ab:cd -a -b test1 -cd test2 test3
# 輸出
# -a -b test1 -c -d -- test2 test3
#!/bin/bash
# Extract command line options & values with getopt
# 使用getopt解析複雜選項,-q屏蔽錯誤選項
# set指令的選項之一是雙破折線(--),它會将指令行參數替換成set指令的指令行值
set -- $(getopt -q ab:cd "$@")
echo
while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) param="$2"
echo "Found the -b option, with parameter value $param"
shift ;;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done
#
count=
for param in "$@"
do
echo "Parameter #$count: $param"
count=$[ $count + ]
done
./test.sh -ac
Found the -a option
Found the -c option
注意:getopt指令并不擅長處理帶空格和引号的參數值。它會将空格當作參數分隔符,而不是根據雙引号将二者當作一個參數。
./test.sh -a -b test1 -cd "test2 test3" test4
Found the -a option
Found the -b option, with parameter value 'test1'
Found the -c option
Parameter #1: 'test2
Parameter #2: test3'
Parameter #3: 'test4'
getopts
指令與
getopt
不同,前者将指令行上選項和參數處理後隻生成一個輸出,而
getopts
指令能夠和已有的shell參數變量配合默契。
getopts optstring variable
optstring
值類似于
getopt
指令中的那個。有效的選項字母都會列在
optstring
中,可以在參數值中包含空格。如果選項字母要求有個參數值,就加一個冒号。要去掉錯誤消息的話,可以在
optstring
之前加一個冒号。
getopts
指令将目前參數儲存在指令行中定義的
variable
中。
#!/bin/bash
# simple demonstration of the getopts command
#
echo
while getopts :ab:c opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG";;
c) echo "Found the -c option" ;;
*) echo "Unknown option: $opt";;
esac
done
運作:
./test.sh -ab test1 -c
結果輸出:
Found the -a option
Found the -b option, with value test1
Found the -c option
另一個好用的功能是将選項字母和參數值放在一起使用,而不用加空格。
./test.sh -abtest1
Found the -a option
Found the -b option, with value test1
完整例子
#!/bin/bash
# Processing options & parameters with getopts
#
echo
while getopts :ab:cd opt
do
case "$opt" in
a) echo "Found the -a option" ;;
b) echo "Found the -b option, with value $OPTARG" ;;
c) echo "Found the -c option" ;;
d) echo "Found the -d option" ;;
*) echo "Unknown option: $opt" ;;
esac
done
#
shift $[ $OPTIND - ]
#
echo
count=
for param in "$@"
do
echo "Parameter $count: $param"
count=$[ $count + ]
done
OPTIND
環境變量儲存了參數清單中
getopts
正在處理的參數位置。這樣你就能在處理完選項之後繼續處理其他指令行參數了
常用的Linux指令選項規範
選 項 | 描 述 |
---|---|
-a | 顯示所有對象 |
-c | 生成一個計數 |
-d | 指定一個目錄 |
-e | 擴充一個對象 |
-f | 指定讀入資料的檔案 |
-h | 顯示指令的幫助資訊 |
-i | 忽略文本大小寫 |
-l | 産生輸出的長格式版本 |
-n | 使用非互動模式(批處理) |
-o | 将所有輸出重定向到的指定的輸出檔案 |
-q | 以安靜模式運作 |
-r | 遞歸地處理目錄和檔案 |
-s | 以安靜模式運作 |
-v | 生成詳細輸出 |
-x | 排除某個對象 |
-y | 對所有問題回答yes |