Shell exercise -16

Question requirements

Count the number of numbers that appear in each line of the document a.txt and calculate how many numbers appear in the entire document. For example, the content of a.txt is as follows:
12aa*lkjskdj
alskdflkskdjflkjj
The name of our script is ncount.sh, when we run it:
bash ncount.sh a.txt
The output should be:
2
0
sum:2

Reference answer

#!/bin/bash
sum=0
while read line //New knowledge points can read each line
do
line_n=`echo $line|sed's/[^0-9]//g'|wc -L`
echo $line_n
sum=$[$sum+$line_n]
done <$1 //where $1 is the following file
echo "sum:$sum"

Title Requirements

There are two Linux servers A and B. If A can ssh directly to B, there is no need to enter a password. Both A and B have a directory called /data/web/. There are many files under it.

Of course, we don’t know how many levels of subdirectories there are. If the files in this directory on A and B are all Exactly the same.

But now I’m not sure if it is the same. It is necessary for us to write a script to achieve such a function to detect the similarities and differences between the files in the /data/web/ directory of machine A and machine B. We use the files on machine A as the standard.

For example, if machine B is missing an a.txt file, then we should be able to detect it, or if the b.txt file on machine B has changed, we should also be able to detect it (on machine B Don’t think about more files).

Refer to the answer

#!/bin/bash
dir=/data/web
#delete if the MD5.list file exists
[ -f /tmp/md5.list] && rm -f /tmp/md5.list
#Write the satisfied file name to a file
find $dir/ -type f> / tmp/file.list
while read line
do
# Write md5 code and file name into md5 list
md5sum $line >> /tmp/md5.list < br />done #Synchronize the md5 list to the b server for comparison
scp /tmp/md5.list B:/tmp/
#Judge whether it exists check_md5.sh, delete existence
[ -f /tmp/check_md5.sh] && rm -f /tmp/check_md5.sh
#cat> .sh << eof eof
cat >/ tmp/check_md5.sh << EOF
#!/bin/bash
dir=/data/web
#All backticks need to be escaped
#md5 number
n=\`wc -l /tmp/md5.list|awk'{print \$1}'\`
for i in \`seq 1 \$n\`
do< br /> #Get the name of the md5 file
file_name=\`sed -n "\$i"p /tmp/md5.list |awk'{print \$1}'\`
#Get md5
md5=\`sed -n "\$i"p /tmp/md5.list|awk'{print \$2}'\`
if [-f \$file_name ]
then
#b server md5 value
md5_b=\`md5sum \$file_name\`
#A server and B server md5 value comparison
if [\$md5_b != \$md5 ]
then
echo "\$file_name changed."
fi
else
echo "\$file_name lose."
fi
done
EOF
#Sync check_md5.sh to the b server
scp /tmp/check_md5 .sh B:/tmp/
#Execute check_md5.sh on the b server
ssh B "/bin/bash /tmp/check_md5.sh"

Question requirements

Write a script to detect your network traffic and record it in a log. Need to follow the format below, and count once a minute (only need to count the external network card, assuming the network card name is eth0):
?
2017-08-04 01:11
eth0 input: 1000bps
eth0 output: 200000bps

2017-08-04 01:12
eth0 input: 1000bps
eth0 output: 200000bps
?
Hint: use sar -n DEV??1 59 In this way, the average network card traffic for one minute can be counted, and only the last average value is needed. In addition, pay attention to the conversion, 1Byte=8bit

The main knowledge points sar -n DEV 1 59
1byte=8bit

Reference answer

#!/ bin/bash
logdir=/tmp/sar_log
file=$logdir/`date +%d%H`.log
t=`date +"%F %H:%M" `

[ -d $logdir] || mkdir -p $logdir
LANG=en
sar -n DEV 1 5 |grep eth0 |grep "Average"> /tmp /sar.tmp

exec >>$file
echo "$t"
#\nEnter
awk'{print "eth0 input:",$5 *8000"bps""\n""eth0 output:",$6*8000"bps"}' /tmp/sar.tmp
echo "#### ########### ########"

Question requirements

A machine has a high load, the top view has many sh processes, and then the top -c view can see the corresponding processes The command is sh -c /bin/clearnen.sh.

After analysis, it is found that the execution time of the script is too long, which leads to the execution of the last script in the subsequent execution. Write a script to kill all sh processes in batches.

ps aux|grep clearnen.sh|awk'{print $2}’|xargs kill -9

Reference answer

#!/bin/bash< br />for pid in `ps aux |grep clearnen.sh |awk'{print $2}'` 
do
echo $pid
kill -9 $pid
done< /pre>

Question requirements

Write a script to determine whether web services are enabled in your Linux server? (Monitor port 80) If it is enabled, please determine what service is running. Is it httpd, nginx, or something else?

Reference answer

#!/bin/bash
n=`netstat -lntp |grep':80'|wc -l`
if [$ n -eq 0 ]
then
echo "It not listen port 80"
else
ser=`netstat -lntp |grep':80'|awk -F'/' '{print $NF}'|sed's/ //g'`
echo "It is listenning port 80, and the service is $ser."
fi

Leave a Comment

Your email address will not be published.