Explain trap command, shift Command, getopts command of linux.
Trap command: controls the action to be taken by the shell when a signal is received.
Trap [OPTIONS] [ [arg] signspec..]
Arg is the action to be taken or executed on receiving a signal specified in signspec.
e.g.
trap “rm $FILE; exit” // exit (signal) and remove file (action)
Shift Command: Using shift command, command line arguments can be accessed. The command causes the positional parameters shift to the left. Shift [n] where n defaults to 1. It is useful when several parameters need to be tested.
Getopts command: this command is used to parse arguments passed. It examines the next command line argument and determines whether it is a valid option
Getopts {optstring} {variable1}. Here, optsring contains letters to be recognized if a letter is followed by a colon, an argument should be specified. E.g (whether the argument begins with a minus sign and is followed by any single letter contained inside options ) If not, diagnostic messages are shown. It is usually executed inside a loop.
Explain trap command; shift Command, getopts command of linux.
Trap command is used to catch a signal that is sent to a process. An action is taken based on the signal by using the action which is defined in the trap command instead of taking the default effect on the process.
Example:
$ trap “echo ‘interrupt signal received’ “ INT.
shift command is used to replace the parameters that were sent from command line. For example
$ shift will replace $1 by $2
getopts command is used for the purpose of parsing positional parameters.