Linux學習筆記十八:一些常見的腳本
阿新 • • 發佈:2019-04-06
echo lib64 etc head col nes htm gin -c 備份命令
#!/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函數了。。。
Linux學習筆記十八:一些常見的腳本