1. Retrieve the number of times the shell appears in the/etc/passwd file
Problem: The following is part of a/etc/passwd file. Obtain the shell and count the number of times. Shell refers to the following/bin/bash,/sbin/nologin, for example, the following/bin/bash appears 12 times, /sbin/nologin appears three times.
hyn:x:525:500::/home/hyn:/bin/bashljlxx:x:526:500::/home/ljlxx:/bin/bashlzj:x:527:500::/home/lzj:/bin/bashwfly:x:528:500::/home/wfly:/bin/bashsquid:x:23:23::/var/spool/squid:/sbin/nologinwyj:x:529:500::/home/wyj:/bin/bashqemu:x:107:107:qemu user:/:/sbin/nologinradvd:x:75:75:radvd user:/:/sbin/nologindungbee:x:530:500::/home/dungbee:/bin/bashmysql:x:27:27:MySQL Server:/var/lib/mysql:/bin/bashscidb:x:531:531::/home/scidb:/bin/bashpostgres:x:532:532::/home/postgres:/bin/bashcrane:x:533:533::/home/crane:/bin/bashtest:x:534:534::/home/test:/bin/bashhguser:x:535:535::/home/hguser:/bin/bash
Reference answer:
cat /etc/passwd|awk -F: '{print $7}'|sort|uniq -c
Resolution:Use awk to split the content based on the colon and print the output of the 7th column after the split, that is, the column where the shell is located. Call the sort command to sort and use uniq-C to count the number of times each shell appears. Ii. File SortingProblem:Employee ID and name are recorded in the employee file
employee.txt:100 Jason Smith 200 John Doe 300 Sanjay Gupta 400 Ashok Sharma
Bonus file records employee ID and salary
bonus.txt:100 $5,000 200 $500 300 $3,000 400 $1,250
Merge the two files and output them as follows:
Processing result:
400 ashok sharma $1,250100 jason smith $5,000200 john doe $500300 sanjay gupta $3,000
Reference answer:
paste employee.txt bonus.txt | awk '{print $1,$2,$3,$5}'|tr '[:upper:]' '[:lower:]'|sort -k 2
Resolution:Several commands are used here, including paste, awk, TR, and sort. The paste command is used to merge the same data of multiple files. For example, the preceding two files, employee and bonus, call paste and merge them
100 Jason Smith 100 $5,000 200 John Doe 200 $500 300 Sanjay Gupta 300 $3,000 400 Ashok Sharma 400 $1,250
The paste command can use-D to specify the symbols added to the merge operation. For example, paste-D: employee bonus turns the result into something similar to 100 Jason Smith: 100 $5,000. The default merge symbol is the tab symbol. For more Paste commands, see http://snilwarrior.blog.51cto.com/680306/144462 /. Awk is used to extract the remaining four columns except the tab symbol. The TR command is used to convert all uppercase characters in a string to lowercase characters. For more options, see configure. The sort command sorts the characters. Sort-K 2 indicates sorting by file 2nd fields. Here, the second field is name, so it is sorted by name in ascending order. To sort data in descending order, use sort-K 2R. For more sort commands, see http://www.360doc.com/content/10/0925/15/1107705_56263541.shtml. 3. Print the local swap partition sizeProblem: Print the swap partition size of the local machine. The output is as follows:
Swap:1024M
Reference answer:
top -n 1|grep Swap|sed 's/k.*//'|awk '{print $1,$2/1000"M"}'
Resolution:
The top command displays the usage of system resources.-N 1 indicates that only one call is performed. Grep swap: select the row where SWAp is located. The result of grep command execution may be as follows:
Swap: 16779884k total, 0k used, 16779884k free, 3268200k cached
The SED command is used for regular expression matching of strings. Here the replacement parameter is used to replace the 1st k characters and the subsequent characters with spaces. After SED is executed, the result is:
Swap: 16779884
The output content of the awk command. Divide the second parameter by 1000.
Iv. User cleanup
Problem:Clear all users except the current login user.
Reference answer:
kill $(who -u|grep -v `whoami`|awk '{print $6}'|sort -u)
Resolution:Who-u displays all current users. Grep-V Selects all users other than the current logon user. Awk prints the user process ID. Sort-u deletes the same row. Finally, use the kill command to terminate the service. 5. Baidu script interview question 1) write the script to implement it. You can use shell, Perl, and so on. Find 100 files starting with ABC in the/tmp directory, and save the first line of these files to file new.Reference 1:
#!/bin/shfor filename in `find /tmp -type f -name "abc*"|head -n 100`dosed -n '1p' $filename>>newdone
Resolution:First, the find command is used, where-type
F indicates selecting a common file.-Name is used to set the file name. Second, the head-N 100 command is used to retrieve the first 100 items. Third, sed-N '1p' is used to retrieve the first line of the object content. Fourth,> New indicates appending to the file new.
Reference 2:
find /tmp -type f -name “abc*” | head -n 100 | xargs head -q -n 1 >> new
2) write scripts for implementation. You can use shell, Perl, and so on. Save all rows in file B but not in file a as file C and count the number of lines in file C.
Reference answer:
grep -vxFf a b | tee c | wc -l
Resolution:Grep-V indicates that no matching row is selected,-F indicates that the matching mode is split by row,-f a indicates that the matching mode comes from File A, and finally indicates the target file B. That is, the grep command selects a line that does not exist in A from B. Tee C command to create file C, WC-l command to count the number of lines.