Linux学习笔记十八:一些常见的脚本

备份命令

#!/bin/bash
if [ ! -d /data/back/bin ];then
                mkdir -p /data/backup/bin
fi

if [ ! -d /data/backup/usr/bin ];then
                mkdir -p /data/backup/usr/bin
fi

if [ ! -d /data/backup/lib64 ];then
                mkdir -p /data/backup/lib64
fi

if [ ! -d /data/backup/lib ];then
                mkdir -p /data/backup/lib
fi

while true;do
                read -p "请输入一个可执行命令:" CMD
                if [[ $CMD =~ ^q$ ]];then
                                break
                elif [[ $CMD =~ ^/bin/.*$ ]];then
                                cp -f $CMD /data/backup/bin
                                LIB_DIR=`ldd $CMD|sed -nr 's/.*((\/.*\/)[^/]+) .*/\2/p'|head -n1`

                                for file in `ldd $CMD|sed -nr 's/.*((\/.*\/)[^/]+) .*/\1/p'`;do cp -f $file /data/backup$LIB_DIR;done
                elif [[ $CMD =~ ^/usr/bin/.*$ ]];then
                                cp -f $CMD /data/backup/usr/bin
                                LIB_DIR=`ldd $CMD|sed -nr 's/.*((\/.*\/)[^/]+) .*/\2/p'|head -n1`
                                for file in `ldd $CMD|sed -nr 's/.*((\/.*\/)[^/]+) .*/\1/p'`;do cp -f $file /data/backup$LIB_DIR;done
                fi

done

创建文件

    #!/bin/bash
*******************************************************************
if [ ! -d /testdir/ ];then
                mkdir /testdir
                cd /testdir
                for ((i=0;i<10;i++));do
                                touch $i`head -c 100 /dev/random |base64|grep -o "[[:alpha:]]"|head -n8|tr -d "\n"`.html
                done
else
                cd /testdir
                for ((i=0;i<10;i++));do
                                touch $i`head -c 100 /dev/random |base64|grep -o "[[:alpha:]]"|head -n8|tr -d "\n"`.html
                done
fi

获取模块网卡IP地址

#!/bin/bash
. functions
if [ $# -eq 0 ];then
                echo "Usage: $0 NIC_NAME ..."
fi
get_ip $*

开启、关闭、重启服务

#!/bin/bash
. functions
echo "please input your action: [1-5]"
select action in "start" "stop" "status" "restart" "quit";do
                case $action in 
                                start)  $action;;
                                stop)   $action;;
                                restart) $action;;
                                status) $action;;
                                quit)   exit;;
                                *)      echo "请输入[1-5]"
                                continue;;
                esac
done

画杨辉小星星三角形

#!/bin/bash

BEGIN="\e[1;5"
END="\e[0m"
read -p "input lines: " LINE
#if [ -n $LINE ];then
#LINE=7
#fi
for i in `seq $LINE`;do
                j=1
                k=1
                while [ $k -le $[$LINE-$i] ];do
                                echo -e " \c"
                                let k+=1
                done
                k=1
                while [ $j -le $i ];do
                                COLOR=$((RANDOM%7+31))
                                echo -e "$BEGIN;${COLOR}m* $END\c"
                                let j+=1
                done
                echo 
done

查看一个网络所有up的主机

#!/bin/bash

for i in 172.22.{0..254}.{1..254};do 
(ping -c1 -W1 $i &> /dev/null && echo $i is up || echo $i is down |grep -o "up" &) >> ./uphost.txt
done

打印一个99乘法表

#!/bin/bash
BEGIN="\e[1;"
END="\e[0m"
for i in `seq 9`;do
                j=1
                while [ $j -le $i ];do
                                COLOR=$((RANDOM%7+31))
                                echo -e "${BEGIN};${COLOR}m${j}x${i}=$[i*j]$END\t\c"

                let j+=1
                done
                echo
done

检查服务启动状态

#!/bin/bash
. functions
. /etc/init.d/functions
service_status $*
#action $res true

检查系统的版本

#!/bin/bash
. functions
sys_ver
echo $?

有些直接调用了上篇functions函数了。。。

猜你喜欢

转载自blog.51cto.com/13182370/2374699