Shell technical points

Source: Internet
Author: User
Shell technical points-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1. Learning tips

Learning shell is mainly used, but reading books is useless.

2. single quotation marks, double quotation marks, Parentheses, curly brackets, and reverse quotation marks

All metacharacters in single quotes lose special meanings (including \). metacharacters except for the variable field ($) and command field (') lose special meanings, therefore, double quotation marks are generally used for reference.

Curly braces {} are used to differentiate the variable name and surrounding text: echo $ {file} and $ file1 find the variable file, file1

Command substitution format: the reverse quotation marks enclose a command like 'cmd', which is equivalent to $ (command): ls-l 'Find.-type F'

3. Fetch rows

Sed-n "3" p file: 3rd rows

Sed-n "1, 3" p file: 1st to 3 rows

Sed-n "1, $" p file: 1st to the last row

Sed-n "1, $ num" p file: 1st to num rows

Sed-n "\ $ p" file takes the last line

Sed-e'1! G; h; $! D'file display in reverse mode

4. Add/replace sed:

Sed "/xmdh/a \ daoyou" file attaches daoyou (with line breaks) to the end of the row containing xmdh)

Sed's/$/daoyou/'file append the end of each line to daoyou (same line)

Sed '/test/s/$/daoyou/' file attaches the end of the test row to daoyou (Same row)

Sed '10s/$/daoyou/'file append the end of row 10th with daoyou (Same row)



Sed "s/xmdh/daoyou/g" file replaces xmdh with daoyou

Sed "s/xmdh/daoyou/; G" file replaces xmdh with daoyou and adds a line feed.

Cat userlog | sed-n'/xmdh/w test.txt'contains xmdhand is written to test.txt.

5. Search:

After vi file :/\ /

6. display the row number:

Awk '/dhshunde/{print NR, $0}' userlog or grep-n dhshunde userlog display the row number and content containing dhshunde

Cat userlog | sed-n'/erptest/= 'display the row number containing erptest

Cat userlog | sed-n'/xmdh/P' | sed-n' $ P' displays the last line containing xmdh

7. Use variables in awk

/Bin/cat/etc/ppp/chap-secrets | grep $5 | awk '{print logouttime "\ t", "username:" $1 "\ t ", "logout" "\ t", "data:" datasize} 'logouttime = "'/bin/date'" datasize = "$ size" >>$ pptplogdirectory/userlog (note: size is defined earlier)

8. Find usage

Note: The f I n d command passes all matching files to e x e c for execution, while the x a rg s command only obtains part of the files, not all of them at a time, therefore, exec has a length limit, so there cannot be too many files; otherwise, an overflow error will occur, while xargs does not

Find.-mtime-1? Print and the file smaller than one day

Find.-perm 755? Print: displays files with 755 attributes.

Find.-size + 000000c? Print to search for files larger than 1 MB

Find.-type f-exec ls-l {}\; find the file and display the list (Note: There is a space between {} And \, and finally ;)

Find.-type f-exec rm {}\; find and delete the file

Find.-type f-print | xargs ls? L view the file and display the list

Find/\ (-perm-4000-o-perm-2000 \)-type f? Print to search for SUID and SGID files

9. send messages to login terminal users

Echo "hello I am jiangdaoyou" | tee/dev/pts/2 (tty can view its own terminal number), equivalent to: write root pts/2 and then input: hello I am jiangdaoyou and Ctrl + D

10. awk BEGIN and END

Add the column name in the file header:

Cat userlog | awk 'in in {print "Time username \ n -----------------"}; {print $4, $7 }'

Time username

------------------------------

15:19:28 username: xmdh

15:20:00 username: xmdh

The end description "end of report!" will be added based on the above !!!!"

Cat userlog | awk 'in in {print "Time username \ n -----------------" }{ print $4, $7} END {print "end of report !!!! "}'

Back to homepage



11. Capture/convert characters

Echo "200604211031" | cut-c9-12 get 1031

Cat test. OK | tr 'Arp ''rpm converts arp to rpm

12. Average Value

Vmstat 1 4 | awk '{print $4}' | grep-o '[0-9] *' | sed's /, // G' | awk '{total = total + $1; if (NR % 4 = 0) {print total/4}' or the following method:

Vmstat 1 4 | awk 'nr> 2 {sum + = $4} END {print sum/4 }'

13. Loop

Ls | for file in *; do echo "rpm-ivh" $ file; done

Ls | for file in $ (ls *. rpm); do echo "rpm-ivh" $ file; done
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.