getopts : smart positional-parameter parsing
pingnmap
Section titled “pingnmap”#!/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 optionsdocase "$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]" ;;esacdoneif [ ! -z "$nmap_yes" ] && [ "$nmap_yes" -eq "1" ]then if [ ! -z "$ip" ] && [ ! -z "$port" ] then nmap -p "$port" "$ip" fifi
if [ ! -z "$ping_yes" ] && [ "$ping_yes" -eq "1" ]then if [ ! -z "$ip" ] then ping -c 5 "$ip" fifishift $(( OPTIND - 1 )) # Processing additional argumentsif [ ! -z "$@" ]then echo "Bogus arguments at the end : $@"fiOutput
$ ./pingnmap -nt -i google.com -p 80
Starting Nmap 6.40 ( http://nmap.org ) at 2016-07-23 14:31 ISTNmap 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.netPORT STATE SERVICE80/tcp open http
Nmap done: 1 IP address (1 host up) scanned in 0.22 secondsPING 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 ms64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=2 ttl=57 time=30.9 ms64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=3 ttl=57 time=34.7 ms64 bytes from maa03s21-in-f14.1e100.net (216.58.197.78): icmp_seq=4 ttl=57 time=39.6 ms64 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 4007msrtt min/avg/max/mdev = 29.342/33.481/39.631/3.576 ms$ ./pingnmap -vpingnmap version 1.0.0$ ./pingnmap -hInvalid option ?Usage :pingmap -[n|t[i|p]|v]$ ./pingnmap -vpingnmap version 1.0.0$ ./pingnmap -hInvalid option ?Usage :pingmap -[n|t[i|p]|v]Syntax
Section titled “Syntax”- getopts optstring name [args]
Parameters
Section titled “Parameters”| Parameter | Detail |
|---|---|
| optstring | The option characters to be recognized |
| name | Then name where parsed option is stored |
Remarks
Section titled “Remarks”Options
Section titled “Options”
optstring: The option characters to be recognized
If a character is followed by a colon, the option is expected to have an argument, which should be separated from it by white space. The colon (`:`) and question mark (`?`) can not be used as option characters.
Each time it is invoked,
getoptsplaces the next option in the shell variable name, initializing name if it does not exist, and the index of the next argument to be processed into the variableOPTIND.OPTINDis initialized to1each time the shell or a shell script is invoked.When an option requires an argument,
getoptsplaces that argument into the variableOPTARG. The shell does not resetOPTINDautomatically; it must be manually reset between multiple calls togetoptswithin the same shell invocation if a new set of parameters is to be used.When the end of options is encountered,
getoptsexits with a return value greater than zero.
OPTINDis set to the index of the first non-option argument, and name is set to?.getoptsnormally parses the positional parameters, but if more arguments are given inargs,getoptsparses those instead.
getoptscan report errors in two ways. If the first character ofoptstringis a colon (:), silent error reporting is used. In normal operation diagnostic messages are printed when invalid options or missing option arguments are encountered.If the variable
OPTERRis set to0, no error messages will be displayed, even if the first character ofoptstringis not a colon.If an invalid option is seen,
getoptsplaces?intonameand, if not silent, prints an error message and unsetsOPTARG. Ifgetoptsis silent, the option character found is placed inOPTARGand no diagnostic message is printed.If a required argument is not found, and
getoptsis not silent, a question mark (?) is placed inname,OPTARGis unset, and a diagnostic message is printed. Ifgetoptsis silent, then a colon (:) is placed in name andOPTARGis set to the option character.