1. 程式人生 > >SHELL訓練營--day19_shell練習36-40

SHELL訓練營--day19_shell練習36-40

#一個 數字的行
#!/bin/bash
while read line
do
    n=`echo $line |sed 's/[^0-9]//g'|wc -L`
    if [ $n -eq 1 ]
    then
        echo $line
    fi
done < 1.txt

#日誌切割歸檔
#!/bin/bash
cd /data/logs
log=1.log
mv_log()
{
    [ -f $1 ] && mv $1 $2
}

[ -f $log.5 ] && rm -f $log.5

for i in `seq 4 -1 1`
do 
    j=$[$i+1]
    mv_log $log.$i $log.$j
done
mv_log $log $log.1

#查詢線上IP
#!/bin/bash

for i in `seq 1 254`
do
    if ping -c 2 -W 2 192.168.0.$i &>/dev/null
    then
        echo "192.168.0.$i 線上。"
    else
        echo "192.168.0.$i 不線上。"
    fi
done

#檢查指令碼錯誤
#!/bin/bash
sh -n $1 2> /tmp/sh.err
if [ $? -ne 0 ]
then
    cat /tmp/sh.err
    read -p "請輸入 q/Q 退出指令碼。" c
    if [ -z "$c" ]
    then
        vim $1
        exit 0
    fi

    if [ $c == "q" ] || [ $c == "Q" ]
    then
        exit 0
    else
        vim $1
    fi
else
    echo "指令碼 $1 沒有語法錯誤。"
fi

#格式化數字
#!/bin/bash
n=`echo$1|wc -L`
for d in `echo $1|sed 's/./& /g'`
do 
    n2=$[$n%3]
    if [ $n2 -eq 0 ]
    then
        echo -n " ,$d"
    else
        echo  -n  "$d"
    fi
    n=$[$n-1]
done |sed 's/^,//'
echo