Document directory
- Use the top script to retrieve the CPU of a process:
- Add User:
- IP address access statistics:
- Calculate the sum of 2 numbers
- Text Analysis
- File Sorting
- Print the swap partition size of the Local Machine
- Output the time used to create 20000 directories on the local machine
- Print the current sshd port and process ID
- Print the number of executable files that can be used by the root user
- Compile all. c files in the current directory:
- Change the extension of all files in a directory to Bak.
Space Management your location: 51testing software testing network» plight» log sharing friends ~~ If you want to discuss it, you can leave MSN ~~~ It is hoped that the Group of Friends will frequently raise questions and solve them together to jointly improve some common pen questions of shell programming.
1. UseShellProgram to determine whether a file is a block or character device file. If it is copied to the/dev directory.
#! /Bin/bash
# 1.sh
# Determine whether a file is a character or a block device file. If it is copied to the/dev directory
# File executable: chmod 755 1.sh
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
Echo-e "the program will judge a file is or not a device file. \ n"
Read-P "input a filename:" filename
If [-B "$ FILENAME"-o
-C "$ FILENAME"]
Then
Echo "$ filename is a device file"
& CP $ filename/dev /&
Else
Echo "$ filename is not a device file"
& Exit 1
Fi
----------------------------------------------------
2. Design a shell program, add a new group named class1, and add 30 users to the group. The username format is stdxx, where xx ranges from 01 to 30.
#! /Bin/bash
# 2.sh
# Design a shell program, add a new group named class1, and add 30 users to the group. The username format is stdxx, where xx ranges from 01 to 30.
# File executable: chmod 755 2.sh
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
Groupadd class1
For I in {9901... 9930}
Do
Xx = 'echo
$ I | SED's/99 // g''
Useradd-G class1 STD $ XX
Echo STD $ XX | passwd STD $ XX -- stdin
Echo-e "User STD $ XX password is STD $ XX"
>/Root/newuser.txt
Done
-------------------------------------------
3. Write a shell program to automatically delete 50 accounts. The account name is stud1 to stud50.
#! /Bin/bash
# 4.sh
# Compile a shell program to automatically delete 50 accounts. The account name is stud1 to stud50.
# File executable: chmod 755 3.sh
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
I = 1
While [I-Le 50]
Do
Let I ++
Userdel
-R stud $ I
Done
---------------------------------------------
4. A system administrator must repeat each day.WorkPrepare a solution according to the following requirements:
(1) Delete All subdirectories and all files under the/ABC directory;
(2) From ~ At pm, all data in the first domain of each line in the X1 file under the/XYZ directory is read to the bak01.txt file under the/backupdirectory;
(3) archive and compress all directories and files under the/data directory as files at every Monday: backup.tar.gz;
(4) uninstall the CD-ROM of the IDE interface at PM (assuming the device name of the CD-ROM is HDC );
(5) Start the instance before a.m.
In Vim/etc/crontab, add the following content:
50 16 *** rootrm-RF
/ABC/* 2> & 1 &
00 8-18 *** root CAT/XYZ/X1 | awk
'{Print $1}'>/backup/bak01.txt 2> & 1 &
50 17 ** 1 root CD/data; tar-zcvf backup.tar.gz * 2> & 1 &
55 17 *** root umount/HDC 2> & 1 &
5) Start the instance before a.m. --> I don't quite understand what it means. I don't know if it is set to start the instance before a.m, you don't need to start it after.
Run the following command.
Chkconfig -- level 2345 crond on
---------------------------------------------
5. Design a shell program, back up and compress all the content in the/etc directory on the first day of every month, store it in the/root/bak directory, and file name
The format is yymmdd_etc. yy is year, mm is month, and DD is day. The shell program fileback is stored in the/usr/bin directory.
Vim/usr/bin/fileback. Sh
#! /Bin/bash
# Fileback. Sh
# File executable: chmod 755 fileback. Sh
Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ /Bin
Export path
Filename = 'date
+%Yymm1_d'_etc.tar.gz
CD/etc/
Tar-zcvf $ filename *
MV $ filename/root/bak/
------------------------------------------------------
Vim/etc/crontab
** 1 ** root./fileback. Sh &
------------------------------------------------
6. What should a common user do to regularly back up/user/backup to the/tmp directory at 00:00 every Sunday?
You can use contab-E, which is similar to the above question.
7. Design a shell program and create 50 directories under the/userdata directory, that is, user1 ~ User50, and set permissions for each directory.
MediumOthersThe user has the following permissions: read; the object owner has the following permissions: read, write, and execute; and the Object owner's group has the following permissions: read and execute.
#! /Bin/bash
How to remove the same words in two files. |
The content of file 3. Sh below
I have answered this question. Thank you for your advice.
> Lwp1
For a in $ (Cat)
Do
Echo $ A> lwp1
Done
> Lwp2
For B in $ (Cat B)
Do
Echo $ B> lwp2
Done
> Comf
For C in $ (cat lwp1)
Do
For D in $ (cat lwp2)
Do
If ["$ C" = "$ D"]
Then ECHO $ C> comf
Fi
Done
Done
> Ptf1
> Ptf2
Diff lwp1 comf | grep "\ <" | TR "<" "\ 0">
Ptf1
Diff lwp2 comf | grep "\ <" | TR "<" "\ 0">
Ptf2
Echo "ptf1 ---- >>>"
Echo "ptf2 ---- >>> B"
Question:
1. Extract the rows that contain "warning" or "Fatal" and do not contain "ignor" from the. log file, and then extract the 5th fields separated ":".
2. Write a script and only want to perform simple subtraction. A prompt is required to enter the variable.
3. Read the shell script (explain the functions executed below). Pick out the errors in the following program or script and describe the errors.
#! /Bin/bash
# Monitor whether the Point Port of cpuser is normal
LOGNAME = "/home/FORUM/log/lpointlog. WF"
Flagfile = "/home/FORUM/log/lognum.txt"
Lodnum = sed-n "1, 1 P" $ flagfile
Newnum = WC-L $ {LOGNAME}
Echo $ newnum> $ flagfile
Totalnum = expr $ newnum-$ oldnum
Tail-N $ totalnum $ LOGNAME | grep "point_thread
Warning"
If [$? = 0]
Then
Mail-s "cpuser point" port exception. Please handle it! "Test@aa.com </dev/null
FI>
Answer:
1.
#! /Bin/bash
Awk-F: '{if ($0 ~ /(Warning | fatal )/&&
$0 !~ /Ignor/) {print $5} 'a. Log
2.
#! /Bin/bash
Read-P "input a num :"
Num1
Read-P "input another num:" num2
Result = 'expr $ num1-$ num2'
# Let "result = num1-num2"
# Let result = num1-num2
Echo $ result
~
3.
Use reverse quotation marks to replace the command line. If [$? = 0] It should be written as if [
$? = 0], used to determine whether the last command was successfully executed;
The last line should be>/dev/null, And the last line after the fi is removed.
And write scripts. You can use shell and Perl. Find 100 files starting with ABC in the/tmp directory, and save the first line of these files to file new.
2And write scripts. You can use shell and Perl. Save all rows in file B but not in file a as file C and count the number of lines in file C.
Answer:
1.
#!/bin/sh
forfilenamein`find/tmp-typef-name"abc*"|head-n100`
do
sed-n'1p'$filename>>new
done
Or
#!/bin/bash
#for filename in `find ./ -type f -name "*.sh" |head -n 5`
for filename in `ls *.sh |head -n 5`
do
read line1<$filename
echo $line1>>new
done
Or
Find/tmp-type F-name "ABC *" | head-N 100 | xargs head-Q-N 1> New
2.
Grep-vxff a B | tee c | WC-l
(Sort a B | uniq-u | tee c | WC-l no !!!)
Use the top script to retrieve the CPU of a process:
#/Bin/sh
Max_cpu = 0
Avg_cpu = 0
Total_time = 1
Process = $1
Interval = $2
# Check the parameters
If [$ #-ne 2]; then
Echo "Usage: $0 processname interval"
Exit
Fi
Logfile = "per.txt"
Echo "'date'"> $ logfile
While sleep $ Interval
Do
Top-D 1-N 1 | grep
$ Process | grep-V grep | awk '{print $9 "\ t" $10}' >>$ logfile
Done
Add User:
#/Bin/bash
Groupadd-F class1
For I in {9909... 9911}
Do
Xx = 'echo $ I | SED's/99 // g''
Useradd-G class1 STD $ {XX}
Echo STD $ {XX} | passwd STD $ {XX} -- stdin
Echo-e "User STD $ {XX} passwd is
STD $ {XX} ">>/ root/newuser.txt
Done
Exit 0
Note that there should be no spaces before and after the equal sign: xx = 'echo $ I | SED
'S/99 // g''
If there are characters before and after the variable, it must be braces.
IP address access statistics:
Analysis of Apache access requirementsLogsTo find the top 100 IP addresses that access the page. The log size is around 78m. The following is an Apache access log excerpt.
202.101.129.218--[26/MAR/2006: 23: 59: 55
+ 0800] "Get/online/stat_inst.php? PID = d065 HTTP/1.1 "302
20-"-" "-" Mozilla/4.0 (compatible; MSIE 6.0;Windows
NNT 5.1 )"
#
Awk '{print $1}' log | sort | uniq
-C | sort-r | head-N10
5 221.224.78.15
3 221.233.19.htm
1 58.63.148.135
1 222.90.66.142
1 222.218.90.239
1 222.182.95.155
1 221.7.249.206
1 221.237.232.191
1 221.235.61.109
1 219.129.183.122
Here is a question about why sort is required before uniq is used.
Calculate the sum of 2 numbers
#/Bin/bash
Typeset first second
Read-P "input the first number :"
First
Read-P "input the second
Number: "second
Result = $ [$ first + $ second]
Echo "result is: $ result"
Exit 0
Text Analysis
Retrieve the number of times the shell appears in the password
Result of the first method:
4/bin/bash
1/bin/Sync
1/sbin/halt
31/sbin/nologin
1/sbin/Shutdown
Result of Method 2:
/Bin/Sync 1
/Bin/bash 1
/Sbin/nologin 30
/Sbin/halt 1
/Sbin/shutdown 1
Answer:
Cat
/Etc/passwd | awk-F: '{if ($7! = "") Print $7} '| sort | uniq-C
CAT/etc/passwd | awk-F: '{if
($7! = "") Print $7} '| sort | uniq-c | awk' {print $2, $1 }'
File Sorting
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,250
100 Jason Smith $5,000
200 John Doe $500
300 Sanjay Gupta $3,000
Answer: Join employee bonus | sort-K 2
Print the swap partition size of the Local Machine
Processing result:
Swap: 1024 m
Free-M | sed-n'/SWAP/P' | awk
'{Print $2 }'
Free-M | sed-n's/swap: \ * \ ([0-9] * \). */\ 1/P'
Output the time used to create 20000 directories on the local machine
Processing result:
Real 0m3. 367 s
User 0m0. 066 s
Sys 0m1. 925 s
Answer:
#
Time for I in {1 .. 2000}; do mkdir/root/Neil $ I; done
Real 0m6. 200 s
User 0m1. 128 S
Sys 0m4. 710 s
Print the current sshd port and process ID
Processing result:
Sshd Port & amp; PID: 22 5412
Answer: netstat-anp | grep sshd |
Sed-n's /. *: \ ([0-9] * \) \. * \ ([0-9] * \) \/sshd/\ 1 \ 2/P'
Print the number of executable files that can be used by the root user
Processing result:
Root's bins: 2306
Echo "Root's bins: $ (find./-type F |
Xargs LS-L | sed '/-. X/P' | WC-l )"
Root's bins: 3664
Compile all. c files in the current directory:
For
File in *. C; do echo $ file; gcc-o $ (basename $ file. c) $ file; sleep 2; done> compile 2> & 1
Change the extension of all files in a directory to Bak.
For
I in *. *; Do MV $ I $ {I %. *}. bak; done