計劃任務&腳本進階練習

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.www58058.com/71877

(0)
CL80516000CL80516000
上一篇 2017-03-26 22:49
下一篇 2017-03-26 23:08

相關推薦

  • N28 第三周【1】:grep和文本處理工具的使用

    grep一些練習 1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@localhost ~]# who |cut -d” ” -f1 |sort -u root 2、取出最后登錄到當前系統的用戶的相關信息。 [root@localhost ~]# last |cut -d” ” -f1|head -1 |…

    Linux干貨 2017-12-19
  • 設計模式(八)裝飾器模式Decorator(結構型)

    1. 概述        若你從事過面向對象開發,實現給一個類或對象增加行為,使用繼承機制,這是所有面向對象語言的一個基本特性。如果已經存在的一個類缺少某些方法,或者須要給方法添加更多的功能(魅力),你也許會僅僅繼承這個類來產生一個新類—這建立在額外的代碼上。       通過繼…

    Linux干貨 2015-07-03
  • Linux網絡屬性管理

      ifconfig命令 ifconfig [interface] # ifconfig -a # ifconfig IFACE [up|down] ifconfig interface [aftype] options | address … # ifconfig IFACE IP/mask [up] # ifconfig IFACE…

    Linux干貨 2015-09-26
  • 開篇

    test 新人報道

    Linux干貨 2016-10-28
  • Linux基礎操作-week5

    1、顯示當前系統上root、fedora或user1用戶的默認shell; 方式一: # whoami root # echo $SHELL /bin/bash 方式二: #grep user1 /etc/passwd|awk -F “:” ‘{print $7}’ /bin/bash 2、找出/etc/rc.…

    Linux干貨 2016-11-27
  • wk_03作業

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 who |cut -d' ' -f1|uniq 2、取出最后登錄到當前系統的用戶的相關信息。 # who |tail -1 |cut -d &#03…

    Linux干貨 2016-12-15
欧美性久久久久