1. Find the number of times "Linux" appears in the file sample.txt
#! /Bin/bash # How many "Linux" in linux.txt? Grep-o "Linux" linux.txt | WC-lgrep-c "Linux" linux.txt
2. Retrieve the first column (User Name) of/etc/passwd and use "the N account is $ USR" to display each user name. N indicates the number of rows, where/etc/passwd uses ":" As the Separator
#! /Bin/bash # Find the first column of/etc/passwdawk-F: '{printf ("the % d account is % s \ n", NR, $1) '}/etc/passwd
'Before {And} cannot be replaced with \; otherwise, an error will occur. Nr is the variable of the number of records that awk comes.
Awk Baidu Encyclopedia: http://baike.baidu.com/view/209681.htm
3. display the file content in the line
#! /Bin/bash # print odd rowcat test.txt | awk '{If (NR % 2 = 1) Print $0 }'
If you want to delete the content of this file through the line, you can redirect the output and delete the original file.
4. delete files of unspecified dates in the current directory
#! /Bin/shls-1 | while read linedomonth = 'echo $ Line | awk '{print $6} ''day = 'echo $ Line | awk' {print $7 }' time = echo $ Line | awk '{print $8}' file = 'echo $ linr | awk' {print $9} 'If [$ month = "rightmonth"-A $ day = "rightday"-A $ time = "righttime" thencontinueelserm $ filrfidone
It is best not to try it easily. I accidentally deleted all the script files and couldn't find them in the recycle bin. It should be a black hole ..
5. Mount/detach a USB flash drive to/mnt/USB
# Fidsk-L # VM-> snapshot-> Find the USB flash disk # Cat/proc/partitions to display the file system CD/mntmkdir usbsudo Mount/dev/sdb1. /USB # mount the CD usbls-LCD .. umount. /USB # uninstall
6,
WriteProgram, Please1 + 2 + 3... nAnd requirementsNInput from Command Line
#! /Bin/Bashi = 1sum = 0 while [$ I-Le $1] dosum = $ ($ sum + $ I) I = $ (I + 1 )) doneecho "the sum is $ sum"
7. Design a shell program, input a string, and judge whether the string is a return string.
#! /Bin/bash # determine whether the input string is a return string, complexity O (N) echo "Enter string" read strlen = 'echo $ STR | WC-C 'len = $ ($ len-1) L = $ ($ Len/2 )) CTR = 1 flag = 0 while [$ CTR-Le $ l] DOA = 'echo $ STR | cut-C $ CTR 'B = 'echo $ STR | cut-C $ Len 'If [$! = $ B] Then flag = 1 fictr = $ ($ CTR + 1) Len = $ ($ len-1 )) doneif [$ flag-EQ 0] Then ECHO "the input string is the echo string" else echo "Are you kidding? DS, this is not the input string "fi
zeroclock is original in this article, but it can be reproduced because we are brothers.