getopts function | Working with Flags / Options - Tech Arkit

getopts optstring name [args]. getopts  is  used by shell procedures to parse positional parameters. optstring contains 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  characters may not be used as option characters. Each time it is invoked, getopts places 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  variable  OPTIND. OPTIND  is  initialized to 1 each time the shell or a shell script is invoked.  When an option requires an argument,  getopts  places  that argument  into  the variable OPTARG.  The shell does not reset OPTIND automatically; it must be manually reset between  multiple  calls  to getopts  within  the same shell invocation if a new set of parameters is to be used.



              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 ?.



              getopts normally parses the positional parameters, but if more  arguments are given in args, getopts parses those instead.



getopts  can  report  errors  in two ways.  If the first character of optstring is 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 OPTERR  is set  to  0,  no  error  messages will be displayed, even if the first character of optstring is not a colon.



              If an invalid option is seen, getopts places ? into name and, if  not silent,  prints  an  error  message and unsets OPTARG.  If getopts is silent, the option character found is placed in OPTARG and  no  diagnostic message is printed.



              If  a  required  argument  is not found, and getopts is not silent, a question mark (?) is placed in name, OPTARG is unset, and a  diagnostic  message  is  printed.  If getopts is silent, then a colon (:) is placed in name and OPTARG is set to the option character found.



              getopts returns true if  an  option,  specified  or  unspecified,  is found.   It  returns false if the end of options is encountered or an error occurs.



Arrays in Shell Scripting Tutorial for Beginners

ARRAYS :-



korn and bash support one-dimensional arrays when the first element has the index 0 . Here's how you set and evaluate the value of the third element of the array prompt



prompt[2]="Enter your name"

echo ${prompt[2]}



Note that evaluation is done with { }, and prompt[2] is treated just like a variable. It, however, doesn't conflict with a variable prompt that you may also define in the same shell



set -A month_arr 0 1 2 3 4 5 6 7 8 9 10       korn only



month_arr=(0 1 2 3 4 5 6 7 8 9 )              bash only



array stores the number of days available in each of the 12 months



echo ${month_arr[6]}



using @ or * as subscript, you can display all elements of the array as well as the number of elements



echo ${month_arr[@]}



echo ${#month_arr[@]}     - length of array



Arithmetic Operators | Shell Scripting Beginners Tutorial-34

Arithmetic operators :-



+ addition



- substraction



\* multiplication



/ division



% modulo division (it gives remainder)



eg:-



5 % 2 = 1 (remainder)



5 / 2 = 2


Special Variables | Shell Scripting Tutorial For Beginners-32

$* it stores the complete set of positional parameters as a single string



$# It is set to the number of arguments specified



$1 first argument



$2 second argument



$0 name of executed command



"$@" each quoted string treated as a seperate argument



$? exit status of last command



$$ PID of the current shell



$! PID of the last background job

Variables | Shell Scripting Tutorial for Beginners-31

Variables are used to store information to be referenced and manipulated in a computer program. They also provide a way of labeling data with a descriptive name, so our programs can be understood more clearly by the reader and ourselves. It is helpful to think of variables as containers that hold information. Their sole purpose is to label and store data in memory. This data can then be used throughout your program.