Shell temporary notes

Source: Internet
Author: User

-EQ equals

-Ne is not equal

-GT greater

-Ge is greater than or equal

-Lt is less

-Le is less than or equal

 

SEQ 1 30 numbers from 1 to 30

Touch-mt2010030303.03 $ dir: Modify the directory time format

STAT/tmp/1.dir view the modification time


Lines = 'grep' ^ # '$ file | WC-l'

Echo "$ file: $ lines" // you can use multiple variables to obtain the result and assign the value to the variable ''.



Cat file.txt | tr-s '\ n' compresses multiple consecutive characters into a single character


Cat-N display row number


Cat file.txt | xargs-N 3 the maximum value of each line is 3.

Line line

Line line

Line line



Tr can be replaced, deleted, and compressed.

The first part of TR '1' 2 is replaced by the second part.

Echo "Hello who is this" | tr 'a-M, N-z'' a-m, N-Z 'capital A-M N-Z replace with a-m, N-Z


Tr-D delete character

Echo "Hello 0980 world" | tr-D '0-9'

Hello World


The complement set of tr-C [set1] [set2] characters is reversed.

Echo-e "Hello 0980 world" | tr-D-C '0-9 \ N'

0980


Tr-s compression character (compressing multiple consecutive characters into a single character)


Backup File

CP file.txt/tmp/filebak.txt; RM-RF file.txt


########### MySQL ################

Select Concat (UID, '@', cname) as email, nickname, mobile, createdate, passwd from mop2oxdatabases

This means to integrate the UID field and cname field in the mop2oxdatabases database into an email field. When each field is extracted, separate them with commas (,).


Mysql-uroot-p-h 10.101.120.38-P 3308 mop-e "" command line operations



Dd command

Dd If =/dev/Zero of = anbaoyu. Data BS = 1 m COUNT = 1

/Dev/zero is a bubble machine (spit data)


File modification time

Touch-A changes the access time

Touch-MT changes the file modification time

Touch-D Timestamp



While loop statement


1 While read line

Do

Echo $ line

Done <a.txt A can only be a file, reading the row of the file


<A.txt is written at the end, which is equivalent to adding a constraint to the entire while do statement. Each row in a is read until the end of the file.


2 while read line <

Do

Echo $ line

Done

<A is written on the front side, and the entire while do statement does not have any constraints, because read line <A, which is always true (display the first row, endless loop ), indicates to read the first row without stopping, assign the value to line, and print the parameter.



3 awk print multiple rows of data

Ls-L | awk '{print $1 ":" $7}' $1 $7 are separated:


Awk 'nr = m, Nr = n' filename prints the range from Row M to row n


4 print in '/start_pattern/,/end_pattern/' filename

For example:

$ Cat section.txt

Line with pattern1

Line with pattern2

Line with pattern3

Line End with pattern4

Line with pattern5


$ Awk '/PA. * 3/,/end/'section.txt

Line with pattern3

Line End with pattern4


Replace some text in the variable content

$ Var = "this is a line of test"

$ Echo $ {var/line/replaced}

"This is a replaced of test"


View the status of machines on the network


#! /Bin/bash

Network = "192.168.1"

For sitenu in $ (SEQ 100)

Do

(Ping-C 1-W 1 $ {network }. $ {sitenu} &>/dev/null & Echo $ {network }. $ {sitenu} Up | echo $ {network }. $ {sitenu} Down )&

Done


& If the frontend operation is successful, continue the operation

| If the previous operation fails, the Operation will continue.

It can be translated into, if 1, then 2, otherwise 3


Principle () & put the task into the background; every Ping is very slow,-W delay of 1 second

When wait is placed at the end of the script, it will wait until all sub-processes are finished.


Netstat-TNP | grep service process to list open ports and services


Statistics on hard disk usage

DF-H disk free disk usage

Du-SH usage file and directory size

Du-a dir recursively displays the disk usage of all files


Search for large files

Find.-Type F-exec Du-K {}\; | sort-NRK 1 | HEAD

. Current Directory

Sort-NRK-N is sorted by Numerical Value

-R Reverse Order

-K: select the region for sorting.

-T specifies the delimiter

-F case-insensitive

-B. Ignore the leading space characters in each line.


Password issues in the script:

Install two RPM packages to download the http://download.csdn.net/detail/wang7dao/4416172

# Rpm-ihv expect-5.43.0-8.el5.i386.rpm

