Monday, April 12, 2021
About BASH getopts
getopts optstring optname [ arg ]
Note
There are two reserved characters which cannot be used as options: the colon (":") and the question mark ("?").
getopts processes the positional parameters of the parent command. In bash, this is stored in the shell variable "$@". So, if you run this command:
mycmd -a argument1 -b argument2
During the time that mycmd is running, the variable $@ contains the string "-a argument1 -b argument2". You can use getopts to parse this string for options and arguments.
Every time you run getopts, it looks for one of the options defined in optstring. If it finds one, it places the option letter in a variable named optname. If the option does not match those defined in optstring, getopts sets variable optname to a question mark ("?").
If the option is expecting an argument, getopts gets that argument, and places it in $OPTARG. If an expected argument is not found, the variable optname is set to a colon (":"). Getopts then increments the positional index, $OPTIND, that indicates the next option to be processed.
The special option of two dashes ("--") is interpreted by getopts as the end of options.
getopts is designed to run multiple times in your script, in a loop, for example. It processes one option per loop iteration. When there are no more options to be processed, getopts returns false, which automatically terminates a while loop. For this reason, getopts and while are frequently used together.
Specifying the optstring
optstring is a string which defines what options and arguments getopts look for. For instance, in this call to getopts:
Let's say that you'd like the -a and -Z options to take arguments. You can specify this by putting a colon (":") after that option in optstring. For example:
Now you can specify arguments to the -a and -Z options such as -a argument1 -pZ argument2. The -p option cannot take arguments, because there is no colon after the p in optstring.
By default, getopts will report a verbose error if it finds an unknown option or a misplaced argument. It also sets the value of optname to a question mark ("?"). It does not assign a value to $OPTARG.
If the option is valid but an expected argument is not found, optname is set to "?", $OPTARG is unset, and a verbose error message is printed.
However, if you put a colon at the beginning of the optstring, getopts runs in "silent error checking mode." It will not report any verbose errors about options or arguments, and you need to perform error checking in your script.
In silent mode, if an option is unexpected, getopts sets optname to "?" and $OPTARG to the unknown option character.
Examples
Here is a bash script using getopts. The script prints a greeting, with an optional name, a variable number of times. It takes two possible options: -n NAME and -t TIMES.
As mikeserv mentions in the comment, shift $((OPTIND-1)) can be unsafe. To prevent unwanted word-splitting etc, all parameter expansions should be double-quoted. So the safe form for the command is
Note that $OPTIND is a shell variable managed by the getopts builtin command of the shell. When the shell starts, $OPTIND equals 1 and is incremented every time a getopts call discoveres options or option arguments.
he exact use of shift $(($OPTIND-1)) is to skip the options parsed and standardized by getopts (i.e. -uvh will become -u -v -h), so by doing so, you will have your first "non-option" argument in the $0 position of the argument array.
When the end of options is encountered, getopts exits with a return value greater than zero. OPTIND is set to the index of the first non-option argument, and name is set to ‘?’.
removes n strings from the positional parameters list. Thus shift $((OPTIND-1)) removes all the options that have been parsed by getopts from the parameters list, and so after that point, $1 will refer to the first non-option argument passed to the script.
Difference with getopt
Positional And Optional Argument
Positional And Optional Argument
positional and optional arguments. The positional arguments must occur at the location specified (ex: $1, $2) while the optional arguments can occur at any position denoted by their command line flag
Saturday, March 20, 2021
Saturday, September 5, 2020
List of DNS Reocrds and Its explanations
Before going further into the details, it’s important to know that A and CNAME records are standard DNS records, whilst ALIAS and URL records are custom DNS records. Both are translated internally into A records to ensure compatibility with the DNS protocol.
The CNAME record maps a name to another name. It should only be used when there are no other records on that name.
Removing Duplicates Using AWK and How it Works.
<script data-ad-client="ca-pub-7841181112240136" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
As a normal method in unix we need to use sort and then uniq to remove the duplicates from the file. Because without sort, it wont give correct unique values.
Instead of that we can use awk command to remove the duplicates in the file. For this we need to use awk associate array.
Short notes about AWK associative array
Unlike regular arrays in AWK associative arrays the indexes need not to be continuous set of number; you can use either string or number as an array index. Also, there is no need to declare the size of an array in advance – arrays can expand/shrink at runtime.
Its syntax is : array_name[index] = value
Example
Cat tes
File Contents
Cat test
1
2
3
1
1
3
3a
3a
5
Command to Use: cat tes | awk '!seen[$0]++'
How it Works
Seen[$0] - Uses the current line as the key to the array a. In our case 1 so array index will become 1 2 3 3a and 5. As we can't have same array index.
Note : Seen is an arbitrary word, it can be any words like a, b or any strings.
if seen[1] is never reference before then a[1] evalutes to empty string as awk will crate empty if it was not initialized before. IN this zero is false. If we negate then we will get true result. if it is non-zero (true) then we will get false result.
uses the current line $0 as key to the array a, taking the value stored there. If this particular key was never referenced before, a[$0] evaluates to the empty string.
!seen[$0]
The ! negates the value from before. If it was empty or zero (false), we now have a true result. If it was non-zero (true), we have a false result. If the whole expression evaluated to true, meaning that a[$0] was not set to begin with, the whole line is printed as the default action.
Also, regardless of the old value, the post-increment operator adds one to a[$0], so the next the same value in the array is accessed, it will be positive and the whole condition will fail.
Below is the actual way how awk with expression works
awk 'expression' file Is actually a short hand of: awk 'expression {print $0}' file
Whenever the a test with no associated action is true, the default action is triggered. The default action is the equivalent of { print } or { print $0 }, which prints the current record, which for all accounts and purposes in this example is the current unmodified line of input.