1、每天的2点和12点整,将/etc备份至/testdir/backup目录中,保存的文件名称格式为“etcbak-yyyy-mm-dd-HH.tar.xz”
mkdir -p /testdir/backup
vim /root/bin/etcbak.shrm
tar cvf /testdir/backup/etcbak-`date "+%F-%H"`.tar.xz /etc
chmod +x etcbak.sh
crontab -e
0 2,12 * * * /root/bin/etcbak.sh
2、每周2, 4, 7 备份/var/log/messages文件至/logs 目录中,文件名形如“messages-yyyymmdd”
vim /root/bin/messages.sh
cp /var/log/messages /app/logs/messages-`date "+%Y%m%d"`
chmod +x messages.sh
crontab -e
0 0 * * 2,4,7 /root/bin/messages.sh
3、每两小时取出当前系统/proc/meminfo 文件中以S或M开头的信息追加至/tmp/meminfo.txt
crontab -e
0 */2 * * * grep "^[S|M].*" /proc/meminfo >> /tmp/meminfo.txt
4、工作日时间,每10 分钟执行一次磁盘空间检查,一旦发现任何分区利用率高于80% ,就执行wall 警报
crontab -e
vim disk.sh
[ `df -h |grep "^/dev/sd" |tr -s " " "%"|cut -d"%" -f5|sort -nr|head -1` -lt 80 ] || wall Disk will be full
*/10 * * * * /root/bin/disk.sh
shell脚本 & 条件判断练习
5 、编写脚本/root/bin/createuser.sh ,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id 号等信息
read -p "give an username:" name
if id $name &> /dev/null;then
echo "the user is exist"
else
useradd $name
echo "user id is `id -u $name`"
fi
6 、编写脚本/root/bin/yesorno.sh,提示用户输入yes 或no,并判断用户输入的是yes还是no,或是其它信息
read -p "go with me play football,yes or no :" answer
answer=`echo $answer | tr 'A-Z' 'a-z'`
if [ $answer == yes -o $answer == y ] ; then
echo "you are a playboy"
elif [ $answer == no -o $answer == n ] ; then
echo "you are not a playboy"
else
echo "you answer is false"
fi
7 、编写脚本/root/bin/filetype.sh, 判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)
read -p "you must give filename:" filename
if [ -L $filename ];then
echo "the file is link file"
elif [ -f $filename ];then
echo "the file is flat file"
elif [ -d $filename ];then
echo "the file is dir file"
elif [ -b $filename ];then
echo "the file is block device file"
elif [ -c $filename ];then
echo "the file is char device file"
elif [ -p $filename ];then
echo "the file is pipe file"
elif [ -S $filename ];then
echo "the file is socket file"
else
echo "You input the wrong filename"
fi
8 、编写脚本/root/bin/checkint.sh, 判断用户输入的参数是否为正整数
read -p "plase input number:" num
if [[ "$num" =~ ^[0-9]+$ ]];then
echo "the number is positive whole number"
else
echo "input error"
fi
shell脚本 & for循环练习
9 、判断/var/目录下所有文件的类型
for i in $@ ; do
if [ -L $i ];then
echo "the $i is link file"
elif [ -f $i ];then
echo "the $i is flat file"
elif [ -d $i ];then
echo "the $i is dir file"
elif [ -b $i ];then
echo "the $i is block device file"
elif [ -c $i ];then
echo "the $i is char device file"
elif [ -p $i ];then
echo "the $i is pipe file"
elif [ -S $i ];then
echo "the $i is socket file"
else
echo "You input the weong filename"
fi
done
10 、添加10个 用户user1-user10 ,密码为8 位随机字符
for i in {1..10};do
if id user$i &> /dev/null ; then
echo "the user$i is exsit"
passwd `tr -dc 'a-zA-Z0-9'< /dev/urandom |head -c8` &> /dev/null | passwd --stdin user$i &> /dev/null echo "user$i passwd is finished"
else
useradd user$i
passwd `tr -dc 'a-zA-Z0-9'< /dev/urandom |head -c8` &> /dev/null | passwd --stdin user$i &> /dev/null echo "add user$i and passwd is finished"
fi
done
11 、/etc/rc.d/rc3.d 目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的文件输出为文件加stop,以S开头的文件输出为文件名加start
“K34filename stop”
“S66filename start”
方法一:
ls /etc/rc.d/rc3.d/ | sed -n -e 's#^K.*#& stop#p' -e 's#^S.*#& start#p'
方法二:
for i in `ls /etc/rc.d/rc3.d` ;do
echo $i | sed -n -e 's#^K.*#& stop#p' -e 's#^S.*#& start#p'
done
12 、编写脚本,提示输入正整数n的值,计算1+2+…+n的总和
read -p "you must give a positive number: " num
sum=0
for i in `seq 1 $num` ;do
let sum=sum+i
done
echo "The sum is $sum"
13 、编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态
read -p "please input an ip : " IP
for i in $IP ; do
nmap -v -sn $IP/24 | grep -B 1 "Host is up"
done
14 、在/testdir目录下创建10个html 文件, 文件名格式为数字N(从1到10)加随机8个字母,如1AbCdeFgH.html
for i in {1..10} ;do
for j in `tr -dc 'a-zA-Z'< /dev/urandom |head -c8` ; do
touch /testdir/$i$j.html
done
done
shell脚本 & while循环练习
15 、 编写脚本,求100以内所有正奇数之和
方法一
i=0
sumji1=0
sumou1=0
while [ $i -le 100 ] ; do
if [ $(($i%2)) == 0 ] ;then
let sumou1=sumou1+i
else
let sumji1=sumji1+i
fi
let i++
done
echo "The sumji is $sumji1"
echo "The sumou is $sumou1"
方法二:
for i in {1..100};do
if [ $(($i%2)) == 0 ] ;then
let sumou=sumou+i
else
let sumji=sumji+i
fi
done
echo "The sumji is $sumji"
echo "The sumou is $sumou"
16 、 编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态,并统计在线主机和离线主机各多少
read -p "please input an ip : " IP
ip=$IP
i=0
ipup=0
ipdown=0
while [ $i -le 2 ] ; do
j=0
while [ $j -le 2 ] ; do
if [ $i == 0 -a $j == 0 ] || [ $i==255 -a $j == 255 ] ;then
ping -b -c 1 -W 1 $ip.$i.$j &> /dev/null
if [ $? == 0 ] ; then
echo "The $ip.$i.$j is up"
let ipup++
else
echo "The $ip.$i.$j is down" &> /dev/null
let ipdown++
fi
else
ping -c 1 -W 1 $ip.$i.$j &> /dev/null
if [ $? == 0 ] ; then
echo "The $ip.$i.$j is up"
let ipup++
else
echo "The $ip.$i.$j is down" &> /dev/null
let ipdown++
fi
fi
let j++
done
let i++
done
echo "host up is $ipup"
echo "host down is $ipdown"
let j++
done
k=1
while [ $k -le $((2*$i-1)) ] ; do
echo -e "^\c"
let k++
done
let i++
echo
done
17 、 编写脚本,打印九九乘法表
i=1
while [ $i -le 9 ] ; do
j=1
while [ $j -le $i ] ; do
echo -e "$i*$j=$(($i*$j))\t\c"
let j++
done
echo
let i++
done
18 、 编写脚本,利用变量RANDOM生成10个随机数字,输出这10个数字,并显示其中的最大值和最小值
方法一:
for i in {1..10} ; do
k=`echo $RANDOM`
l="$l $k"
if [ $i -eq 1 ] ; then
let max=$k
let min=$k
fi
if [ $k -ge $max ]; then
let max=$k
elif [ $k -le $min ] ; then
let min=$k
fi
done
echo $l
echo max $max
echo min $min
方法二:
i=1
while [ $i -lt 10 ] ; do
k=`echo $RANDOM`
l="$l $k"
if [ $i -eq 1 ] ; then
let max=$k
let min=$k
fi
if [ $k -ge $max ]; then
let max=$k
elif [ $k -le $min ] ; then
let min=$k
fi
let i++
done
echo $l
echo max $max
echo min $min
方法三:
i=1
while [ $i -le 10 ] ; do
let random=`echo $RANDOM`
z="$z $random"
while [ $i -eq 1 ] ; do
let max=$random
let min=$random
let i++
done
j=1
while [[ $j -le 1 ]] ;do
[[ $random -gt $max ]] && max=$random
let j++
done
j=1
while [[ $j -le 1 ]] ;do
[[ $random -lt $min ]] && min=$random
let j++
done
let i++
done
echo the $z
echo the max is $max
echo the min is $min
19 、编写脚本,实现打印国际象棋棋盘
方法一:
i=1
while [ $i -le 8 ] ; do
if [ $(($i%2)) == 1 ]; then
j=1
while [ $j -le 4 ] ; do
echo -e "\033[41m \033[0m\c"
echo -e "\033[43m \033[0m\c"
let j++
done
else
k=1
while [ $k -le 4 ] ; do
echo -e "\033[43m \033[0m\c"
echo -e "\033[41m \033[0m\c"
let k++
done
fi
echo
let i++
done
方法二:
for i in {1..8} ; do
if [ $(($i%2)) == 1 ]; then
for j in {1..4} ; do
echo -e "\033[41m \033[0m\c"
echo -e "\033[43m \033[0m\c"
done
else
for k in {1..4} ; do
echo -e "\033[43m \033[0m\c"
echo -e "\033[41m \033[0m\c"
done
fi
echo
done
方法三:
until [ $I -gt 8 ] ; do
if [ $(($I%2)) == 1 ]; then
J=1
until [ $J -gt 4 ] ; do
echo -e "\033[41m \033[0m\c"
echo -e "\033[43m \033[0m\c"
let J++
done
else
K=1
until [ $K -gt 4 ] ; do
echo -e "\033[43m \033[0m\c"
echo -e "\033[41m \033[0m\c"
let K++
done
fi
echo
let I++
done
20 、后续六个字符串:154773ae5d、bc3f3efe68、ada7aa2054 、4ee771de1f、2ebd3caa45、3417171ac1是通过对RANDOM随机数变量执行命令:md5sum|cut –c1-10后的结果,请破解这些字符串对应的RANDOM值
for k in {1..32767} ; do
i=`echo $k |md5sum|cut -c1-10`
if [ $i == efbaf275cd ] ; then
echo "the efbaf275cd is m5m $k create"
elif [ $i == 4be9c40b8b ] ; then
echo "the 4be9c40b8b is m5m $k create"
elif [ $i == 44b2395c46 ] ; then
echo "the 44b2395c46 is m5m $k create"
elif [ $i == f8c8873ce0 ] ; then
echo "the f8c8873ce0 is m5m $k create"
elif [ $i == b902c16c8b ] ; then
echo "the b902c16c8b is m5m $k create"
elif [[ $i == ad865d2f6* ]] ; then
echo "the ad865d2f6 is m5m $k create"
fi
done
网友评论