# Rpm-ihv expect-devel-5.43.0-8.el5.i386.rpm


#! /Usr/bin/CT-F

Set Password 123456

# Download

Spawn SCP [email protected]:/root/a. wmv/home/yangyz/

Set timeout 300

Reset CT "[email protected]'s password :"

Set timeout 300

Send "$ password \ r"

Set timeout 300

Send "Exit \ r"

CT EOF


# Upload

Spawn SCP/home/yangyz/ABC. SQL [email protected]:/root/test. SQL

Set timeout 300

Reset CT "[email protected]'s password :"

Set timeout 300

Send "$ password \ r"

Set timeout 300

Send "Exit \ r"

CT EOF


#: Ct a. Sh



Http://down.51cto.com/data/1866892



Common shell symbols:



Let's first write a simple script and then explain the meaning of each variable.

 

# Touch variable

# Vi variable

 

The script content is as follows:

 

#! /Bin/sh

Echo "number: $ #"

Echo "scname: $0"

Echo "First: $1"

Echo "Second: $2"

Echo "argume: [email protected]"

Echo "show parm list: $ *"

Echo "show process ID: $"

Echo "show precomm stat: $? "

Save and exit

 

Grant the script execution permission

 

# Chmod + x variable

 

Execute scripts

 

#./Variable AA bb

Number: 2

Scname:./variable

First: AA

Second: bb

Argume: AA bb

Show parm list: AA bb

Show process ID: 24544

Show precomm stat: 0

 

The displayed result shows:

 

$ # Indicates the number of parameters passed to the script.

$0 is the script name.

$1 is the first parameter passed to the shell script.

$2 is the second parameter passed to the shell script.

[Email protected] is a list of all parameters passed to the script.

$ * All parameters passed to the script are displayed as a single string. Unlike location variables, there can be more than 9 Parameters

$ Is the ID of the current process running the script

$? Is to display the exit status of the last command, 0 indicates there is no error, other indicates there is an error



Three methods for calling another script in shell:


1 fork/directory/script. Sh if the shell contains the execution command, the sub-command does not affect the parent-level command. After the sub-command is executed, the parent command is executed.

(The Sub-command environment variable does not affect the parent level)

A sub-shell call script will be opened during running.



2 exec/directory/script. Sh



3 source/directory/script. sh: Execute the sub-level command and continue to execute the parent-level command. At the same time, setting the environment variable for the sub-level will affect the parent-level environment variable.


(The variables and environment variables declared in the called script can be used in the main script)



Note:

1 ^ # Start #

^ $ Indicates space

[[: Space:] exact Space Representation


2 set-X: trace and execute the shell execution.


3 CP $ File/tmp/$ file-'date + % F' copy the file and rename it at the current time


4. display the username, ID, and shell

Echo "User: $ username ID: $ uid shell: $ shell"

Then assign values to the variables respectively.


When Filtering

A: You can use the grep keyword.

B: Head-6 file | row number of tail-1



Cut-D:-F 3

ID-GN Tom: displays the group name of the Tom user




8 Important


List Generation Method


A provides/etc/fstab/etc/inittab one by one.

B. Use the wildcard for file in/var /*

C. Generate a list using commands (usually relative paths)

#! /Bin/bash

For file in 'ls/var'; do

File/var/$ File

Done

D. Generate a digital sequence

{}:{ Start number... end number} example {1 .. 100} indicates all positive integers from 1 to 100

'Seq [start number] [step size] [end number]'

For example, seq 1 2 100 indicates that the output is in two units from 1 to 100.



8. A for loop reads the number of cycles one by one according to the number of variables.



9 arithmetic operation implementation method: expression: Operation Method

$ [Expression] example: Echo $ [$ A + $ B] -------- common

$ (Expression) Example: $ ($ A + $ B ))

Let expression example: Lete = $ A + $ B echo $ E

Expr expression example: F = 'expr $ A + $ B'


10 COUNT = 0


Count = $ [$ count + 1]


Echo "total file: $ count" // total file size



11 'seq 1 2 100 'list with an odd number

'Seq 2 2 100 'list with an even number


12 idsum = the sum of 0 IDS

Premise of evensum = 0 sum of odd numbers // variable



Idsum = $ [$ idsum + $ I] $ I is a variable number // variable entity

+ 1 is an incremental change


Echo "$ idsum" shows the sum of variables // the result of the variable




Shell temporary notes

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.