Shell script learning notes (6)-redirection and traps

Source: Internet
Author: User

Redirection

 

File Code: 0, 1, 2 ,...
<Input> output

Most of the conversion semantics cannot be used independently and must be used together with the Bash command.

 

Operation file:

Fd <> File
Fd file code, available for reading and writing

Fd <&-
Close input file

Fd> &-
Close output file

N <& m
Copy to input file code m, save as file code n, and connect n to m

N> & m
Copy to the output file code m, save it as file code n, and connect n to m

 

Once the standard input is backed up, you can perform operations on the standard input, and then restore the standard input, as follows: exec
0 <& 5 5 <&-

 

Conversion usage:

Switch to input fd <File

Conversion to output fd> File
Set-o noclobber set-C does not overwrite
Fd> | forcibly overwrite the file

Move to additional fd> File

Standard errors are redirected along with standard output: &> files
Or> & file

 

Here
Document redirection

Fd <mark

Materials

...

...

Mark

Example

 

#!/bin/bash

exec 6<<EOF
line1
line2
line3
EOF

while read <&6
do
echo $REPLY
done

Result

 

 

Trap trigger

 

During Script execution, sometimes you want to process system signals in real time. For example, if you press ctrl-c, there are two Processing Methods: receiving or ignoring signals. The former usually

Execute specific commands on the signal arrangement. The latter mainly wants to avoid unexpected interference with script running. A mechanism like this that captures specific information and responds is called trap)

 

Ps
Auxw ps-ef

View the system-defined signal kill
-L or trap-l


 

The signal name is case-insensitive. You can use a digital code or a signal name when passing a signal.

 

Signal Transmission Method

Use the built-in command kill:

Kill
-Signal process ID

Kill
-S signal process ID

Kill
-N signal process number

After kill, you can receive multiple process numbers and send the specified signals to these processes. If '-sign' is omitted, the SIGTERM signal is sent to the process by default.

Killall
-Signal process name

Example

 

#!/bin/bash

for ((;1;))
do
sleep 1
done

Run

 

Application of trap

Syntax

Trap
'Trigger directive 'signal serial

Purpose: register a signal with shell. As long as the system generates this specific signal, trap will trigger and execute the "trigger command" in the trap command"

 

Trap-
Signal serial

Purpose: If the trap syntax does not trigger the command, but is-or blank, the original function of the signal serial can be restored. If trap has been used to make other arrangements for some signals, trap will be used to restore their original functions before the script ends.

 

Trap
''Signal serial

Purpose: Ignore the "signal serial" sent by the system, and do not send any requests.

The kill and stop signals cannot be captured by trap or ignored.

 

Trap
-P

Purpose: list the signals currently registered by trap to shell.

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.