Bash command and shell script Summary (3) command

Source: Internet
Author: User
Tags rsync

@ Rigorous use of set @

Set-eux

-E. If the command returns a non-zero value, exit immediately.

-U when the execution parameter is included, the unset variable is used as the error handling (if the extension attempt appears in the unset variable, the shell displays the error message. if it is not interactive, exit with a non-zero value)

-X: after each simple command is extended, the PS4 extension value is displayed, followed by the command to be executed.

 

@ Set-E possible errors @

Not the return values of all commands. If it is not 0, it indicates a failure.

DIFF: 0 indicates the two files are the same; 1 indicates the two files are different; 2 indicates an exception. For example, one of the two comparison projects is that the directory is not a file.

Grep: 0 indicates that a match is returned; 1 indicates that no match is returned; 2 indicates an exception. For example, grep targets folders, but does not add traversal.

Problems

Set-E is not omnipotent. If the returned value 2 is abnormal without bypassing diff, grep, and so on, the process will be terminated in some inexplicable situations.

Too formal habits will cause unnecessary troubles

If [$? -EQ 0]; then

How to solve the problem in set-e

Pack this special command with set + E and set-E, that is, temporarily disable the E Option.

Pack this special command ()

Set-E is only valid for the current thread and is invalid for the Child thread

() Creates a sub-thread for the process. Generally, the E option is disabled by default.

 

@ Unset usage @

Uninitialized_variable = 23 # assign a value

Unset uninitialized_variable # unset it.

Echo "uninitialized_variable = $ uninitialized_variable"

 

@ Mkdir-P @

It can be a path name. If some directories in the path do not exist, after this option is added, the system automatically creates those directories that do not exist, that is, multiple directories can be created at a time. For example, if the current directory contains test, mkdir test/Test2/test3 reports an error because Test2 does not exist and cannot be created. Mkdir-P test/Test2/test3.

 

@ Copy all files with a folder shell vs copy all files without a folder Shell

If CP./ABC/*. * test_duplicate/, all files under test duplicate will be available.

If CP./ABC/test_duplicate/is used, the ABC/folder and all its files under test duplicate will exist.

If CP./ABC test_duplicate/is used, the ABC/folder and all its files under test duplicate will exist.

 

@ Cat

1. display the entire file at a time. $ Cat filename

2. Create a file from the keyboard. $ Cat> filename

3. Clear an existing file. $ CAT/dev/null> redirectfile.txt

4. Merge several files into one file and output them. The input files are printed in the order of arrangement. If you want to change the information, you must change the order of the input files. Cat file1 file2 file3> fileall

5. When the number of files to be processed is too large and the file name cannot be manually entered, you can use wildcards, such

Cat chapter *> book combines all the files starting with chapter in the folder into the book file.

When wildcard characters are used, the file names are listed in ascending order.

6. The cat-s file combines multiple blank rows to form a separate blank row.

Cat-s file | sed '/^ [[: Space:] * $/d' clear all blank lines

7. Cat-B file numbers non-blank rows

8. Cat-N file numbers all rows

 

@ Echo format

By default, echo returns a line feed symbol after argument is generated. -N option can cancel line feed.

 

@ Nohup @

Which of the following methods can be used to make a process run in the background?

① Nohup Ctrl + Z then BG

② Screen command

③ Crontab

@ Crontab execution @

Basic Format:

* ***** Command by hour, day, month, and week

The 1st column indicates minute 1 ~ 59 minutes * or
*/1 indicates

The first column indicates the hour 1 ~ 23 (0 indicates 0 points)

The 3rd column indicates the date 1 ~ 31

The 4th column indicates the month 1 ~ 12

The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)

6th columns of commands to run

 

30 21 ***/usr/local/etc/rc. d/Lighttpd restart 1>/dev/null 2> & 1

The preceding example indicates restarting Apache at every night.

 

45 4, 10, 22 **/usr/local/etc/rc. d/Lighttpd restart 1>/dev/null 2> & 1

The example above indicates 4 of the 1st, 10th, and 22nd of every month.
: 45 restart Apache.

 

10 1 ** 6, 0/usr/local/etc/rc. d/Lighttpd restart 1>/dev/null 2> & 1

The preceding example indicates that Apache is restarted at every Saturday and Sunday.

 

