0% found this document useful (0 votes)
46 views

Unix 25

The document discusses how to ignore and reset signals in shell scripts using the trap command. It provides examples of using trap to ignore signals while executing commands and an example scan script that uses trap to ignore interrupts. It also discusses how commands are executed by the shell by forking a new process and using exec to replace the shell with the new command.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Unix 25

The document discusses how to ignore and reset signals in shell scripts using the trap command. It provides examples of using trap to ignore signals while executing commands and an example scan script that uses trap to ignore interrupts. It also discusses how commands are executed by the shell by forking a new process and using exec to replace the shell with the new command.

Uploaded by

arunabhatla
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

A procedure may, itself, elect to ignore signals by specifying the null string as the argument to trap.

The following fragment is taken from the nohup command.


trap '' 1 2 3 15

which causes hangup, interrupt, quit and kill to be ignored both by the procedure and by invoked commands.

Traps may be reset by saying


trap 2 3

which resets the traps for signals 2 and 3 to their default values. A list of the current values of traps may be obtained by writing
trap

The procedure scan (Figure 5) is an example of the use of trap where there is no exit in the trap command. scan takes each directory in the current directory, prompts with its name, and then executes commands typed at the terminal until an end of file or an interrupt is received. Interrupts are ignored while executing the requested commands but cause termination when scan is waiting for input.
d=`pwd` for i in * do if test -d $d/$i then cd $d/$i while echo "$i:" trap exit 2 read x do trap : 2; eval $x; done fi done

Figure 5. The scan command read x is a built-in command that reads one line from the standard input and places the result in the variable x. It returns a non-zero exit status if either an end-of-file is read or an interrupt is received.

3.7 Command execution


To run a command (other than a built-in) the shell first creates a new process using the system call fork. The execution environment for the command includes input, output and the states of signals, and is established in the child process before the command is executed. The built-in command exec is used in the rare cases when no fork is required and simply replaces the shell with a new command. For example, a simple version of the nohup command looks like
trap \'\' 1 2 3 15 exec $*

The trap turns off the signals specified so that they are ignored by subsequently created commands and exec replaces the shell by the command specified.

Most forms of input output redirection have already been described. In the following word is only subject to parameter and command substitution. No file name generation or blank interpretation takes place so that, for example,
echo ... >*.c

will write its output into a file whose name is *.c. Input output specifications are evaluated left to right as they appear in the command. > word The standard output (file descriptor 1) is sent to the file word which is created if it does not already exist. >> word

You might also like