標籤:linux shell getopts
getopts是bash shell的內建命令,作用是在shell指令碼中解析命令列傳遞、傳遞給函數或傳遞給另一個調用的shell指令碼的位置參數(選項或參數,後面會講解,getopts只支援短選項,若要解析長選項請參考getopt)。
getopts命令文法:
getopts optstring name [arg]
相關的術語:
選項(option):GNU風格的命令選項,如:-x,-y等減號加上單個字母的為短選項;--help為長選項;
選項的參數:某些選項之後必須尾隨參數,如:-f xxx.conf,xxx.conf稱為選項的參數;
參數:位於最後一個選項,空格之後的字串;
optionstring:用於匹配option的字串,optionstring中每個字母對應的是去除減號後的option(這裡指短選項);
[arg]:用來替代位置參數被getopts解析的,類似於將[email protected]賦值給arg;
OPTIND: getopts的一個內建變數,表示下一個要處理的參數索引,每次shell或shell指令碼執行的時候初始化為1;
name: getopts每解析到一個選項,都會將該選項對應的字元複製給name變數
getopts命令的簡單解釋:
1.optstring中如果字母后面跟隨著冒號,表示該字母所表示的option後面是帶參數的,參數值將被賦到OPTARG變數中。如test.sh中包含:getopts "i:" name; 那麼在命令列執行test.sh -i 1.1.1.1時,$name=‘i‘,$OPTARG=1.1.1.1
getopts包含兩種錯誤處理的模式
安靜模式下:
2.optstring的第一個字元為冒號時,getopts將使用安靜模式來報告錯誤(但不會有錯誤提示輸出),生產環境中一般使用該模式;
3.如果輸入一個無效的字元作為選項時(optstring中沒包含的字元),將‘?’賦值給name變數,該無效選項的字元賦值給OPTARG變數;
4.如果選項要求跟隨的參數沒找到,getopts會將冒號":"賦值給name變數,設定OPTARG為發現的選項字元;
非安靜模式下:
5.如果getopts不處於安靜模式,一個無效的選項被發現,‘?‘將被設定到name變數中且unset OPTARG
6.如果選項要求的參數沒找到,‘?‘將被設定到name,OPTARG將被unset,錯誤提示資訊將被列印
退出狀態:
7.如果shell中$OPTERR環境變數值為0,getopts將關閉錯誤資訊的列印,即使optstring中第一個字元不是冒號。預設的$OPTERR值為1
8.getopts一般情況下解析位置參數($0-$9),但位置參數超過這個範圍時仍然能被正確解析
9.如果選項被發現(匹配optionstring)返回true,如果遇到了選項的結尾或者有錯誤則返回false
指令碼執行個體:
1. 不帶選擇性參數[arg]
#!/bin/bash
echo "OPTIND starts at $OPTIND"
while getopts ":q:p" optname
do
case "$optname" in
"p")
echo "Option $optname is specified"
;;
"q")
echo "Option $optname has value $OPTARG"
#shift 1
shift $((OPTIND-2))
;;
"?")
echo "Unknown option $OPTARG"
;;
":")
echo "No argument value for option $OPTARG"
;;
*)
# Should not occur
echo "Unknown error while processing options"
;;
esac
echo "OPTIND is now $OPTIND"
done
執行:
[[email protected] ~]# bash test_getopts.sh -q aa -p -h
OPTIND starts at 1
Option q has value aa
OPTIND is now 3
Unknown option h
OPTIND is now 4
2. 帶選擇性參數[arg]
#!/bin/bash
echo "OPTIND starts at $OPTIND"
while getopts ":q:p" optname "-q qarg" "-p" "-h"
do
case "$optname" in
"p")
echo "Option $optname is specified"
;;
"q")
echo "Option $optname has value $OPTARG"
;;
"?")
echo "Unknown option $OPTARG"
;;
":")
echo "No argument value for option $OPTARG"
;;
*)
# Should not occur
echo "Unknown error while processing options"
;;
esac
echo "OPTIND is now $OPTIND"
done
執行:
[[email protected] ~]# bash test_getopts.sh
OPTIND starts at 1
Option q has value qarg
OPTIND is now 2
Option p is specified
OPTIND is now 3
Unknown option h
OPTIND is now 4
3. 實現一個添加yum源的指令碼
#!/bin/bash
#
# Descrition: this script will download the yum repo file from mirrors.163.com and epel for CENTOS/REDHAT DISTIBUTE
# Author: Gateray Jo
# mail: [email protected]
set -e
prog=`basename $0`
arch=`uname -m`
release=`lsb_release -r | sed ‘s/.*:[[:space:]]*//‘`
function usage() {
cat<<eof
Usage: ./${prog} [options] [arg]
-h: print the command help.
-u: specified the baseurl for yum repository, must use with -ufn combination.
-f: specified the name of local repofile(no ".repo" ext name) which place in /etc/yum.repos.d/, must use with -ufn
combination.
-n: specified the repo‘s name in xxx.repo file, must use with -ufn combination.
-d: specified the url of repofile which will be download to /etc/yum.repos.d/.
By default, nothing options are specified which will download epel and 163 yum repofile.
eof
}
function default() {
major_rel=${release%%.*}
# download 163 repofile
printf "Download 163 repofile from mirrors.163.com:\n"
wget -P /etc/yum.repos.d http://mirrors.163.com/.help/CentOS${major_rel}-Base-163.repo
printf "Download epel repofile from http://dl.fedoraproject.org.:\n"
if [[ $arch = "x86_64" ]]; then
[ ${major_rel} -eq 5 ] && rpm -ivh http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
[ ${major_rel} -eq 6 ] && rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
else
[ ${major_rel} -eq 5 ] && rpm -ivh http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
[ ${major_rel} -eq 6 ] && rpm -ivh http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
fi
return $?
}
[ $# -eq 0 ] && default && exit $?
while getopts ":hu:f:n:d:" optname; do
case $optname in
"h")
usage && exit 0
;;
"u")
u_opt=1
u_arg=$OPTARG
;;
"f")
f_opt=1
u_arg=$OPTARG
;;
"n")
n_opt=1
n_arg=$OPTARG
;;
"d")
wget -P /etc/yum.repos.d $OPTARG
exit $?
;;
"?")
echo "Unknown option $OPTARG"
usage && exit 1
;;
":")
echo "It is need a option value for $OPTARG"
usage && exit 1
;;
*)
echo ‘Unknown error!‘ && exit 1
;;
esac
done
if [ $u_opt -eq 1 -a $f_opt -eq 1 -a $n_opt -eq 1 ]; then
if [[ ${n_arg} =~ ‘-‘ ]]; then
echo "Invalid value for -n option, \"${n_arg}\" is include ‘-‘ character."
exit 1
fi
cat >> /etc/yum.repos.d/${f_arg}.repo <<eof
[${n_arg}]
name=${n_arg}
baseurl=${u_arg}
enabled=1
gpgcheck=0
eof
else
usage && exit 1
fi
指令碼的執行:
1)列印命令協助
[[email protected] ~]# ./load_yum_repo.sh -h
Usage: ./load_yum_repo.sh [options] [arg]
-h: print the command help.
-u: specified the baseurl for yum repository, must use with -ufn combination.
-f: specified the name of local repofile(no ".repo" ext name) which place in /etc/yum.repos.d/, must use with -ufn
combination.
-n: specified the repo‘s name in xxx.repo file, must use with -ufn combination.
-d: specified the url of repofile which will be download to /etc/yum.repos.d/.
By default, nothing options are specified which will download epel and 163 yum repofile.
2)不帶任何選項參數時,自動下載163和epel的yum源repofile
[[email protected] ~]# ./load_yum_repo.sh
Download 163 repofile from mirrors.163.com:
--2015-04-04 22:23:05-- http://mirrors.163.com/.help/CentOS6-Base-163.repo
Resolving mirrors.163.com... 123.58.173.106
Connecting to mirrors.163.com|123.58.173.106|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2006 (2.0K) [application/octet-stream]
Saving to: “/etc/yum.repos.d/CentOS6-Base-163.repo”
100%[==========================================================================================>] 2,006 --.-K/s in 0.05s
2015-04-04 22:23:05 (39.2 KB/s) - “/etc/yum.repos.d/CentOS6-Base-163.repo” saved [2006/2006]
Download epel repofile from http://dl.fedoraproject.org.:
Retrieving http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
warning: /var/tmp/rpm-tmp.wVrCDS: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:epel-release ########################################### [100%]
3)從指定的url中下載repofile
[[email protected] ~]# ./load_yum_repo.sh -d http://mirrors.163.com/.help/CentOS6-Base-163.repo
--2015-04-04 22:13:47-- http://mirrors.163.com/.help/CentOS6-Base-163.repo
Resolving mirrors.163.com... 123.58.173.106
Connecting to mirrors.163.com|123.58.173.106|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2006 (2.0K) [application/octet-stream]
Saving to: “/etc/yum.repos.d/CentOS6-Base-163.repo”
100%[==========================================================================================>] 2,006 --.-K/s in 0.05s
2015-04-04 22:13:47 (40.9 KB/s) - “/etc/yum.repos.d/CentOS6-Base-163.repo” saved [2006/2006]
4)建立自訂的repofile
[[email protected] ~]# ./load_yum_repo.sh -f test -n test -u file:///mnt
[[email protected] ~]# cat /etc/yum.repos.d/test.repo
[test]
name=test
baseurl=file:///mnt
enabled=1
gpgcheck=0
Linux shell中getopts命令學習--實現一個添加yum源的指令碼