The main purpose of the script is to practice capturing signals in a Linux bash script, by practicing the use of functions, and by terminating a running program, subsequent processing of files opened by the program, and so on!
Scripting Features:
Ping IP within a network segment, detect which IP is online, which IP is not online
command to Practice:
1, Mktemp
Usage: #mktemp 1117.www.qixoo.qixoo.com/path/to/somefile.xxx where XXX can be multiple, is a string that is randomly generated by the system
-D Create as Directory
2, Ping test network is unobstructed
Usage: #ping [-C #] [-W N] IP
-C #, #表示指定ping的次数
-W n,n indicates a specified time-out
3. Trap Capture Signal
Usage: #trap ' COMMAND ' sign_table
Common signals are:
1:sighup function: To reread a process configuration file without restarting the process
2:sigint function: Interrupt a process, CTRL + C sends the signal
9:sigkill effect: To kill a process in any case, relative to signal number 15th
15:sigterm action: Will let it take care of its own affairs and then kill the process.
18:sigcont action: To keep a stopped process going
19:sigstop action: To make a process stop the job, CTRL + Z is sending the signal
4. Tee reads the standard input data and outputs its contents to a file
Usage: Tee [-A]/path/to/somefile
-A: Represents the attachment to the back of an existing file, rather than overwriting it.
Note: The tee instruction reads data from a standard input device and outputs its contents to a standard output device while saving to a file.
!/bin/bash
net=192.168.1
file= ' Mktemp qkxue.net/tmp/file.xxxxxx '
Cleanup () {
echo "Quiting ..."
Rm-f $FILE
Exit 1
}
Trap ' cleanup ' INT
For I in {1..254};d o
If Ping-c 1-w 1 $NET. $I &>/dev/null; Then
Echo-e "\033[32m$net. $I is up\033[03m" | tee-a $FILE
Else
Echo-e "\033[31m$net. $I is down\033[03m"
Fi
Done
Interested friends, you can debug here, in the process of ping Press CTRL + C to try the effect Oh!
Ping a network segment of signal capture and function practice scripts in shell scripts under Linux