Bash Learning Notes

Source: Internet
Author: User

========================================================================
Reference: http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts.php
========================================================================


1. $*, $@, $#, $, $, $?


The script name is called test.sh Three: 1 2 3
After running test.sh 1 2 3
$* is "1 2 3" (enclosed in quotes)
$@ for "1" "2" "3" (wrapped separately)
$ #为3 (number of parameters)
$: Script Full Name
$: First parameter


$ usage: If you find that the command on the previous line is executing incorrectly, the script terminates execution
If [$?-ne 0];then
Fatal_log "Fail execute!"
Exit 1
Fi




2. datefmt= ' Date +%y%m%d '
Echo $datefmt #20141209

datefmt= ' Date +%y%m%d-d '-1 day '
Echo $datefmt #20141208

3. Awk-f "" ' Nr==fnr{a[$1]=1}nr>fnr{if (a[$1]) {print $1,$2, "1";} Else{print $1,$2, "0";}} ' A.txt B.txt

which

A.txt:

Guomei
Nginx
Hello
World


B.txt:

Nginx
Hello
World

Results:

Nginx 1
Hello 1
World 0

4. Awk-f "' $ ~/name=dash/{print $} ' 20141001.log

Usage instructions


5. Statistics the sum of the second column (one column, two Columns tab split)

ABC 12

DDD 234

XYZ 87

Cat Test.txt | Awk-f "T" ' Begin{sum=0}{sum+=$2}end{print sum} '


6. Press the second column to go heavy

awk ' {!a[$2]++} '

Reference: http://bbs.chinaunix.net/thread-1837787-1-1.html

Principle:

awk syntax: awk ' pattern{action} ', action defaults to print

So the above statement equates to: awk '!a[$2]{print $0;a[$2]++} '


7. Find two file difference sets

Grep-f-v-f B.txt A.txt | Sort | Uniq

It's a-b.

Reference: http://blog.csdn.net/autofei/article/details/6579320


8. $var, ${var}, ${#var},$ (arithmetic expression)

$var, ${var} returns the value of the variable, but ${var} to ambiguity

${#var} return length

$ ((1+2*5)) return 11


9 du-s./* | Sort-nr

Lists the file (folder) size in the current directory, descending order, rather than recursively calculating the file size


10.shell can you read one row of the contents of a file and then split the read line and put it in the array

Http://bbs.chinaunix.net/thread-4117251-1-1.html


11. In the first column + The second column key, calculates the third row and

Tom Kobe 13

Jack John 22.

Tom Kobe 57

Cat File.txt | Awk-f "" ' {a[$1 ' "" $2]+=$3}end{for (i in a) {print I ' a[i]} '



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.