Question requirements
Write a shell and see if there are custom users (ordinary users) in your Linux system. If so, how many?
Core points
centos6 uid>=500
eentos7 uid>=1000
awk -F’:”$3>=500′ /etc/ passwd|wc -l centos 5 6
awk -F’:”$3>=1000′ /etc/passwd|wc -l centos 7
awk -F’release”{print $2}’ /etc /redhat-release|cut -d’.’ -f1
Reference answer
#!/bin/bash
v=`awk -F'release ''{ print $2}' /etc/redhat-release |cut -d'.' -f1`
user()
{
if [$1 -eq 0 ]
then
echo "The system has no custom users"
else
echo "There are custom users in the system, there are $1"
fi
}
case $v in
5|6)
n=`awk -F':''$3>=500' /etc/passwd|wc -l`
user $n
;;< br /> 7)
n=`awk -F':''$3>=1000' /etc/passwd|wc -l`
user $n
;;
*)
echo "Script error."
;;
esac
Question requirements
Write a shell script to check the usage of all disk partitions And the inode usage rate is recorded in a log file named after today’s date. When you find that a certain partition capacity or inode usage is greater than 85%, send an email to notify yourself.
Reference answer
#!/bin/bash
dir=/tmp/disk
d=`date +%F`
[email protected]
[ -d $dir] || mkdir $dir
df >> $dir/$d.log
df -i >> $dir /$d.log
df|sed '1d' |awk -F '+|%''$5>=85 {print $7}'> $dir/df.tmp
df -i|sed '1d' |awk -F '+|%''$5>=85 {print $7}'> $dir/df_i.tmp
n1=`wc -l $dir/ df.tmp|awk'{print $1}'`
n2=`wc -l $dir/df_i.tmp|awk'{print $1}'`
tag=0
if [$n1 -gt 0 ]
then
if [$n2 -gt 0 ]
then
tag=11
else
tag= 10
fi
else
if [$n2 -gt 0 ]
then
tag=01
else
tag=00
fi
fi
case $tag in
11)
python mail.py $mail "Disk space and inode usage is higher than 85%" "`cat $dir/df.tmp $dir/df_i.tmp|xargs`"
;;
10)
python mail.py $mail "Disk space usage is higher than 85%" "` cat $dir/df.tmp|xargs`"
;;
01)
python mail.py $mail "Disk inode usage is higher than 85%" "`cat $ dir/df_i.tmp|xargs`"
;;
*)
;;
esac
Question requirements
One As a web application, the server has a directory (/data/web/attachment) that will be uploaded by users from time to time, but it is unknown when it will be uploaded. Therefore, we need to check whether there are new files generated every 5 minutes.
Please write a shell script to complete the test. If there are new files after the detection is completed, the list of new files needs to be output to a log divided into names by year, month, day, hour, and time.
Key points
find -mmin -5
date +%Y%m%d%H%M
#!/bin/bash
basedir=/ data/web/attachment
t=date +%Y%m%d%H%M
find $basedir/ -type f -mmin -5> /tmp /file.list
n=wc -l /tmp/file.list|awk'{print $1}'
if [$n -lt 0 ]
then
mv /tmp/file.list /tmp/$t.list
fi
Question requirements
Write a shell script to see which commands you use the most, List your most commonly used commands top10.
Reference answer
cat ~/.bash_history |sort |uniq -c |sort -nr |head -n 10
Question requirements
< p>If you need to execute a script every hour. To achieve this function in the script, when the time is 0 o’clock and 12 o’clock, you need to clear all the files in the directory /data/log/.
Note that you can only clear the contents of the file, not delete the file. At other times, you only need to count the size of each file, one file per line, and output to a log named by date and time.
You need to consider the files in the secondary, tertiary, …?? and other subdirectories under the /data/log/ directory.
Key points
find list all files
date +%H
file empty file
reference answer< /h4>
#!/bin/bash
dir=/tmp/log_stat
t=`date +%d%H`
t1=`date +%H`
logdir=/data/log
[ -d $dir] || mkdir $dir
[ -f $dir/$t.log] && rm -f $dir/$ t.log
if [$t == "00" -o $t == "12" ]
then
for f in `find $logdir/ -type f `
do
> $f
done
else
for f in `find $logdir/ -type f`
do
du- sh $f >> $dir/$t.log
done
fi
dir=/tmp/log_stat
t=`date +%d%H`
t1=`date +%H`
logdir=/data/log
[ -d $dir] || mkdir $dir
[ -f $dir/$t.log] && rm -f $dir/$ t.log
if [$t == "00" -o $t == "12" ]
then
for f in `find $logdir/ -type f `
do
> $f
done
else
for f in `find $logdir/ -type f`
do
du- sh $f >> $dir/$t.log
done
fi