任务计划及脚本20170701

20170630

  1. 每天的2点和12点整,将/etc备份至/testdir/backup目录中,保存的文件名称格式为“etcbak-yyyy-mm-dd-HH.tar.xz”

     00 11 * * * tar Jcvf /app/backup/etcbak-$(date +%Y-%m-%d-%H).tar.xz /etc
    
  2. 每周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 . 每两小时取出当前系统/proc/meminfo文件中以S或M开头的信息追加至/tmp/meminfo.txt文件中

00 */2 * * * cat /proc/meminfo | grep -e ^S -e ^M >> /tmp/meminfo.txt

4 . 工作日时间,每10分钟执行一次磁盘空间检查,一旦发现任何分区利用率高于80%,就执行wall警报

*/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 . 编写脚本/root/bin/createuser.sh,实现如下功能:使用一个用户名做为参数,如果指定参数的用户存在,就显示其存在,否则添加之;显示添加的用户的id号等信息

#!/bin/bash
# filename createuser.sh
# author:danran 
# time is 2017-07-01
# create user

#判断参数是否小于1,参数若小于1者交互输入username,若参数不小于1,则判断直接用户


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 . 编写脚本/root/bin/yesorno.sh,提示用户输入yes或no,并判断用户输入的是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 . 编写脚本/root/bin/filetype.sh,判断用户输入文件路径,显示其文件类型(普通,目录,链接,其它文件类型)

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 . 编写脚本/root/bin/checkint.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实现
10 . 判断/var/目录下所有文件的类型

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个用户user1-user10,密码为8位随机字符

#!/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目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加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"
echo "The S开头的文件有:$s"

13 . 编写脚本,提示输入正整数n的值,计算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 . 计算100以内所有能被3整除的整数之和

[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 . 编写脚本,提示请输入网络地址,如192.168.0.0,判断输入的网段中主机在线状态

[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目录下创建10个html文件,文件名格式为数字N(从1到10)加随机8个字母,如: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

你可能感兴趣的:(任务计划及脚本20170701)