Linuxshell programming guide ------ shell tool

Source: Internet
Author: User
When a user logs on, the system executes the etcprofile file. the root user does not want other common users to interrupt the process. He usually blocks signals 1, 2, 3, and 15 by setting traps, and then re-opens these signals when the user reads the messages of the day.

When a user logs on, the system will execute the/etc/profile File. the root user does not want other common users to interrupt a process. He usually blocks signals 1, 2, 3, and 1 5 by setting traps, and then re-opens these signals when the user reads the messages of the day. Finally, return to the status of shielding these signals.

Similar methods can also be used when writing scripts. In some critical moments of script running, for example, when many files are opened, you do not want the script to be interrupted to avoid damage to these files. You can solve this problem by setting trap to shield some signals. After these key processes are completed, re-open the signal.

The general format of the ignored signal is (except for signal 9 ):
Trap "" signal no :( s)

Note that there are no characters between double quotation marks. to return to the capture signal status again, you can use the following command:
Trap "do something" signal no :( s)

Next we will summarize the above methods.

Trap "" 1 2 3 15: ignore the signal.

Key processing process trap "my_exit" 1 2 3 15: return to the capture signal state, call the myexit function after capturing the signal. The following is an example. the "key" process is actually a while loop, but it can well illustrate this method. In the first loop, the signal is blocked by setting traps, but in the second example, the signal is returned to the capture state.

Both cycles only count to 6, but a sleep command is used in the loop, so that you can have enough time to experiment to interrupt the loop.

The following is the script.
[Root @ localhost huangcd] # cat trap_ignore
#! /Bin/bash
Trap "" 1 2 3 15
LOOP = 0
My_exit ()
{
Echo "Received interrupt on count $ LOOP"
Echo "Now exiting"
Exit 1
}
LOO = 0
While:
Do
LOOP = 'expr $ LOOP + 1'
Echo "critical processing... $ LOOP... you cannot interrupt me"
Sleep 1
If ["$ LOOP"-eq 6]
Then
Break
Fi
Done
LOOP = 0
Trap "my_exit" 1 2 3 15
While:
Do
LOOP = 'expr $ LOOP + 1'
Echo "no-critical processing... $ LOOP... interrupt me if you want"
Sleep 1
If ["$ LOOP"-eq 6]
Then
Break
Fi
Done
[Root @ localhost huangcd] # sh trap_ignore
Critical processing... 1 .. you cannot interrupt me
Critical processing... 2 .. you cannot interrupt me
Critical processing... 3 .. you cannot interrupt me
Critical processing... 4 .. you cannot interrupt me
Critical processing... 5 .. you cannot interrupt me
Critical processing... 6 .. you cannot interrupt me
No-critical processing... 1 .. interrupt me if you want
Received interrupt on count 1
Now exiting

The eval command first scans the command line for all replacements and then executes the command. This command is applicable to variables that cannot implement its functions at one scan. This command scans variables twice. These variables that require two scans are sometimes called complex variables. However, I think these variables are not complicated.

The eval command can also be used to display simple variables, not necessarily complex variables.
[Root @ localhost huangcd] # NAME = honeysuckle
[Root @ localhost huangcd] # eval echo $ NAME
Honeysuckle
[Root @ localhost huangcd] # echo $ NAME
Honeysuckle

First, create a small file named testf, which contains some text. Next, assign cat testf0 to the variable MYFILE. now we can echo the variable to see if the above command can be executed.

[Root @ localhost huangcd] # cat testf
May Day, May Day
Going Down

Now we assign cat testf to the variable m y f I L E.
[Root @ localhost huangcd] # MYFILE = "cat testf"
[Root @ localhost huangcd] # echo $ MYFILE
Cat testf
[Root @ localhost huangcd] # eval $ MYFILE
May Day, May Day
Going Down

From the above results, we can see that using the eval command can not only replace the variable, but also execute the corresponding command. Variable replacement is performed for the first scan, and cat testf is executed for the second scan.

