Linux Unix Shell Programming Guide Learning Notes (part fifth)

Source: Internet
Author: User
Tags ftp transfer

The 25th chapter in-depth discussion <<

When the shell sees <<, it knows that the next word is a delimiter, and the content behind the delimiter is treated as input until the shell sees the delimiter (in a separate line). Like what:

Cat >> Tmpfile <<DOC> This was the first line> this is the second line> third> Forth>.....>do C
Where Doc is the delimiter, when you enter Doc again in a new line, it means that the input is complete.

Quickly create a file

Auto-Entry Menu

FTP Transfer (instance operation)

Connecting to other application systems (instance operations)

Quickly create files

Cat >> tmpfile <<delimiter (System not recognized after adding tab) or cat >> tmpfile <<-Delimit Er

Quickly create a printed document (enter the document and press ENTER to automatically send to the printer): LPR << QC

Chapter 26th Shell Tools

Create a date-named file and a temporary file


Trap command and how to capture the signal

eval command

Logger command

To create a file using the date command:

Date format: Date [option] +%format (please refer to date--help for details)

Work often defines some of their own file formats under Xxx_xxx_ date information. Log to create the file:

Which date information of the command date +%y_%m_%d:%h:%m:%s.%n

command to create a file: >xxx_xxx_ ' date +%y_%m_%d:%h:%m:%s.%n '. Log, LS will see the following file:


Create a unique temporary file: Use $$ (process number) to create a temporary file that is unique in the script run (the process number is unique in the system ) and delete the temporary file when exiting the program.


A signal is a message that the system sends to a script or command. Common signals and their meanings table (Kill-l shows all signals):

Signal <span style= "White-space:pre" ></span> signal name meaning 1SIGHUP hangs or the parent process is killed 2SIGINT interrupt signal from the keyboard, usually < ctrl-c> 3SIGQUIT exit from keyboard 9SIGKILL unconditional termination 11SIGSEGV segment (memory) conflict 15SIGTERM software termination (default kill process signal)
Format of sending signal: Kill [-signal | -S signal] pid[s]

Kill a Process

Close current shell:kill-9 $$ (also can query process information with PS)

Detection signal

Some signals can be captured by the program or script, but some signals cannot be captured (the signal is received 9 and no other signal is received).

The signals that need attention in the script are 1 SIGHUP, 2 SIGINT, 3 sigquit, and SIGTERM

These signals are captured to the system for processing, or the signals are processed by the script or application itself (a trap command is required).


The trap command captures the signal in the script, trap name (the processing function that snaps to the signal) signal (s)

Examples of common methods of trap:

Trap "" "2 3 ignore signal 2 and signal 3, the user cannot terminate the script trap" commands "2 3 if the signal 2 or 3 is captured, execute the corresponding C o m m a n d S command trap 2 3 Reset signal 2 and 3, the user can terminate the script can also use single quotation marks (") to replace Double quotation marks (""); the result is the same.
Here is an example of a listening keyboard interrupt <CTRL_C>:

#!/bin/bash trap "Keyboardinthandler" 2function Keyboardinthandler () {    echo "Ctrl_c pressed!"} While [1] do    sleep 2done

Eval uses a domain once to scan for variables that cannot implement its function-it first scans the command line for all permutations before executing the command.

Execute the string containing the command:

#!/bin/bash name= ' cat ctrl_c_listener ' echo-e  $NAME ' \ n ' eval $NAME
From the above execution results, it is found that the eval echo is the contents of the file, not the string value of name.

For a file that only has a name-value pair, use Eval to take the first column as a variable, and the second column as the value, the sample code is as follows:

Key_value_pair content: Key_one     timekey_two     lovekey_three   mum script kv_eval contents: #!/bin/bash while read NAME Typedo    eval ' echo ' ${name} "=" ${type} "' Done < Key_value_pairecho" Key_one's value is  ${key_one} "echo" Key_two ' s Value is  ${key_two} "

Logger command:

It is best to consult the online manual before using this command (the syntax for this command differs from the operating system provided by the vendor).

Logger command format: Logger [options] Message

Options in Ubuntu 12.04:

-D,--udp use             UDP (TCP is default)-I,--id              log the process ID too-f,--file <file>     log the contents of  This file-h,--help            Display the text and exit-n,--server <name> write to this   remote syslog server -P,--port <number> Use this   UDP port-p,--priority <prio> Mark given message with this priority-s,--s Tderr          output message to standard error as WELL-T,--tag <tag>       mark every line with this tag-u,--socket <socket> write to this Unix socket-v,--version         output version information and exit

The 27th chapter several script examples

Pingall: A script that pings all hosts in accordance with the entries in the/etc/hosts file.
Backup_gen: A generic backup script that can load the default settings.
Del.lines: A script that references the SED command to remove several rows from the file.
Access_deny: A tool that prevents certain users from logging on.
Logroll: A tool that cleans up logs that exceed a certain length.
Nfsdown: A quick tool to unmount all NFS file systems


28th Run Level Script

If you want to automatically run some applications, services, or scripts at system startup, or if you can shut down these programs correctly when the system restarts, you need to create a run-level script.

Run level

Determine the current runlevel: Who-r (Linux system: RunLevel The first value is the previous runlevel of the system, and the second value is the current runlevel.) )


The init process starts during the system startup process, which uses the Inittab configuration file to see which services need to be started and which runlevel to enter.

Inittab format: id:rstart:action:process

ID: Unique identification of the process

Rstart: The level at which the process runs

Action: Tells the INIT process how to treat processes that correspond to the process.

Process: The actual command to run.

29th Chapter CGI Script

Cgi:common Gateway Interface (Universal gateways interface).

Protocols that connect to the Web: protocols include HTTP, FTP, mailto, file, Telnet, and news. Here we only care about the HTTP protocol (Hypertext Transfer Protocol).

This chapter mainly explains the web operation and practice.

Linux Unix Shell Programming Guide Learning Notes (part fifth)

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: 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.