shell編程筆記

查看目錄php

tree -i -f | awk '{if(!system("test -d "$1))print $1}'

 

批量快速建立user前端

for i in user{0..10}; do
    useradd $i
    echo 123456 | passwd --stdin $i
done

 

使用if語句判斷mysql

#!/bin/bash
#文件判斷
if [ -e $1 ]; then
    echo "file exists"
fi

if [ -r $1 ]; then
  echo "readable"
fi

if [ -w $1 ]; then
  echo "writable"
fi

if [ -x $1 ]; then
  echo "executeable"
fi

if [ -s $1 ]; then
  echo "have contents"
fi

if [ -d $1 ]; then
  echo "directory"
fi

if [ -f $1 ]; then
  echo "file"
fi

if [ -c $1 ]; then
  echo "charactor device"
fi

if [ -b $1 ]; then
    echo "block device"
fi

#字符串判斷
if [ $1 = "admin" ]; then
    echo "you are admin"
fi

if [ $1 != "demon" ]; then
    echo "you are not demon"
fi

if [ -z $1 ]; then
    echo "zero string"
fi

if [ -n $1 ]; then
    echo "not empty"
fi


#數字判斷
if test $1 -eq 4; then
    echo "$1=4"
fi
if test $1 -ne 4; then
    echo "$1!=4"
fi
if test $1 -gt 4; then
    echo "$1>4"
fi
if test $1 -ge 4; then
    echo "$1>=4"
fi
if test $1 -lt 4; then
    echo "$1<4"
fi
if test $1 -le 4; then
    echo "$1<=4"
fi


#C語言語法
if (( $1 != 'demon' )); then
    echo "[C]not demon"
elif (( $1 > 5 )); then
    echo "[C]$1 > 5"
fi

#更兼容的用法
if [[ $str == "hello" ]]; then
    echo $str
fi

if [[ -d $dir ]]; then
    echo $dir is directory
fi

 

 循環 與 選擇sql

#POSIX用法
for
((i=0;i<10;i++)); do if [ $i -eq 5 ]; then continue elif [ $i -eq 9 ]; then break; fi echo $i done read -p "Enter a char: " char case $char in [a-z]|[A-Z]) echo "A letter" ;; [0-9]) echo "A digut" ;; *) echo "function key" ;; esac echo '$#: ' $# echo '$@: ' $@ echo '$*: ' $*
#shift用法 while [ $# -gt 0 ]; do if [[ $str == "" ]]; then str=$1 else str="$str-$1" fi shift done echo $str

 

數組shell

#!/bin/bash
a="hello"
arr=($a  1  2  "c"  d)

