计划任务&脚本进阶练习

1、每天的2点和12点整,将/etc备份至/testdir/backup目录中,保存的文件名称格式为“etcbak-yyyy-mm-dd-HH.tar.xz”
    mkdir /testdir/backup
    vim /root/bin/etcbak.sh
        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

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

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、打印九九乘法表
for i in {1..9} ;do
    for j in `seq 1 $i` ;do
        echo -e "$i*$j=$(($i*$j))\t\c"
    done
    echo
done

15、在/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


16、提示输入行数n,打印相应n行的等腰三角形用while实现
方法一:
for i in {1..9} ; do
    for j in `seq 1 $((9-$i))` ; do
            echo -e " \c"
    done
    for k in `seq 1 $((2*$i-1))` ;do
            echo -e "^\c"
    done
    echo
done

方法二
i=1
while [ $i -le 9 ] ; do
    j=1
    while [ $j -le $((9-$i)) ] ; do
            echo -e " \c"
            let j++
    done
    k=1
    while [ $k -le $((2*$i-1)) ] ; do
            echo -e "^\c"
            let k++
    done
    let i++
    echo
done

17、编写脚本,求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"

18、编写脚本,提示请输入网络地址,如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"

19、编写脚本,打印九九乘法表
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


20、编写脚本,利用变量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


21、编写脚本,实现打印国际象棋棋盘
方法一:
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


22、后续六个字符串:efbaf275cd、4be9c40b8b、44b2395c46、f8c8873ce0、b902c16c8b、ad865d2f6是通过对随机数变量RANDOM随机执行命令:echo $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

以上内容为个人实验脚本,考虑不是很周全,还望海涵,谢谢!

原创文章,作者:CL80516000,如若转载,请注明出处:http://www.178linux.com/71877

(0)
CL80516000CL80516000
上一篇 2017-03-26
下一篇 2017-03-26

相关推荐

  • 文本处理工具sed习题

    1、删除/etc/grub2.cfg文件中所有以空白开头的行行首空白字符 [root@wzc date]# sed -n 's/^[[:space:]]\+//p' /etc/grub2.cfg load_env set default="${next_entry}&q…

    Linux干货 2016-08-12
  • 20160801作业-用户和组

    http://note.youdao.com/yws/public/redirect/share?id=ba6a68550cd5c0ba5fd1cbfb66651916&type=false

    Linux干货 2016-08-08
  • linux网络管理之三

    cat /etc/hosts              可以做名字解析,黑客用来冒充网站IPcentos6 更改主机名          vim /etc/sysconfig/network      &nbs…

    2017-03-21
  • keepalived——高可用集群

    HA Cluster 集群类型:LB、HA、HP 系统可用性的公式:A=MTBF/(MTBF+MTTR) (0,1) 几个9: 99%, …, 99.999%     建议使用3个9的系统可用性 如何降低MTTR:冗余(redundant) active/passive active –>…

    Linux干货 2016-11-01
  • 位置变量&特殊变量总结

    位置变量 常用的位置变量有 $1, $2, $3 ……,表示命令行传给脚本的第一个参数,第二个参数,第三个参数。。。 $0 表示脚本的文件名,比如a.sh 位置变量在脚本中的主要作用,是让脚本通过他们来获取命令行传递给脚本的参数。 变量位置调整 shift [n] 用于调整变量位置 第n+1个位置变量会被重新命名为$1…

    Linux干货 2016-08-15
  • 文本处理工具之awk

                    文本处理工具之AWK 一、linux文本处理工具三剑客:grep 、sed、 awk。其中grep是一种文本过滤器,sed是文本行编辑器,而awk是一种报表生成器,就是对文件进行…

    2017-05-21