20170630
-
每天的2點(diǎn)和12點(diǎn)整,將/etc備份至/testdir/backup目錄中柿究,保存的文件名稱(chēng)格式為“etcbak-yyyy-mm-dd-HH.tar.xz”
00 11 * * * tar Jcvf /app/backup/etcbak-$(date +%Y-%m-%d-%H).tar.xz /etc
每周2, 4, 7備份/var/log/messages文件至/logs目錄中哩罪,文件名形如“messages-yyyymmdd”
0 0 * * 2,4,6 /app/script/backup_messages.sh
vim /app/script/backup_messages.sh
#!/bin/bash
\/usr/bin/cp -a /var/log/messages /app/backup/messages-`date +%Y%m%d%H`
3 . 每?jī)尚r(shí)取出當(dāng)前系統(tǒng)/proc/meminfo文件中以S或M開(kāi)頭的信息追加至/tmp/meminfo.txt文件中
00 */2 * * * cat /proc/meminfo | grep -e ^S -e ^M >> /tmp/meminfo.txt
4 . 工作日時(shí)間,每10分鐘執(zhí)行一次磁盤(pán)空間檢查,一旦發(fā)現(xiàn)任何分區(qū)利用率高于80%派继,就執(zhí)行wall警報(bào)
*/10 * * * 1-6 /app/script/diskcheck.sh
vim /app/script/diskcheck.sh
#!/bin/bash
num=`df | grep ^"/dev/sd" | tr -s ' ' '%' | cut -d'%' -f 5 | sort | tail -n 1`
if [ $num -gt 8 ] ;then
wall "The disk will be full"
fi
5 . 編寫(xiě)腳本/root/bin/createuser.sh蒸走,實(shí)現(xiàn)如下功能:使用一個(gè)用戶(hù)名做為參數(shù)仇奶,如果指定參數(shù)的用戶(hù)存在,就顯示其存在比驻,否則添加之该溯;顯示添加的用戶(hù)的id號(hào)等信息
#!/bin/bash
# filename createuser.sh
# author:danran
# time is 2017-07-01
# create user
#判斷參數(shù)是否小于1,參數(shù)若小于1者交互輸入username别惦,若參數(shù)不小于1狈茉,則判斷直接用戶(hù)
if [ $# -lt 1 ] ;then
read -p "Please input username " name
if id $name &> /dev/null ; then
echo "The user is exits"
else
useradd $name
id $name
[ $? -eq 0 ] && echo "$1" | passwd --stdin $1 &> /dev/null || exit 1
fi
else
if id $1 &> /dev/null;then
echo "$1 exists"
exit 0
else
useradd $1
[ $? -eq 0 ] && echo "$1" | passwd --stdin $1 &> /dev/null || exit 1
id $1
fi
fi
6 . 編寫(xiě)腳本/root/bin/yesorno.sh,提示用戶(hù)輸入yes或no,并判斷用戶(hù)輸入的是yes還是no,或是其它信息
#!/bin/bash
read -p "please input yes or no " str
case $str in
Y|y|[Yy][Ee][Ss])
echo yes
;;
n|N|[nN][oO])
echo no
;;
*)
echo "please input yes or no"
;;
esac
7 . 編寫(xiě)腳本/root/bin/filetype.sh,判斷用戶(hù)輸入文件路徑掸掸,顯示其文件類(lèi)型(普通氯庆,目錄,鏈接扰付,其它文件類(lèi)型)
vim script/filetype.sh
#!/bin/bash
# filename filetype.sh
# author:danran
# time is 2017-07-01
read -p "Please input file " file
if [ -e $file ] ;then
if [ -L $file ] ;then
echo "$file is linkfile"
elif [ -d $file ] ;then
echo "$file is directory"
elif [ -f $path ] ;then
echo "$file is common files"
else
echo "$file is others file"
fi
else
exit 2
fi
8 . 編寫(xiě)腳本/root/bin/checkint.sh,判斷用戶(hù)輸入的參數(shù)是否為正整數(shù)
#!/bin/bash
# filename checkint.sh
# author:danran
# time is 2017-07-01
read -p "Please input num " num
if expr $num + 0 &>/dev/null ;then
if [ $num -gt 0 ] ;then
# if echo $num | grep "^[0-9]\+$" > /dev/null ;then
echo "$num is number"
else
echo "$num is 0"
exit 2
fi
else
echo $num is not integer
exit 2
fi
9 . 用for實(shí)現(xiàn)
10 . 判斷/var/目錄下所有文件的類(lèi)型
vim type.sh
#!/bin/bash
# filename type.sh
# author:danran
# time is 2017-07-01
for f in /var/* ;do
if [ -L $f ] ;then
echo "$f is linkfile"
elif [ -f $f ] ;then
echo "$f is common file"
elif [ -d $f ] ;then
echo "$f is directory"
else
echo "$f is others files"
fi
done
11 . 添加10個(gè)用戶(hù)user1-user10堤撵,密碼為8位隨機(jī)字符
#!/bin/bash
# filename useradd.sh
# author:danran
# time is 2017-07-01
for i in {1..10} ;do
if id user$i &> /dev/null ;then
echo "The user$i is exits"
else
useradd user$i
echo "`head -c 500 /dev/urandom | tr -dc a-zA-Z0-9 | head -c 8` | passwd --stdin user$i" > /dev/null
echo "The user$i passwd successful"
fi
done
12 . /etc/rc.d/rc3.d目錄下分別有多個(gè)以K開(kāi)頭和以S開(kāi)頭的文件;分別讀取每個(gè)文件羽莺,以K開(kāi)頭的輸出為文件加stop实昨,以S開(kāi)頭的輸出為文件名加start,如K34filename stop S66filename start
#!/bin/bash
# filename SK.sh
# author:danran
# time is 2017-07-01
k=0
s=0
for file in /etc/rc.d/rc3.d/* ;do
if basename $file | grep ^K ;then
let k=$k+1
mv "$file" "$file stop"
elif basename $file | grep ^S ; then
let s=$s+1
mv "$file" "$file start"
fi
done
echo "The K開(kāi)頭的文件有:$k"
echo "The S開(kāi)頭的文件有:$s"
13 . 編寫(xiě)腳本盐固,提示輸入正整數(shù)n的值荒给,計(jì)算1+2+…+n的總和
[root@danran script]# vim sum.sh
#!/bin/bash
# filename /app/script/sum.sh
# author:danran
# time is 2017-07-01
read -p "Please input num n :" num
sum=0
for i in `seq $num`;do
let sum=$sum+$i
done
echo "the positive integer sum is:$sum"
14 . 計(jì)算100以?xún)?nèi)所有能被3整除的整數(shù)之和
[root@danran script]# cat 3sum.sh
#!/bin/bash
# filename 3sum.sh
# author:danran
# time is 2017-07-01
sum=0
for i in $(seq 3 3 100);do
let sum=$sum+$i
done
echo "An integer divisible by 3 sum is:$sum"
15 . 編寫(xiě)腳本,提示請(qǐng)輸入網(wǎng)絡(luò)地址刁卜,如192.168.0.0志电,判斷輸入的網(wǎng)段中主機(jī)在線狀態(tài)
[root@danran script]# vim hostping.sh
#!/bin/bash
# filename hostping.sh
# author:danran
# time is 2017-07-01
read -p "Please input network(eg:192.168.0.0): "network
net=`echo $network | cut -d. -f1-3`
up=0
down=0
for i in {1..254};do
if ping -c1 -W1 $net.$i &> /dev/null ;then
echo "$net.$i is ip"
let ++up
else
echo "$net.$i is down"
let down++
fi
done
echo "The up host is $up"
echo "The down host is $down"
16 . 打印九九乘法表
[root@danran script]# cat 99table.sh
#!/bin/bash
# filename 99table.sh
# author:danran
# time is 2017-07-01
for j in {1..9};do
for i in `seq $j`;do
echo -n -e "$[i]*$[j]=$[$i*$j]\t"
let i++
done
echo
done
17 . 在/testdir目錄下創(chuàng)建10個(gè)html文件,文件名格式為數(shù)字N(從1到10)加隨機(jī)8個(gè)字母,如:1AbCdeFgH.html
[root@danran script]# cat html.sh
#!/bin/bash
# filename html.sh
# author:danran
# time is 2017-07-01
for i in $(seq 10) ; do
touch /app/html/$i`head -c 500 /dev/urandom | tr -dc a-zA-Z | head -c 8`.html
done
18 . 打印等腰三角形
root@danran script]# vim triangle.sh
#!/bin/bash
# filename triangle.sh
# author:danran
# time is 2017-07-01
#space=s
# *=i
[ "$#" != 1 ] && read -p "Please input number:" num || num=$1
if echo $num | grep "^[0-9]\+$" > /dev/null;then
for n in `seq $num` ;do
for s in `seq "$[$num-$n]"`;do
echo -n " "
done
for i in `seq "$[2*$n-1]"`;do
echo -n "*"
done
echo
done
else
echo "Please input positive integer"
fi