Linux shell script Writing

Source: Internet
Author: User

Hello everyone

This is a self-reading, learning shell programming, the work of a large number of applications, especially awk, SED, grep-based. Hope to be useful to everyone

You can see the central location directly.

"Data"

Export Path=/mall/jdk/jdk1.7/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

Export LANG=ZH_CN. GB18030


start= ' Date +%s-d ' 2011-11-28 15:55:37 "'

end= ' Date +%s-d ' 2011-11-28 15:55:52 "'

echo $ (($end-$start))

Date "+%y-%m-%d%h:%m:%s"

Date-d ' +1 day ' + '%d ' Today +1

Date-s 20100405

Date-s 14:31:00


1. Late last month

Date-d ' Date +%y%m01 ' "-1 day" +%d

2. Forward

Date-d-' Date +%d ' Day +%d

3. Conditions

If [' Date +%d-d tomorrow ' = = 01];

4. Last month

Date-d Last-month

Date-d -1month

Date-d '-1 month ' + "%m"

5, next month

Date-d Next-month

Date-d 1month

6. Early this month

Date-d 1 ' Date +%b '

Early last month

Date-d 1 ' date-d last-month +%b '

Specify month

Day-d 1May


echo ' Date +%d ' | xargs-i{} date-d '-{} day ' +%y%m%d How Linux gets last month's end date



Sed-i "3s/.*/$table/g" conf/run.sql

Table=t_sms_mt_log_ ' date + "%m" ' this month, then the beginning of the month 1th is still counted last month

Date-d '-1 day ' + '%m ' this month, real this month

Subject= "Daily verification code issued quantity statistics _ ' date-d '-1 day ' +"%m%d ""


SELECT * from T_sys_user where user_code= ' duanzz ';


Update t_sys_user set terminal_id= ' 13730000000 ' where user_code= ' duanzz ';



Set Fileformat=unix


Intercept-Replace of string

Echo ${stringa:1} from the second place has been all

Echo ${stringa:2:5} intercepts 5 bits from the third position.


echo ${stringz: (-4)} Intercept from right to left

echo ${stringz:-4} Intercept from right to left, same as above

A=${STRINGA%%F} Remove the variable Stringa suffix F.

echo ${stringz/abc/xyz} variable in string ABC replaced by XYZ

