Bash scripts create temporary files and signal capture

Source: Internet
Author: User

Script to create temporary files:

#mktemp Create a temporary file or directory

#mktemp [OPTION] FILE. Randomlength

[OPTION]

-D Create a directory.

For example:

#mktemp/tmp/file. Xx

/tmp/file.36

#mktemp/tmp/file. Xx

/tmp/file.37

#mktemp/tmp/file. Xx

/tmp/file.38

#mktemp/tmp/file.xxxx

/tmp/file/1234

#mktemp/tmp/file.xxxx

/tmp/file/1235

#mktemp-D/tmp/file.xx

#ll

Drwxr-xr-x/tmp/file.33



Tips for use:

#FILE = ' mktemp/tmp/file.xx ' Saves the result to a FILE variable and can be called multiple times.

#echo $FILE

/tmp/file.33

#echo $FILE

/tmp/file.33

#unset FILE is not used when you delete a variable.


The script implements the signal capture:

9 and 15 cannot be captured.

CTRL + C = SIGINT terminated.


#trap capture the signal.

#trap ' COMMAND ' siglist


For example:

1. Achieve signal capture:

#vim a.sh

#!/bin/bash

Trap ' echo ' not end ' INT

While:;d o

Date

Sleep 2

Done

2. Write a script to test the host in the network segment, while cleaning up the temporary files generated by the script.

#vim ping.sh

#!/bin/bash

net=192.168.0

File=mktemp/tmp/file.xxx

Clearup () {

echo "Quit"

Rm-f $FILE

Exit 1

}


Tarp ' Clearup () ' Sigini

For I in {1..254};d o

If Ping-c 1-w 1 $NET. $I &>/dev/null;then

echo "$NET. $I is up" |tee >> $FILE

Else

echo "$NET. $I is Down"

Fi

Done


This article is from "Small Private blog" blog, please be sure to keep this source http://ggvylf.blog.51cto.com/784661/1615383

Bash scripts create temporary files and signal capture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.