Several shell assignments in Linux

Source: Internet
Author: User

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.

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.