echo ${stringz/#abc/xyz} with ' XYZ ' to replace the beginning ' abc '.

Echo ${STRINGZ/%ABC/XYZ} Replaces the end of ' abc ' with ' XYZ '.


You_id=${user}-on-${hostname}

you_id= $USER \-on-$HOSTNAME

echo $you _id root-on-db1 effect. Mixed output of variable and string

val=${1##+ (0)} # uses local variables to match the longest consecutive one or more 0.



Reference: ${backup_dir}/$DATE

#备份的变量:

Backup_dir=/home/mysql/mysql_bak

Date= ' date-d "+%y%m%d%h:%m:%d" '

Yes_time= ' date-d Yesterday "+%y%m%d" '

Mkdir-p/opt/chen$date\nailong Chen20160608nailong

Date-d "2 day Ago" + "%y-%m-%d" Yesterday date

date-d "1 week Ago" + "%y-%m-%d" last Sunday

Date +%y%m%d%h%m


The single quotation mark (') operation is basically the same as double quotes, but it is not allowed to reference a variable because the special meaning of $ is closed. In single quotation marks,

Any special characters are interpreted literally, except '. So the single quotation mark ("full reference") is a more than double quotation mark ("partial citation").

") more restrictive reference methods.



' Single quote mask all special characters

"" Double quotation marks mask some special characters


"Eval"


Two scans


Test.txt content: Hello Shell world!


Myfile= "Cat Test.txt"


(1) Echo $myfile #result: Cat test.txt


(2) eval echo $myfile #result: Hello Shell world!


"Echo"

ECHO-E handles special characters and interprets special characters. Effective special characters

Echo-n Non-newline output

Echo-e "A\NDDDD"//Automatic line wrapping

echo $ (ls-al) do not wrap echo "$ (ls-al)" Line wrap


bash$ Echo hello\!

Hello!

bash$ echo "hello\!"

hello\!

bash$ echo-e X\ty

Xty

bash$ echo-e "X\ty"

X y


echo "Chen" >> file: Append Chen content to file, without overwriting

echo "Chen" > file: Covered!!!



[A-za-z0-9]


########################  (( ... )) Structures can be used to calculate and test the results of an arithmetic expression.

#!/bin/bash

((5 > 4))

echo $?

echo "Exit Status of \" ((5 > 4)) \ "is $?."


"Input from keyboard-more than read"

Command << Word

Any input

Word



Read-p "Your command:" CMD

until [$CMD = = ' Q ']; Do

! Which $CMD && echo "wrong command" && read-p "Input again:" CMD && continue

Command= ' which $CMD | Grep-v "^alias" | Grep-o "[^[:space:]]\{1,\}" '

BINCP $COMMAND

echo "Copy $COMMAND finished."

Read-p "Continue:" CMD

Done


"Output to Screen"

Cat << Word

-----------

Name ID

Word

"Call in script" source _function.sh

"Shell function"

Hello ()

{

Echo ' Date '

}

Hello # # #这儿直接写hello

' Anti-quote allows you to assign the output of a shell command to a variable

today= ' Date +%y%m%d '

echo "Today is ' date +%y%m%d '"


Sleep 5 #过五秒再执行



"BaseName"

$ basename/tmp/test/file.txt

File.txt

$ basename/tmp/test/file.txt. txt

File


"DirName"

Dirname/var/log/messages get the path to a file or folder (previous level)

/var/log


Array

Var= ("123" "456" "789")

Var[1]=123

var[2]=456

echo "$var [1], $var [2]"

#


echo $? Condition is true, output 0, condition no, Output 1

CP 1.txt/opt/apps/2.txt >/dev/null 2>&1

Status=$?

echo $? #如果cp这个命令成功的话, output 0, unsuccessful, output 1


: The colon is here a placeholder, {str:=expr} if the variable str is not empty, ${str:=expr} equals the value of STR, and if Str is empty, the value of expr is assigned to STR

: >> target_file If this file does not exist before, create it

if [$name = ""] If this variable is a null value

Then: # doing nothing, branching out.

If [!-S 22.txt] If 22.txt this file is empty

or if [-Z 22.txt]


[$I-eq 1] && min= $MYRAND

If [-s $name] If this variable is empty

If [-D $path] If the directory exists

If [-s $path] if the directory exists

If [-N ""] # Tests whether there are command-line arguments (NOT null).

-A file exists YES!

-F This is a generic file

-D This is a directory

-B This is a fast device (floppy disk, hard drive, optical drive, etc.)

-C This is a character device (keyboard, sound card, etc.)

-P This file is a conduit

-H OR-l this is a symbolic link

-R file is readable

-W file is writable

-X file is executable

-O Whether you are a file owner

-G file Group-id is the same as you

F1-nt F2 file f1 than file F2 new

F1-ot F2 file F1 f2 older than file




&&:and |: or!: Non-command| | comand | |: The former command is true, the latter can be executed, otherwise the latter does not perform

Comand&&comand The former is true, the latter executes, the former is false, the latter skips

Sort: sorted WC: Statistics

Cd/opt &&/1.txt: successive executions

"Find memory Usage"

[Email protected] opt]# free-m|grep Mem|awk ' {print "used\n----"}; {Print $} '

Used

----

985


"Numeric Comparison"

------------used in []

-eq values are equal.

-ne values are not equal.

-GT the first number is greater than the second number.

-lt The first number is less than the second number.

-le The first number is less than or equal to the second number.

-ge the first number is greater than or equal to the second number.

------------used in (())

<= > < >=

----------string comparison = = =


Less than, sorted by ASCII character

if [["$a" < "$b"]

If ["$a" \< "$b"]

Note that "<" needs to be escaped when used in the [] structure.


-A logic and exp1-a EXP2 if the expression Exp1 and Exp2 are true, then the result is true.

-O logic or exp1-o exp2 if at least one of the expressions Exp1 and Exp2 is true, the result is true.

If ["$exp 1"-A "$exp 2"]


and (logical)

1 if [$condition 1] && [$condition 2]

2 # Same as if [$condition 1-a $condition 2]

3 # If both Condition1 and Condition2 are true, the result is true.

4

5 if [[$condition 1 && $condition 2]] # can also.

6 # Note: && is not allowed to appear in the [...] Structure.


or (logical)

1 if [$condition 1] | | [$condition 2]

2 # Same as if [$condition 1-o $condition 2]

3 # If one of the Condition1 or Condition2 is true, then the result is true.

4

5 if [[$condition 1 | | $condition 2]] # can also be.

6 # Note | | Operator is not able to appear in the [...] In the structure.





"Grep"

Often used with Cat: cat/path/filename |grep [option]

Grep-i ignoring case

Grep-v "^#" file:grep-v "^#" FileName Output header character is not a # line

Grep-o only matching strings are displayed

When grep--color output, find the keyword with color.

grep ' [nn]* ' test1.sh

Grep-c 5 Foo file displays the line with the Foo string in the file and the top and bottom 5 lines

Grep-b 5 foo file shows Foo and the first 5 rows

Grep-a 5 foo file shows Foo and the following 5 rows


Exact match grep ': 80\b ' only matches to: 80

Netstat-nat|grep ':80\> '

Netstat-nat|grep ': 80\b '

Grep-e ' 123|abc ' filename//Find file (filename) contains 123 or lines containing ABC




"SED"


-R supports regular expressions


Sed-n ' 5p ' filename Select line 5th output

Sed-n '/^\//p '/etc/fstab sed out line with/start and print out

Sed '/^\/dev/d ' fstab find row deletions starting with/dev

Sed ' s/^l/#/g ' Fstab_bak the line that starts with L in Fstab_bak and converts l to # Plus G for global substitution


Sed-n '/^\//a hello '/etc/fstab sed out lines that begin with/and append a line: Hello goes in and print out, A's function is append, append content to File

Sed-n '/^\//a hello \ n Chen '/etc/fstab add another line Chen this is to add a line to the back, if it is preceded by a transposition I


Sed ' 3r/etc/fstab ' fstab_bak add etc/fstab content to the third line of Fstab_bak

Sed '/tmp/w/etc/tmp.txt '/etc/fstab fstab a line with TMP is saved in the/etc/tmp.txt file


Sed ' 1,3d '/etc/fstab delete First and third rows

Sed ' 1,+2d '/etc/fstab delete the first row and the following two lines

Sed-e ' 8/p '-e ' s/30/50/g ' 1.txt multiple command execution

Sed '/^f/= ' 1.txt the first character is F and outputs his line number

Sed '/chen/s/x/x/g ' passwd find the line of Chen in passwd, and in that row x changes to X

Sed-n '/^#/! {/chen/p} ' 11.txt finds the line with the first letter that is not a # and contains Chen. Print it out

Sed-i '/chen/{s/^#//g} ' 11.txt contains Chen, and the first character is #, minus #. Effect

Sed-i '/^#/d ' a.txt remove empty lines

Sed '/^chen/s/^chen.*/chennailong/' install.log to replace Chen's line with the entire line Chennailong

Sed-i "7s/.*/$table/g" Conf/run.sql replace line seventh with the value of the $table variable

Sed ' s/<!--/&chen/' 1.txt added Chen at the back of <!--

\ Translate special characters

Cat tomcat_running_state.txt |sed-r ' S/\S+/\N/1 ' The first space is replaced with a newline


Bashusers= ' echo $BASHUSERS | Sed ' [Email protected][[:space:]]@,@g '


The default command for Xargs is echo, which means that the input passed to Xargs by the pipeline will contain line breaks and whitespace, but with xargs processing, line breaks and whitespace will be replaced by spaces.


A back reference to sed;

If you want the SED command to take effect, add-I


Sed-f/path/script/etc/fstab go to this directory and select all scripts under script to process the fstab.


Sed-i ' s/^[[:space:]]//g '/etc/fastab to Fstab, the line at the beginning of the space removed [[: space]]]+ at least one space





"Awk"


~ Match Regular Expression < less than = = Not included, must be equal to

!    Mismatched regular expression!== not equal to && both sides are true | or | | ! Non-

awk ' {print $1,$4} ' Fstab_bak awk out the first column and the fourth column

awk ' {print ' \ t ' $4} ' Fstab_bak awk out the first column and fourth column, output time, middle plus space

awk ' {if ($1~/devpts/)}print{$2} ' Fstab_bak if the first column appears devpts, this prints the second column of the devpts

awk ' {if ($1~/devpts/)}print{"Good"} ' Fstab_bak if the first column appears devpts, this prints out good

awk ' {if ($1<$2)}{print $} ' Fstab_bak if the number in the first column is less than the number in the second column, output this line

awk ' {$1~/^...a/} ' Fstab_bak in the first column if there is a third character

awk ' {$1~/tmpfs|proc/} ' Fstab_bak the first column has TMPFS or proc, output

Awk-f "," ' {print $} ' q.txt with "," as a delimiter to find the second segment and output

awk ' {name=$1;belts=$4;if (belts ~/yellow/) print name "is belt" belts} ' Grant.txt

awk ' {if ($1~ "Chen") ($1= "Chennailong");p rint ' 1.txt ' in the first column, Chen appears, replaced by Chennailong

awk ' {if ($1~/chen/) {($1= "Chennailong");p rint {}} ' 1.txt ' Output all of the commands, more than one pair {} represents the output only modifies that line

awk ' {if ($1~/chen/) {print $}} ' 1.txt

awk ' {sum+=$2}; {print sum} ' 1.txt counts the sum of the second column

Ls-al | awk '/^[^d]/{print $9 ' \ t ' $ $ $ {tot+=$5} END {print ' total KB: ' tot} ' statistics the file name and length of files in this directory, plus the sum

awk ' BEGIN {split ("123#456#789", Myawk, "#")} ' 123#456#789 the field, with # as a delimiter

awk-f! ' {print substr ($3,6,11)} ' 1.txt intercept! The 6th character of the third paragraph of the delimiter and the next 11 characters including the sixth character, if it's only 6, that's 6 to the back of the line.

awk ' Sub (/chen/, Chen, $) ' 1.txt intercepts the first paragraph (the default delimiter is a space), which contains Chen, and is replaced by Chen

awk ' BEGIN {st= ' Chen is a good boy '} END {print substr (st,1,4)} ' 1.txt defines a variable called ST and then uses SUBSTR to intercept the first character and include the first character backwards 4 characters

awk ' BEGIN {st=$1} END {print substr ($1,1,4)} ' 1.txt

awk ' BEGIN {st=$1} END {print substr ($1,1,4)} ' 1.txt |sed '/st/p

awk ' {print length ($)} ' calculates the string length

num=$ (awk-f! ' {print substr ($3,6,11)} ' 1.txt) assignment ... A little surprised.

Echo $num

awk ' {print NR} ' filename count the total number of lines in this file

awk ' {print NF} ' filename Statistics the total number of domains for this file

awk ' {if (NR > 0 && $4~/chen/) print$0} ' filename printing is not a blank line, and the fourth column contains Chen's Row

Awk-f: {' Print $ '}/etc/passwd|tr "\ n" "#查找passwd里面的用户, so many users output horizontally

echo $PWD |awk-f/' {print $NF} ' displays its directory

Awk-f ' "' special characters



Who |awk ' {if ($1~/root/);p rint "\ T" "Welcome"} '

Who |awk ' {print $ "\ T" "Welcome"} '

"WC"

Wc-l | Number of rows calculated by file

wc-w | Number of words in file calculation files

Wc-c .... Number of characters

"Cut"

Cut-b 3: Take the third column (no separators)

Cat 1.txt |cut-d! -F1 to! As a delimiter, cut out the first segment of the output

Cat 1.txt |cut-d! -f2-3 to! As a delimiter, cut out the first and third segments of the output

Cut-c1 Cut-c1-5 cut by character

"Find"

Find/etc-perm 777-print

Find/etc-type |xargs-exec Ls-al {}\; -exec is used only when find out files

Find-type F

Find/etc-user Chen-print

Find/etc-size +1g-print.

Find/etc-mtime-n (How many days from now) +n (how many days ago)

Find./-name "*.txt"-delete

Find./-name "*.txt" |xargs RM-RF

Find./-name "*.txt"-exec {} RM \;

"Sort"

Sort passwd Default Sorting

Sort-t passwd

SORT-T:-k1 passwd is sorted by: delimiter, K1 is the first domain. Indicates the sort by user name.

Sort-n-K 2 This is sorted by number, which means sort by the number in the second column

Cat Access.log |awk ' {print $} ' |sort |uniq-c |sort-n-R |head-n 10

awk ' {print '} ' Access.log|sort | Uniq-c |sort-n-K 1-r|more

Sort-u (remove duplicate rows, sort) sort-r (descending) sort-r File.log-o File.log (post-order redirection)

Sort-n number. Log (character type, ascending sort)

"Head"

Head-n 101-like after sorting, statistics select the top 10 or head-10

Tail-n 10 Statistics tail-1000 Acess.log

"Uniq"

Uniq-c the number of times each duplicate line is printed: basically the number of accesses to the statistics IP-----


Uniq-d only duplicate rows are displayed, and duplicate lines show only one row



"Script Practice"-------"if Else judgment"

#!/bin/bash

echo "Give Y is OK; GEi N is no "

Read Choose

echo "Your action is $choose"

If ["$choose" = = "Y"]

Then

echo "Your choose is Yes,thanks"

elif ["$choose" = = "N"]

Then

echo "Your choose is no,we be sorry to You"

Else

echo "Take your answer in [Y]or[n]"

Fi


"If"

If ["$UID"-ne "$ROOT _uid"] # of course, use the ROOT user to run. The following is not the time to determine the root

If [-N ""] # Tests whether there are command-line arguments (NOT null).

One


If CD "$dir" 2>/dev/null; Then # "2>/dev/null" hides the error message.

Two

Similarly, the conditions in the brackets do not necessarily have to be if not, or you can use the list structure.

Var1=20

Var2=22

["$var 1"-ne "$var 2"] && echo "$var 1 is not equal to $var 2"

Home=/home/bozo

[-D "$home"] | | echo "$home directory does not exist."



"Expr"


For integer values, also for strings

Expr 10 + 10 output 20

Expr 30/3 Output 10

Expr 30 \* 3 Output 90


"Case"

Case $name in

Root) echo "Welcome Super User"