0, 30 18-23 ***/usr/local/etc/rc. d/Lighttpd restart 1>/dev/null 2> & 1

The preceding example indicates that Apache is restarted every 30 minutes between and every day.

 

0 23 ** 6/usr/local/etc/rc. d/lighttpdrestart 1>/dev/null2> & 1

The preceding example indicates that Apache is restarted at every Saturday.

 

**/1 ***/usr/local/etc/rc. d/Lighttpd restart 1>/dev/null 2> & 1

Restart Apache every hour

 

* 23-7/1 ***/usr/local/etc/rc. d/Lighttpd restart 1>/dev/null 2> & 1

Restart Apache every hour between PM and PM.

 

0 11 4 * Mon-wed/usr/local/etc/rc. d/Lighttpd restart 1>/dev/null 2> & 1

Restart Apache on November 4 and every Monday to Wednesday.

 

0 4 1 Jan */usr/local/etc/rc. d/Lighttpd restart 1>/dev/null 2> & 1

Restart Apache at on January 1, January 1

 

 

@ Cut usage

[@ Administer] # echo 11-22-33 | cut-d "-"-F2, 3

22-33

[@ Administer] # echo 11-22-33 | cut-d "-"-F2, 3 | cut-d "-"-F1

22

Note that if there are spaces before and after the MPs queue symbol, the same display result will be output.

 

@ Head and tail @

Head is used to view 10 lines in the file header,

Tail is used to view the end 10 rows of the file. Tail can also be used to track file updates.

Head <FILENAME>:

You can use the head command to view the first few lines of content in a specific file. This command is the first 10 lines of content by default. If you want to view more information, you can set it using a number option, for example

Head-20 install. Log

By running the preceding command, you can view the first 20 lines of content in the install. log file.

Unlike the head command, the tail command is used to view the content of the last few lines of a specific file. By default, it is used to view the content of the last 10 lines of the file. Similarly, if you want to view more information, you can also set it using the number option, for example

Tail-20 install. Log

You can also use tail to observe the log file update process. With the-F option, tail automatically runs in real time.

To display the new message in the open file to the screen. For example, you need to observe the changes in/var/log/messages in real time, and prompt in shell as the root user.

Run the following command:

Tail-F/var/log/messages

 

@ Sort @ parameter @

Press ctrl-v-I

Cat finalresult2.txt | sort-t'-K1, 1-K3, 3 Nr> finalresult3.txt

Note that when sorting is based on more than one column, it should be written as sort-T ''-K1, 1-K3, 3nr, and cannot be written as sort-T''-k1-k3nr.

 

@ View disk space size @

Usage 1 DF-H

[@ Administer ~] # DF-H

Filesystem size used avail use % mounted on

/Dev/mapper/vgroot-lvroot

3.9g 482 m 3.3g 13%/

/Dev/mapper/vgroot-lvusr

4.9g 2.3g 2.4g 50%/usr

/Dev/mapper/vgroot-lvvar

3.9g 427 m 3.3g 12%/var

/Dev/mapper/vgroot-lvopt

137 Gbit/s 79 Mbit/s 137 Gbit/s 1%/OPT

/Dev/CCISS/c0d0p1 190 m 19 m 162 m 11%/boot

Tmpfs 6.9G 16 K 6.9g 1%/dev/SHM

/Dev/CCISS/c0d1p1 8.2 t 7.3 t 992G 89%/search

Glusterfs #10.12.130.18

60 TB, 49 TB, 12 TB, 81%/GFS

Usage 2

Du-CH: view the entire folder.

Du-CHS are separated to view all the small folders under this folder.

Du -- Max-depth = 1 Separate all small folders with a depth of 1 under this folder.

 

@ TOP command to view running status @

# Top

Top-09:13:42 up 43 days, 2 users, load average: 11.62, 26.60, 20.90

Task: 168 total, 2 running, 166 sleeping, 0 stopped, 0 zombie

CPU (s): 24.8% us, 3.2% Sy, 0.0% Ni, 69.4% ID, 2.4% wa, 0.0% hi, 0.2% Si, 0.0% St

Mem: 16443520 k total, 10953860 K used, 5489660 K free, 2540 K Buffers

Swap: 8388600 k total, 8286076 K used, 102524 K free, 429984 K cached