length=${#arr}                # 5
arr[5]=$arr

echo length is $length        # 6

for i in ${arr[@]}; do
    echo $i
done

for i in `seq 0 $length`; do     # 0 ~ 5
    arr[$i]=$i
done

i=0

while [ $i -lt ${#arr[*]} ]; do
    echo ${arr[$i]}
    let "++i"
done

 

函數apache

function add(){
    tot=1
    for (( i=1;i<=$1;i++ )); do
        tot=$(($tot*$i))
    done
    echo $tot
}

add 6   #720

function sum(){
    return $(($1+$2))

}

sum 30 40

echo "30 + 40 = $?"

echo $$         #pid
echo $-         #set
echo $!         #the lastest pid

 

 

cut 提取用戶名數組

cut -d: -f1 /etc/passwd

 

cat /etcpasswd | awk -F: '{print "username: "$1"("$2")"}'

 

 awk 一些常見用法bash

for i in `awk -F: '{print $1}' /etc/passwd |head` ; dosleep 1
    echo $i
done

for i in `df -Th | awk '{if(NR==2)print int($6)}'`; do
    echo usage: $i%
done


df -Th | awk 'END{print "row: "NR "\ncol: "NF}'

 

測試文件[access.log]ssh

192.168.10.1  root.php
192.168.10.2  bin.php
192.168.10.4  daemon.php
192.168.10.2  adm.php
192.168.10.1  lp.php
192.168.10.1  sync.php
192.168.10.2  shutdown.php
192.168.10.6  halt.php
192.168.10.2  mail.php
192.168.10.2  uucp.php
192.168.10.2  operator.php
192.168.10.3  games.php
192.168.10.4  gopher.php
192.168.10.1  ftp.php
192.168.10.7  nobody.php
192.168.10.2  vcsa.php
192.168.10.4  abrt.php
192.168.10.4  ntp.php
192.168.10.3  saslauth.php
192.168.10.2  postfix.php
192.168.10.3  sshd.php
192.168.10.2  tcpdump.php
192.168.10.3  dbus.php
192.168.10.1  apache.php
192.168.10.8  mysql.php

 

awk 過濾 192.168.10.1 訪問的記錄tcp

 cat access.log | awk  '$1 ~ /192.168.10.1/ {print $0}'

awk 過濾非 192.168.10.1 訪問的記錄

 cat access.log | awk  '$1 !~ /192.168.10.1/ {print $0}'

 

sed 只打印第5行數據

 cat passwd  |sed -n '5'p

sed 打印第5到8行數據

 cat passwd  |sed -n '5,8'p

sed 屏蔽第3到20行數據

cat passwd  |sed  '3,20'd

sed 打印登陸用戶

cat passwd  | sed -n '/bash/'p

sed 顯示第一行到包含sync的行

cat passwd  | sed -n 1,/sync/p

sed 顯示從sshd到最後一行

 cat /etc/passwd | sed -n '/sshd/,$'p

 

uniq 相同合併並統計

 cat access.log  | awk '{print $1}' | sort | uniq  -c

uniq 打印出現超過一次的行

 cat access.log  | awk '{print $1}' | sort | uniq  -d

sort 按字母升序

 cat access.log  | awk '{print $1}' | sort

sort -r 按字母降序

cat access.log  | awk '{print $1}' | sort -r

sort 也能夠本身分割文件排序,不用awk, -t指定分隔符默認空格, -k指定按哪一列排序

 cat access.log | sort -t: -k1

split 將文件進行分割成多個小文件

 split -5 passwd splitname
#ls
#passwd  splitnameaa  splitnameab  splitnameac  splitnamead  splitnameae

 

 

顏色庫

#!/bin/bash
#name color.sh
#auth demonxian3
da=` echo -e "\033[31m"` #danger su=` echo -e "\033[32m"` #success wa=` echo -e "\033[33m"` #warning pr=` echo -e "\033[34m"` #primary vi=` echo -e "\033[35m"` #violet in=` echo -e "\033[36m"` #info de=` echo -e "\033[37m"` #default cl=` echo -e "\033[0m"` #clear

 

 

項目一   目錄文件統計

#!/bin/bash
#author demonxian3
line
=`tree -i -f .|wc -l` files=`tree -i -f .| head -n $(($line-1))` fileCount=0 dirCount=0 for file in $files; do if [ -d $file ]; then dirCount=`expr $dirCount + 1` ; echo $file else fileCount=`expr $fileCount + 1` fi done echo "Dir:"$dirCount echo "Files:"$fileCount

 


項目二  文件修改監視器

#!/bin/bash
#name filemonitor
#auth demonxian3
len
=`tree -i -f | wc -l` len=`expr $len - 1 ` files=`tree -i -f | head -n $len` timestamp=() idx=0 for file in $files; do mtime=`date +%s -r $file` timestamp[$idx]="$mtime#$file" idx=$(($idx+1)) done idx=$(($idx-1)) function check(){ for i in `seq 0 $idx`; do file=${timestamp[$i]:11} oldtime=${timestamp[$i]:0:10} newtime=`date +%s -r $file` if [ $newtime -ne $oldtime ]; then echo "$file has been modified" timestamp[$i]="$newtime#$file" fi done } while true; do check sleep 1 done

 

 

項目三  編寫系統服務腳本以及實現開機自啓

/etc/rc.d/init.d 目錄下存放着許多服務腳本, 可使用service  和  chkconfig 來實現服務關閉開啓或者開機自啓動

爲了方便,系統會製做連接目錄連接上面的路徑, /etc/init.d/  ->  /etc/rc.d/init.d/

若是不想經過chkconfig 來實現開機自啓,也能夠經過寫入啓動腳本 /etc/rc.d/rc.local  或者 /etc/rc.d/rc.local 實現自啓動

若是想把一個服務腳本改形成 chkconfig 能夠識別的啓動項,須要在腳本前端添加以下兩行

# chkconfig: 2345 90 21
# description:  myservicename service daemon

2345表示不指定runlevel等級時運行的默認等級

90表示開啓時的順序 S90開頭

21表示關閉時的順序 K21開頭、

只要將腳本放到 /etc/rc.d/init.d下就能夠經過services 來啓動腳本

 

 

項目四  awk 分析apache日誌訪問記錄

#!/bin/bash
#[09/May/2018:07:59:32]

UsrAgent=$1


cat /var/log/httpd/access_log | awk '
BEGIN{
    dan="\033[31m"
    suc="\033[32m"
    pri="\033[34m"
    war="\033[33m"
    vio="\033[35m"
    inf="\033[36m"
    def="\033[37m"
    cls="\033[0m"
    usrAgent=$useragent
}
{
    if("'"$UsrAgent"'") print vio "user-agent: " $12$13$14$15$16$17$18$19$20$21$22$23$24$25$26$27$28$29$30$31$32 cls


    len=length($10);
    len=3-len;
    while((len--)>0)
        $10=$10" "

    len=length($6);
    len=6-len;
    while((len--)>0)
        $6=$6" "

    print war $1"\t" cls,
    suc substr($4,5,3)"/"substr($4,2,2)" "substr($4,14,8) cls,
    substr($6,2,5),
    $9,
    $10,
    inf $7 cls
}'

使用方法:

# bash analysis.sh

IP         日期     方法   返回碼 長度  訪問的頁面

查看用戶代理

# bash analysis.sh 1

 

項目五  根據日誌404頻繁訪問進行iptables的封殺, 封殺半個小時

#!/bin/bash
# banshell.sh


# [Notice] clear iptables
`iptables -F` #calculate the
time for ban time curHour=`date +%H` curMinu=`date +%M` starttime="$curHour:$curMinu" curMinu=$(($curMinu + 30)) if [[ $curMinu -gt 59 ]]; then curHour=$(($curHour + 1)) curMinu=$(($curMinu % 60)) fi stoptime="$curHour:$curMinu" dangerIP=`cat /var/log/httpd/access_log | grep 404 | awk '{print $1}' | sort | uniq -c | awk '{if($1>30)print $2}'` for i in $dangerIP; do res=`iptables -nvL | grep $i` if [[ $res == "" ]]; then `iptables -A INPUT -s $i -m time --timestart $starttime --timestop $stoptime -j DROP ` else echo "go" fi done

 

項目六  製做操做菜單

 

 

 經過點能夠實現shell文件包含

.  shellname.sh

 

編寫菜單

#!/bin/bash
#name menu.sh
#auth demonxian3
. color.sh       #包含color庫 function menu(){ cat << EOF $pr ********************************************* * Operation Menu * ********************************************* $cl $in * * * 1) add a user * * 2) set passwd * * 3) del a user * * 4) select user * * 5) print disk space * * 6) print mem space * * 7) quit * * * ********************************************* $cl EOF } if [[ $0 == "menu.sh" ]]; then menu fi

 

編寫 交互處理 和 邏輯處理

#!/bin/bash
#name func.sh
#auth demonxian3
. menu.sh function addUsr(){ read -p "Enter the username: " user read -p "Enter the password: " pass `useradd $user` `echo "$pass" | passwd --stdin "$user" 1>/dev/null` id $user } function setPasswd(){ read -p "Enter the username: " user read -p "Enter the password: " pass echo $pass | passwd --stdin $user 1> /dev/null if [ $? -eq 0 ]; then echo "Successfully" fi } function delUsr(){ read -p "Enter the username: " user `userdel -r $user 1> /dev/null ` if [ $? -eq 0 ]; then echo "successfully" fi } function selectUsr(){ cat /etc/passwd | awk -F: '{ if(NR%4==0) printf("\n"); len=length($1); while(len++<20)$1=$1" " printf($1" ") }END{ printf("\n") }' } function selectDisk(){ df -Th | grep sda | awk 'BEGIN{print "deviceName\tfs\ttotal\tused\tfree\tperc\twhere"}{print $1"\t"$2"\t"$3"\t"$4"\t"$5"\t"$6"\t"$7}' } function selectMem(){ free -h | grep -vw "cache" } while true; do clear menu read -p "Input your select: " input case $input in 1) addUsr ;; 2) setPasswd ;; 3) delUsr ;; 4) selectUsr ;; 5) selectDisk ;; 6) selectMem ;; *) echo "Invalid selection" ;; esac read -p "Press any key to continue" done

 

如下命令將文中全部的字符串idiots替換成managers:
:1,$s/idiots/manages/g
一般咱們會在命令中使用%指代整個文件作爲替換範圍:
:%s/search/replace/g
如下命令指定只在第5至第15行間進行替換:
:5,15s/dog/cat/g
如下命令指定只在當前行至文件結尾間進行替換:
:.,$s/dog/cat/g
如下命令指定只在後續9行內進行替換:
:.,.+8s/dog/cat/g
你還能夠將特定字符作爲替換範圍。好比,將SQL語句從FROM至分號部分中的全部等號(=)替換爲不等號(<>):
:/FROM/,/;/s/=/<>/g
在可視化模式下,首先選擇替換範圍, 而後輸入:進入命令模式,就能夠利用s命令在選中的範圍內進行文本替換。

相關文章
相關標籤/搜索