;;

Chen) echo "Welcome Chen"

;;

*) echo "Unknown Words"

;;

Esac

"For Loop"

For $name in Chen Root ni #这里面是条件, if what satisfies what

Do

Echo $name #如条件成立, the output operation

Done


For i in *.txt;do MV $i $ (sed "s/txt/sh/" <<< $i);d one to change all txt files to sh file

For I in {1..100}20.



"Until" until loop hangs continuously until the file 22.txt is deleted, and after deletion, the script goes into a normal state

#!/bin/bash

File=/opt/22.txt

Until [!-F $file]


Do


Sleep 1

Done

echo "File deleted"


While the simple loop count is small with 5, the continuation is +1. The document does not meet the small with 5 o'clock. Outputs the value of count at this time

#!/bin/bash

Count=0

While ["$count"-LT 5]

Do

Count= ' expr $count + 1 '

Echo $count

Done


"Split" file has 6 lines, cut for every 2 behavior a file, altogether cut out 3 files. XAA Xab Xac

Split-2 A.txt


Also available while the Read name keyboard input


"TR" is generally replaced or deleted for character conversion

Tr-s "[A-Z]" < a.txt to remove duplicate letters such as HOMEEEEEE to home

Tr-s "[\012]" <a.txt or tr-s "[" \ n "]" <a.txt Delete empty lines

Tr-s "[A-Z]" "[A-z]" <a.txt convert lowercase to uppercase

Tr-s "[Chen]" "[Chennailog]" <a.txt to replace Chen with chennailing


Cat 201601.csv|awk-f ', ' {print $ ' |sort|uniq-c|sort-rn|awk ' {if ($1>1);p rint-$} ' |xargs-i grep {} 201601.csv


If the first column of this row = The first column of another row, output this whole line


Export Tmout=0 terminal never expires, directly on the above will not exit


Root_uid=0 # When $UID is 0, the user has the root user's permission

LINES=50 # Default number of rows saved

E_XCD=66 # Cannot modify the directory?

E_notroot=67 # Non-root user will exit with error



Linux shell script Writing

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.