# Ll/proc/13299.

 

@ Change the owner of a file or folder @

Chown-r FELICIA: Felicia./temptask/

 

@ Rsync speed limit and display details @

Rsync -- bwlimit = 1024-v0.0.0.0: Root/A/B/C /*./

 

@ Chmod usage

Chmod U-X, go + RW linuxsir007.txt Note: minus the file owner's right to execute the file, add the owner group and other users to read and write the file;

 

@ Find and grep @

Find is used to find the specified file in the file system. The format is the path expression to find.

For example

Find.-Name 1.txt find the file 1.txt in the current directory and its sub-directory

Find/tmp-name 1.txt find the file 1.txt in the/tmp directory and its sub-directory

The grep command is used to find the specified pattern matching format. The grep [Command Option] matches the pattern to be searched [the file to be searched].

For example

Grep cams test.txt search for the cams string in the test.txt File

Grep-r CAMS/root/CAMs search for the cams string in all the files in the/root/CAMs directory and Its subdirectories

In addition to searching for files, the grep command can redirect any output stream to grep for search.

PS-Ef | grep ora: search for all processes whose names contain ora.

 

@ Pstree @

Pstree-AP is generally used to view the number of processes (view in detail, including the commands and PID contained in)

 

@ Kill a process @

Process = 'ps-Ef | grepyourprocess | grep-V grep | awk '{print $2 }''

Kill-9 $ Process

@ Kill the parent process and its Child processes @

[@ Administer_18_71 Scripts] # pstree-AP | grep statistics. Sh

|-Grep, 14231 statistics. Sh

| '-Sh, 1642 statistics. Sh

[@ Administer_18_71 Scripts] # pstree-AP 1642

Sh, 1642 statistics. Sh

|-Cat, 1643 ../data/adid_log.20120506

'-EXE, 1644 200000000300000 ../data/pv_pvid.20120506 ../data/cd_pvid.20120506

[@ Administer_18_71 Scripts] # kill1642 1644

[@ Administer_18_71scripts] #

[1] + terminated nohup sh statistics. Sh

 

@ Screen command @

Screen-s sessionid

CTRL + a d Exit the created window (return to the environment before the screen)

Screen creates a new terminal window

Screen-ls can be used to view all screen sessions.

Screen-r sessionid can be entered into the specified screen session of sessionid. Exit when it is no longer in use.

Screen-D sessionid forcibly switches a session to the detached status

 

@ String deletion @

For the sake of completeness, I will use some examples to illustrate some special functions of $:

We define a variable:

@ $ {}@

File =/dir1/dir2/dir3/my.file.txt

We can use $ {} to calculate different values for the token:

$ {File # */}: remove the first/and its left outer strings: dir1/dir2/dir3/my.file.txt

$ {File ### */}: remove the last/and its left string: my.file.txt

$ {File # *.}: remove the first. And its left-side string: file.txt

$ {File ### *.}: remove the last. And its left string: txt

$ {File %/*}: remove the last line/and its right string:/dir1/dir2/dir3

$ {File %/*}: remove the first/and its right-hand string: (null)

$ {File %. *}: remove the last. And its right-hand string:/dir1/dir2/dir3/My. File

$ {File %. *}: remove the first. And its right-hand string:/dir1/dir2/dir3/My

Remember the method as follows:

# Remove the left cursor (on zookeeper # On
$ To the left)

% Is to remove the right partition (% in
$ Right Region)

Single-character matching is the minimum matching. The two single-character matching is the maximum matching.

$ {File: 0: 5}: extract the leftmost five words:/dir1

$ {File: 5: 5}: extract 5th words. Extract 5 words from the right pane:/dir2

$ {File/DIR/path}: Replace the first dir with Path:/path1/dir2/dir3/my.file.txt.

$ {File // DIR/path}: replace all dir with Path:/path1/path2/path3/my.file.txt.

 

@ Use touch to change the file modification time @

For example, touch-M file1-r file2 changes the time of file1 to the time of file2.

Example 1 touch-M feature. cpp-r config. cpp

Example 2 touch-M file1-t time (yyyymmddhhmm) uses the time specified by time to modify the file1 time

Example 2 touch-M feature. cpp-T 201211052230

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.