1. Find all files larger than 500M in the current directory, write them into a text file, and count the number of files.
Find./-size +500m-type F | Tee File_list | Wc-l
2. Locate 100 files starting with ABC in the Directory/TMP and save the first line of these files in the file new.
For filename in ' find/tmp-type f-name "abc*" |head-n 100 '
Do
Sed-n ' 1p ' $filename >>new
Done
3. Save all the rows in file B, but not in file a, to file C and count the number of lines in C.
GREP-XVF a B | Tee C | Wc-l
4. Determine if a file is a block or character device file, and if it is copied to the/dev directory
Read-p "Input a file:" filename
If [-B $filename-O-C $filename]
Then
CP $filename/dev/
Fi
5. Monitor every 10 minutes, monitor/usr if greater than 5G, send an email to the administrator
#!/bin/bash
While True
Do
Sleep 600
n=$ (du-s/usr | cut-f1)
If [$n-gt 5242880]
Then
Mail-s "Greater"[email protected]< ~/filename #将文件filename的内容发送出去.
Fi
Done
6. Extract the row containing "WARNING" or "FATAL" from the A.log file without "Ignor", and extract the 5th field with ":" Split
Grep-e ' Warning| FATAL ' A.log | Grep-v Ignor | Awk-f ":" ' {print $} '
7. Write a script to perform a simple subtraction operation requiring prompt input of variables
#!/bin/bash
Read-p "Input a number:" Num1
Read-p "Input Another number:" Num2
Let "num3=num1-num2"
Echo $num 3
8. Change the file name extension in a directory to bat, and then compress the files to a directory with a time file name.
#!/bin/bash
For file in $ (LS $)
Do
New_file=${file%.*}.bat
mv./$1/$file./$1/$new _file
tmp=$ (Date +%y)
Tar cvf./$tmp. Tar./$1
Done
9. Download a file from the Web and save it to the specified directory
#!/bin/bash
Url=http://rs1.bn.163.com/ent/2009/05/20_canquedege.wma
dir=~/Download
Wget-p $dir $url
10. Determine if a number is the end of the number. Prints the number of finishes between 1-1000. The number is twice times the number of the sum equal to itself. (6,28,496)
#!/bin/bash
Sub ()
{
I=1;
sum=0;
While [$i-le $num]
Do
Let "m=num%i"
If [$m-eq 0]
Then
Let "Sum=sum+i"
Fi
Let "i=i+1"
Done
Let "A=2*num"
If [$a-eq $sum]
Then
Echo $num
Fi
}
Num=1
While [$num-le 1000]
Do
Sub
Let "num = num+1"
Done
11. In the Act unit, seek the intersection of file A and file B, and set, difference set.
And:
Sort-m < (sort A | uniq) < (sort B | uniq) | Uniq
Make:
Sort-m < (sort A | uniq) < (sort B | uniq) | Uniq-d
Poor:
Sort-m < (sort A | uniq) < (sort B | uniq) < (sort B | uniq) | Uniq-u
12. Locate the file with the specified string under a folder
#!/bin/bash
For file in $ (LS $)
Do
bname=$ (grep-l $ $2/$file)
BaseName $bname
Done
Calling method:./tst bash# in folder bash to find files that contain "000".
13. Add a new group for Class1, and then add 30 users belonging to this group, with the user name in the form stdxx, where xx is from 01 to 30.
#!/bin/bash
Groupadd Class1
For i in {9901..9930}
Do
xx=$ (echo $i | sed ' s/99//');
useradd-g Class1 Std$xx-p ""
Done
14. Realize the function of automatically deleting 50 accounts. Account name is STUD1 to STUD50
#!/bin/bash
I=0
While [I-le 50]
Do
Let i++
Userdel-r stud$i
Done
15. A system administrator to do a certain amount of repetitive work every day, please follow the following requirements, to develop a solution:
(1) Delete all subdirectories and all files under the/ABC directory at 4:50;
(2) from early 8:00~ 6:00 hourly read the/XYZ directory in the X1 file, the entire data of the first field in each row is added to the Bak01.txt file in the/backup directory;
(3) Every Monday 5:50 archive and compress all directories and files in the/data directory into files: backup.tar.gz;
(4) Unmount the CD-ROM of the IDE interface at 5:55 (assuming that the device name of the CD-ROM is hdc);
(5) Start up before 8:00.
The vim/etc/crontab adds the following content:
1) * * * Root rm-rf/abc/* 2>&1 &
2) 8-18 * * * root Cat/xyz/x1|awk ' {print $} ' >>/backup/bak01.txt 2>&1 &
3) * * 1 root CD/DATA;TAR-ZCVF backup.tar.gz * 2>&1 &
4) * * * Root UMOUNT/HDC 2>&1 &
5) Start up before 8:00--this I do not understand the meaning of it, do not know whether it is 8 before the boot on the start of the above settings, 8 points after the boot does not need to start the meaning. Let's use the following command.
Chkconfig--level 2345 Crond on
16. Design a shell program to back up and compress all contents of the/etc directory on the first day of each month, stored in the/root/bak directory, and the file name
For the following form Yymmdd_etc,yy for the year, MM for the month, DD for the day. The shell program Fileback 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 +%y%m%d ' _etc.tar.gz
cd/etc/
TAR-ZCVF $filename *
MV $filename/root/bak/
------------------------------------------------------
Vim/etc/crontab Join
* * 1 * * root./fileback.sh &
17. A regular user wants to back up/user/backup to/tmp directory every Sunday:00, what should the user do?
Let's start by talking about how non-root users write crontab files.
One:
[Email protected] ~]$ vim Cronfile
[Email protected] ~]$ crontab cronfile
Two:
[Email protected] ~]$ CRONTAB-E
No crontab for sword-using an empty one
Crontab:installing New Crontab
View results:
[Email protected] cron]# crontab-u sword-l
The content of the display is: the contents of the/var/spool/cron/sword file.
Vim ~/shit.sh
#!/bin/bash
cp/user/backup/*/tmp/
--------------------------------------------
Crontab-e
0 0 * * 0 ~/shit.sh &
18. Design a shell program, set up 50 directories under the/userdata directory, i.e. User1~user50, and set the permissions for each directory, which
The permissions for other users in the file are read, read, write, execute, and the permissions of the group to which the file owner resides are: Read, execute.
#!/bin/bash
For ((i=1;i<=50;i++))
Do
Mkdir-p/usrdata/user$i
Cd/usrdata
chmod 754 user$i
Done
19. The contents of a file are all similar
"202.205.151.21"--"23:59:22"-"HTTP GET"-"Mozila"
...
Write a shell command to find up to 10 IPs
Awk-f "--" ' {print $} ' shit | Sort | uniq-c | Sort-r | Sed-n ' 1,10p '
There are 800 files under the 20./tmp path, and the file name format is: filename_yyyymmdd_ serial number (from 001 to 999). DAT, for example: Filename_20040108_089.dat. Now want to rename these files, the format of the new file name is: Filename_today (the current date) _ Serial number (starting from 500, 999 after the arrival of 001). Dat, for example: change filename_20040108_089.dat to filename _20041222_589.dat, note that the sequence of the new file name sequence needs to be consistent with the original, that is, to do the sort processing.
#!/usr/bin/bash
Dest_file_part2= "_ ' Date ' +%y%m%d" _ "
Ext_name= ". Dat"
Src_file_list= ' Find/tmp-name "*_*_* $EXT _name"-print "
For each in $SRC _file_list; Do
Dest_file_part1= ' echo $each | Awk-f "_" ' {print '} '
Old_num= ' echo $each | Awk-f "_" ' {print $} ' | Awk-f "." ' {print '} '
dest_file_part3= ' expr $OLD _num + 500 '
[$DEST _file_part3-gt 999] && dest_file_part3= ' expr $OLD _num-499 '
&& dest_file_part3= ' printf D $DEST _file_part3 '
dest_file= $DEST _file_part1$dest_file_part2$dest_file_part3$ext_name
echo "MV $each to $DEST _file"
MV $each $DEST _file
Done
My solution:
#!/bin/bash
new_file=$ (date +20%y%m%d);
For file in $ (LS ~/bash)
Do
I=$ (echo "$file" | sed ' s/.*_[0-9]*_\ ([0-9]*\). dat/\1/') #取出序列号
i=$ (echo $i | sed ' s/^0*\ ([0-9]*\)/\1/') #去掉序列号前面可能的 ' 0 ',
Let "ii=i+500"
If [$ii-gt 999]
Then
Let "ii=ii-999"
Fi
Ii= ' printf D $ii ' #添上可能需要的 ' 0 '
Update=$ (echo "$file" | sed ' s/\ (. *\) _[0-9]*_[0-9]*.dat/\1_ ' $new _file ' _ ' $ii '. dat/')
MV ~/bash/$file ~/bash/$update
Done
21. Requirements: Complete the program in a script
1. Remove the given user name and user group from the file User.list and add those users and groups to the system by rule
2. Read the given user password from the password.list.
User.list as follows
Zhangsan Adminuser,dbuser,updatauser
Lisi Dbuser,updatauser
Wanger Updatauser,wheel
#!/bin/bash
#group add
For x in ' awk ' {print $} ' user.list | Sed ' s/,/\n/g ' | Sort | Uniq-c|sed ' s/[^a-za-z]//g '
Do
Groupadd $x &>/dev/null
Done
#back message
if (($?==0))
Then
echo "Group ok!!"
Else
Exit 1
Fi
#user add
For i in ' awk ' {print $ ' user.list '
Do
For y in ' awk ' {print $} ' password.list '
Do
Useradd $i &>/dev/null
echo $y | Passwd–stdin $i &>/dev/null
Done
Done
#back message
if (($?==0))
Then
echo "User ok!"
Else
Exit 1
Fi
#add users to groups
For ((q=1;q<=3;q++))
Do
Usermod-g ' awk ' nr== $q {print $} ' user.list | awk ' {print $} ' awk ' nr== $q {print $} ' user.list | awk ' {print '} ' &>/dev/null
Done
if (($?==0))
Then
echo "All finished!"
Fi
22. Compare two decimal sizes.
Awk-v num1=6.6-v num2=5.5 ' Begin{print (num1>num2)? " 0 ":" 1 "} '
echo "0.14 > 0.15" | Bc
Expr 1.2 \< 1.3
Shell script reading (explaining the functions performed below), please pick out the error in the program or script below and explain where it went wrong.
#!/bin/bash
#监控cpuser的point端口是否正常
Logname= "/HOME/FORUM/LOG/LPOINTLOG.WF"
Flagfile= "/home/forum/log/lognum.txt"
Lodnum=sed-n "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! " [email protected]</dev/null
Fi>
The command line is replaced with an inverse quotation mark, if [$?==0] should be written as if [$?=0], to determine whether the last command was executed successfully;
The second-to-last line should be >/dev/null, and finally > removed from the back of FI.
25. Design a shell program, backup and compress all contents of/etc directory on the first day of each month, store in/root/bak directory, and file name is YYMMDD_ETC,YY for year, MM for month, DD for day. The shell program Fileback stored in the/usr/bin directory.
Reference Answer:
(1) Writing shell program Fileback:
#!/bin/sh
Dirname= ' Ls/root | grep Bak '
If [-Z "$DIRNAME"]; Then
Mkdir/root/bak
Cd/root/bak
Fi
yy= ' Date +%y '
mm= ' Date +%m '
dd= ' Date +%d '
backetc= $YY $mm$dd_etc.tar.gz
Tar zcvf $BACKETC/etc
echo "Fileback finished!"
(2) Write task timer:
echo "0 0 1 * */bin/sh/usr/bin/fileback" >; /root/etcbakcron
Crontab/root/etcbakcron
Or add a scheduled task using the CRONTAB-E command:
0 1 * * */bin/sh/usr/bin/fileback
26. There are 10 monitored host, a monitoring machine, in the monitoring machine script, once a monitoring machine/partition utilization rate is greater than 80%, issued an alarm, put in crontab, every 10 minutes check.
(1) First, build a trust relationship 1 VS 10. But take two machines (192.168.1.6,192.168.1.4) to do the experiment
#ssh-keggen-b 1024-t RSA//(root user)
#cd. ssh/
#ls
Id_rsa
Id_rsa.pub
Knows_host
#scp id_rsa.pub 192.168.1.4:/root/.ssh/192.168.1.6
Here, the public key is named the IP address of the trusted host.
Log in to the 192.168.1.4 machine now
#cd. ssh/
#cat 192.168.1.6 >> Authorized_keys
And then back to the 192.168.1.6 machine.
#ssh 192.168.1.4
This can be done, which may involve permission issues. General. ssh/folder is 755,authorized_keys 600 or 644
(2) The script is as follows:
#!/bin/sh
#script:d f_check.sh
fsmax= "80"
Remote_user= ' Root '
remote_ip= (192.168.1.2 192.168.1.3 192.168.1.4 ...) 10 IP addresses
ip_num= ' 0 '
While ["$ip _num"-le "$ (expr ${#remote_ip [@]}-1)"]
Do
read_num= ' 1 '
SSH "$remote _user" @ "${remote_ip[$ip _num]}" Df-h >/tmp/diskcheck_tmp
grep ' ^/dev/* '/tmp/diskcheck_tmp|awk ' {print $ $} ' |sed ' s/\%//g ' >
/tmp/diskcheck_tmp_num
While ["$read _num"-le $ (Wc-l </tmp/diskcheck_tmp_num)]//Calculate how many rows
Do
size=$ (sed-n "$read _num" ' P '/tmp/diskcheck_tmp_num)
If ["$size"-GT "$FSMAX"]
Then
$ (grep ' ^/dev/* '/tmp/diskcheck_tmp|sed-n $read _num ' P ' >
/tmp/disk_mail)
$ (echo $ (remote_ip[$ip _num]) >>/tmp/disk_mail)
$ (mail-s "diskcheck_alert" admin </tmp/disk_mail)
Fi
read_num=$ (Expr $read _num + 1)
Done
ip_num=$ (Expr $ip _num + 1)
Done
(3) Put it inside the crontab.
#######################################################################
############### #让脚本每十分钟执行一次 #################################
In the cron table:
0/10 * * * */home/codefei/diskcheck.sh 2>&1
27. Use shell programming to determine if a file is a character device file, if it is copied to the/dev directory.
Reference program:
#!/bin/sh
Filename=
echo "Input file name:"
Read FILENAME
If [-C "$FILENAME"]
Then
CP $FILENAME/dev
Fi
Shell Instance Collection