Bash
getopts:スマート位置パラメータ解析
サーチ…
構文
- getopts optstring name [args]
パラメーター
| パラメータ | 詳細 |
|---|---|
| オプトストリング | 認識されるオプション文字 |
| 名 | 次に、解析されたオプションが格納されている名前を指定します |
備考
オプション
optstring:認識されるオプション文字文字の後にコロンが続く場合、オプションには引数が必要です。引数は空白で区切る必要があります。コロン(
:)(と疑問符?)オプション文字として使用することはできません。
呼び出されるたびに、 getoptsはシェル変数名に次のオプションを、存在しない場合は名前を初期化し、変数OPTIND処理される次の引数のインデックスをOPTINDます。 OPTINDは、シェルまたはシェルスクリプトが呼び出されるたびに1初期化されます。
オプションに引数が必要な場合、 getoptsはその引数を変数OPTARGます。シェルは自動的にOPTINDリセットしません。新しいパラメータセットを使用する場合は、同じシェル呼び出し内でgetopts複数回呼び出す間に手動でリセットする必要があります。
オプションの終わりに遭遇すると、 getoptsは0より大きい戻り値で終了します。
OPTINDは最初の非オプション引数のインデックスに設定され、nameは?設定され? 。 getoptsは通常、位置パラメータを解析しますが、 argsでより多くの引数が指定された場合、 getoptsはそれらを代わりに解析します。
getoptsは2つの方法でエラーを報告できます。最初の文字場合optstring (コロンです: )、サイレントエラー報告が使用されています。通常の操作では、無効なオプションまたは欠落しているオプション引数が見つかった場合に診断メッセージが出力されます。
変数OPTERRが0に設定されている場合、 optstringの最初の文字がoptstringでない場合でもエラーメッセージは表示されません。
無効なオプションがgetopts場合は、 getoptsます? nameと、サイレントでない場合は、エラーメッセージを表示し、設定解除しOPTARG 。 getoptsがサイレントの場合、見つかったオプション文字はOPTARG置かれ、診断メッセージは出力されません。
必要な引数が見つからず、 getoptsがサイレントでない場合、疑問符( ? )がname 、 OPTARGは設定されず、診断メッセージが出力されます。場合getopts沈黙している、そして、コロン( : )名に置かれ、 OPTARGオプション文字に設定されています。
ピンナップ
#!/bin/bash
# Script name : pingnmap
# Scenario : The systems admin in company X is tired of the monotonous job
# of pinging and nmapping, so he decided to simplify the job using a script.
# The tasks he wish to achieve is
# 1. Ping - with a max count of 5 -the given IP address/domain. AND/OR
# 2. Check if a particular port is open with a given IP address/domain.
# And getopts is for her rescue.
# A brief overview of the options
# n : meant for nmap
# t : meant for ping
# i : The option to enter the IP address
# p : The option to enter the port
# v : The option to get the script version
while getopts ':nti:p:v' opt
#putting : in the beginnnig suppresses the errors for invalid options
do
case "$opt" in
'i')ip="${OPTARG}"
;;
'p')port="${OPTARG}"
;;
'n')nmap_yes=1;
;;
't')ping_yes=1;
;;
'v')echo "pingnmap version 1.0.0"
;;
*) echo "Invalid option $opt"
echo "Usage : "
echo "pingmap -[n|t[i|p]|v]"
;;
esac
done
if [ ! -z "$nmap_yes" ] && [ "$nmap_yes" -eq "1" ]
then
if [ ! -z "$ip" ] && [ ! -z "$port" ]
then
nmap -p "$port" "$ip"
fi
fi
if [ ! -z "$ping_yes" ] && [ "$ping_yes" -eq "1" ]
then
if [ ! -z "$ip" ]
then
ping -c 5 "$ip"
fi
fi
shift $(( OPTIND - 1 )) # Processing additional arguments
if [ ! -z "$@" ]
then
echo "Bogus arguments at the end : $@"
fi
出力
$ ./pingnmap -nt -i google.com -p 80
Starting Nmap 6.40 ( http://nmap.org ) at 2016-07-23 14:31 IST
Nmap scan report for google.com (216.58.197.78)
Host is up (0.034s latency).
rDNS record for 216.58.197.78: maa03s21-in-f14.1e100.net
PORT STATE SERVICE
80/tcp open http
Nmap done: 1 IP address (1 host up) scanned in 0.22 seconds
PING google.com (216.58.197.78) 56(84) bytes of data.
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=1 ttl=57 time=29.3 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=2 ttl=57 time=30.9 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=3 ttl=57 time=34.7 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=4 ttl=57 time=39.6 ms
64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=5 ttl=57 time=32.7 ms
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4007ms
rtt min/avg/max/mdev = 29.342/33.481/39.631/3.576 ms
$ ./pingnmap -v
pingnmap version 1.0.0
$ ./pingnmap -h
Invalid option ?
Usage :
pingmap -[n|t[i|p]|v]
$ ./pingnmap -v
pingnmap version 1.0.0
$ ./pingnmap -h
Invalid option ?
Usage :
pingmap -[n|t[i|p]|v]