计划任务&脚本进阶练习

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

相关推荐

  • linux中的权限和ACL

    linux中权限详解和ACL

    2017-11-18
  • 基于haproxy实现wordpress动静分离

    环境:centos 6.8 注:此处省略对于各服务器的IP配置。 一:图示讲解      用户访问vip,通过haproxy代理得到服务器的资源。此架构下基于keepalived对haproxy做负载均衡(此种两种软件装在同一台服务器),基于haproxy对Nginx和apache做负载均衡,Nginx和Apache使用共享存储…

    2017-05-18
  • grep简述

    grep(Globel Search Regular Expression and Printing out the line) 它能使用正则表达式搜索文本,并把匹配的行打印出来。 grep包括grep、egrep和fgrep。 egrep表示扩展的grep,相比grep支持更多的匹配模式, “grep -E”相当于egrep。 fgrep是fast gre…

    2017-04-06
  • 如何配置本地yum源

    在日常学习中配置本地yum源至关重要,下面简单介绍下centos7如何配置本地yum源: 1、开机启动前检查是否将光盘镜像加载,然后开机; 2、cd /run/media/root/CentOS 7 x86_64下找到Packages和repodata这两个包非常重要,其中Packages是rpm包目录,repodata是元数据 3、cd /etc…

    Linux干货 2017-04-23
  • linux基础学习第七天-正则表达式及grep

    2016-08-04 授课内容: 各种文本工具来查看、分析、统计文本文件(more、less、cat、cut、sort、wc、uniq、head、tail、paste、diff) grep 正则表达式 扩展正则表达式 【】所有的文本查看、分析、统计等都是只是更改标准输出的显示方式,对原文件内容并没有更改 cat [OPTION]… [FILE]&…

    Linux干货 2016-08-07
  • btrfs文件系统及管理

      什么是btrfs? Btrfs(B-tree文件系统,通常念成Butter FS,Better FS或B-tree FS),linux文件系统,具有写时复制COW(copy-on-write),改善ext3文件系统单文件大小限制,并加入其他特性,如可写快照,快照的快照,内建RAID,子卷(subvloume),专注于容错,修复和易于管理。单文件…

    系统运维 2016-05-13