The eval command can also be used to display the last parameter passed to the script. Now let's look at the example below.
[Root @ localhost huangcd] # cat evalit
#! /Bin/bash
Echo "Total number of arguments passed is $ #"
Echo "The process id is $"
Echo "Last argument is" $ (eval echo \ $ #)
[Root @ localhost huangcd] # sh evalit huang cheng du
Total number of arguments passed is 3
The process id is 5351
Last argument is du

You can give a value a variable name. I will explain this as follows, assuming there is a file named d a t:

[Root @ localhost huangcd] # cat data
PC 486
MONITOR svga
NEWWORD yes

You want the first column in the file to be the variable name, and the second column to be the value of the variable, so you can:

We use the first line of the data file to explain the execution process of the above script. the script reads the words "PC" and "486" and assigns them to the variable NAME and TYPE respectively. In the first scan of the Eval command, replace NAME and TYPE with "PC" and "486" respectively. in the second scan, the PC is used as the variable and "486" is used as the variable value.
The following is the result of running the above script:
[Root @ localhost huangcd] # cat eval_it
#! /Bin/bash
While read NAME TYPE
Do
Eval 'echo "$ {NAME }=$ {TYPE }"'
Done <data
Echo "you have a $ PC, with a $ MONITOR monitor"
Echo "and are you network? $ NEWWORK"
[Root @ localhost huangcd] # sh eval_it
You have a 486, with a svga monitor
And are you network? Yes

The system contains a considerable number of log files. One of the log files is called messages, which is usually located in the/var/adm or/var/log directory. A configuration file named syslog can be used to define messages recorded in the messages file. these messages have a certain format. If you want to know the corresponding configuration in the system, you can view the/etc/syslog. conf file. This file contains the tools used to send different types of messages and their priority.

Here we do not want to go into the details of how UNIX and LINUX record information in this file. We now only need to know that these messages have different levels, from informative messages to critical messages.

You can also use the logger command to send messages to the file. Before using this command, you 'd better check the connection manual, because the syntax of this command varies with the operating systems provided by different vendors.

However, because only information messages are involved here, you do not have to worry about the security of the following commands.

You may send messages to the file for the following reasons:

Access or login that occurs during a specific period of time.

Some of your scripts that execute key tasks fail to run.

Monitoring script report.

The following is an example of the/v a r/a d m/m e s a g e s file. The corresponding file displayed on the system may be slightly different from the following example.
[Root @ localhost huangcd] # cat/var/log/messages | more
Dec 11 09:49:29 localhost syslogd 1.4.1: restart.
Dec 11 09:49:44 localhost vmsvc [3122]: [warning] [guestinfo] RecordRoutingInfo:
Unable to collect IPv4 routing table.
Dec 11 09:50:44 localhost last message repeated 2 times
Dec 11 09:52:14 localhost last message repeated 3 times
Dec 11 09:53:44 localhost last message repeated 3 times
Dec 11 09:55:14 localhost last message repeated 3 times
Dec 11 09:56:44 localhost last message repeated 3 times
Dec 11 09:58:14 localhost last message repeated 3 times
Dec 11 09:59:44 localhost last message repeated 3 times
Dec 11 10:01:14 localhost last message repeated 3 times
Dec 11 10:02:44 localhost last message repeated 3 times
Dec 11 10:04:14 localhost last message repeated 3 times
Dec 11 10:05:44 localhost last message repeated 3 times

The general format of the logger command is:
Logger-p-I message

Where:

-P: Priority. here, only the priority indicating the user's attention is involved. this is also the default value.

-I: records the process number of the sent message in each message.

You can use the following command to write data to the message file, which may take several minutes to see:
[Root @ localhost huangcd] # logger-p notice "this id a test message. ignore $ LOGNAME"
[Root @ localhost huangcd] # tail/var/log/messages
Dec 15 18:02:47 localhost syslogd 1.4.1: restart.
Dec 15 18:03:00 localhost vmsvc [3157]: [warning] [guestinfo] RecordRoutingInfo: Unable to collect IPv4 routing table.
Dec 15 18:04:00 localhost last message repeated 2 times
Dec 15 18:04:02 localhost root: this id a test message. ignore root

As you can see, the user who sent the message is also recorded.

A more reasonable use of sending information to a log file is used when the script exits abnormally. If you want to send messages to the log file, you only need to include the l o g e r command in the exit function of the capture signal.

In the following cleanup script, if the script captures signal 2, 3 or 1 5, it will send a message to the log file.
[Root @ localhost huangcd] # cat cleanup1
#! /Bin/bash
Trap "my_exit" 2 3 15
My_exit ()
{
Logger-p notice "'basename $ 0': was killed whilst cleaning up system log"
Exit 1
}
While ["1"-lt "2"]
Do
Sleep 1
Done
[Root @ localhost huangcd] # sh cleanup1
[Root @ localhost huangcd] # cat/var/log/messages
Dec 15 18:02:47 localhost syslogd 1.4.1: restart.
Dec 15 18:03:00 localhost vmsvc [3157]: [warning] [guestinfo] RecordRoutingInfo: Unable to collect IPv4 routing table.
Dec 15 18:04:00 localhost last message repeated 2 times
Dec 15 18:04:02 localhost root: this id a test message. ignore root
Dec 15 18:04:30 localhost vmsvc [3157]: [warning] [guestinfo] RecordRoutingInfo: Unable to collect IPv4 routing table.
Dec 15 18:20:30 localhost last message repeated 3 times
Dec 15 18:21:00 localhost vmsvc [3157]: [warning] [guestinfo] RecordRoutingInfo: Unable to collect IPv4 routing table.
Dec 15 18:21:06 localhost root: cleanup1: was killed whilst cleaning up system log
Dec 15 18:21:30 localhost vmsvc [3157]: [warning] [guestinfo] RecordRoutingInfo: Unable to collect IPv4 routing table.
Dec 15 18:21:37 localhost root: cleanup1: was killed whilst cleaning up system log

Related Article

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.