$0 擷取目前執行shell腳本的檔案名,如果執行腳本帶路徑,那麼就包括腳本路徑
$n擷取目前執行的shell腳本的第n個參數,n=1..9,當n=0時表示腳本的檔案名,如果n>9,需要用{}括起來,例如${10},參數用空格隔開
$#擷取目前執行的shell腳本後面接的參數個數
$* 不加""時,與$@一樣,後面的參數每個都是獨立的,不管參數是否用雙引号括起來
加""時,為"$*"所有的參數都當做一個整體輸出
$@ 不加""時,與$*一樣,後面的參數每個都是獨立的,不管參數是否用雙引号括起來
加""時,為"$@"每個參數是獨立的,但如果其中有參數是用雙括号括在一起的,則視為一個參數
[[email protected] script]# vim arg.sh
#!/bin/bash
#description:test arg
#author:vita
#version:v1.0
echo '#####$0#####'
echo $0
echo '#####$n#####'
echo $2
echo '#####$*#####'
for arg in $*
do
echo $arg
done
echo '#####$@#####'
for arg in $@
do
echo $arg
done
echo '#####"$*"#####'
for arg in "$*"
do
echo $arg
done
echo '#####"$@"#####'
for arg in "$@"
do
"arg.sh" 28L, 349C written
[[email protected] script]# sh arg.sh i am "old boy"
#####$0#####
arg.sh
#####$n#####
am
#####$*#####
i
am
old
boy
#####$@#####
i
am
old
boy
#####"$*"#####
i am old boy
#####"$@"#####
i
a
old boy
[[email protected] script]#
[[email protected] script]# b="w e"
[[email protected] script]# echo "$b"|sed -r 's# ##g'|wc -L
2
[[email protected] script]# echo "$b"|wc -L
3
[[email protected] script]# b=""
[[email protected] script]# echo "$b"|wc -L
0
[[email protected] script]# echo "$b"|sed -r 's# ##g'|wc -L
0
[[email protected] script]#
腳本内容
[[email protected] script]# cat cmpVar.sh
#!/bin/bash
#description:compare to variable
#author:vita
#version:v1.0
read -p "please input two number:" a b
#判斷是否輸入了第一個參數
if [ -z $a ];then
echo "you must input first number"
exit 6
fi
#當使用者輸入三個參數的時候,第一個空格後的内容都當做第二個參數,由于第二個參數中是含有空格的,可以去除空格比較長度
if [ $(echo "$b"|sed -r 's# ##g'|wc -L) -eq $(echo "$b"|wc -L) ];then
####不輸入第二個參數的時候,長度也是相同的,是以要對此情況做判斷
if [ -z $b ];then
echo "you must input second number"
exit 8
fi
#長度不相同,說明輸入了多個參數
elif [ $(echo "$b"|sed -r 's# ##g'|wc -L) -ne $(echo "$b"|wc -L) ];then
echo "you can just input two number"
exit 9
fi
##判斷輸入是否是整數
expr $a + $b + 2 &>/dev/null
if [ $? -ne 0 ];then
echo "the two number must be int"
exit 1
fi
#使用者輸入終于合法了,可以判斷大小了
if [ $a -gt $b ];then
echo "$a>$b"
exit 3
elif [ $a -eq $b ];then
echo "$a==$b"
exit 4
elif [ $a -lt $b ];then
echo "$a<$b"
exit 5
fi
[[email protected] script]#
測試腳本
[[email protected] script]# sh cmpVar.sh
please input two number:
you must input first number
[[email protected] script]# sh cmpVar.sh
please input two number:2
you must input second number
[[email protected] script]# sh cmpVar.sh
please input two number:2 3 4
you can just input two number
[[email protected] script]# sh cmpVar.sh
please input two number:2 a
the two number must be int
[[email protected] script]# sh cmpVar.sh
please input two number:w 3
the two number must be int
[[email protected] script]# sh cmpVar.sh
please input two number:2 3
2<3
[[email protected] script]# sh cmpVar.sh
please input two number:23 3
23>3
[[email protected] script]# sh cmpVar.sh
please input two number:23 23
23==23
讀檔案
•方式1:在while循環結尾done通過輸入重定向指定讀取的檔案。
while read line
do
cmd
done<FILE
•方式2:使用cat讀取檔案内容,然後通過管道進入while循環處理。
cat FILE_PATH|while read line
do
cmd
done
•方式3:采用exec讀取檔案後,然後進入while循環處理。
exec <FILE
sum=0
while read line
do
cmd
done
1.列印數組元素
此為常用知識點,需重點掌握。示例如下:
[[email protected] data]# array=(one two three)
[[email protected] data]# echo ${array[0]}
#<==列印單個數組元素用${數組名[下标]},當未指定數組下标時,數組的下标是從0開始。
one
[[email protected] data]# echo ${array[1]}
two
[[email protected] data]# echo ${array[2]}
three
[[email protected] data]# echo ${array[*]} #<==使用*或者@可以得到整個數組内容。
one two three
[[email protected] data]# echo ${array[@]} #<==使用*或者@可以得到整個數組内容。
one two three
2.列印數組元素的個數
此為常用知識點,需重點掌握。示例如下:
[[email protected] data]# echo ${array[*]} #<==使用*或者@可以得到整個數組内容。
one two three
[[email protected] data]# echo ${#array[*]} #<==用${#數組名[@或*]}可以得到數組長度,這和前文講解的變量子串知識是一樣的,因為數組也是變量,隻不過是特殊的變量,是以也适合變量的子串替換等知識。
3
[[email protected] data]# echo ${array[@]} #<==使用*或者@可以得到整個數組内容。
one two three
[[email protected] data]# echo ${#array[@]} #<==用${#數組名[@或*]}可以得到數組長度,這和前文講解的變量子串知識是一樣的,因為數組也是變量,隻不過是特殊的變量,是以也适合變量的子串替換等知識。
3
3.數組指派
此知識不常用,了解即可。可直接通過“數組名[下标] ”對數組進行引用指派,如果下标不存在,自動添加新一個數組元素,如果下标存在就覆寫原來的值。
示例如下:
[[email protected] data]# array=(one two three)
[[email protected] data]# echo ${array[*]}
one two three
[[email protected] data]# array[3]=four #<==增加下标為3的數組元素。
[[email protected] data]# echo ${array[*]}
one two three four
[[email protected] data]# array[0]=oldboy
[[email protected] data]# echo ${array[*]}
oldboy two three four
[r[email protected] ~]# array[0]=oldboy #<==修改數組元素。
[[email protected] ~]# echo ${array[@]}
oldboy 2 3 4
4.數組的删除
因為數組本質上還是變量,是以可通過“unset 數組[下标]”清除相應的數組元素,如果不帶下标,表示清除整個數組的所有資料。
示例如下:
[[email protected] data]# echo ${array[*]}
oldboy two three four
[[email protected] data]# unset array[1] #<==取消下标為1的數組元素。
[[email protected] data]# echo ${array[*]} #<==列印輸出後發現數組元素“two”,不見了。
oldboy three four
[[email protected] data]# unset array #<==删除整個數組。
[[email protected] data]# echo ${array[*]}
#<==沒有任何内容了。
5.數組内容的截取和替換
這裡和前文變量子串的替換是一樣的,因為數組是特殊的變量。數組元素部分内容截取的示例如下:
[[email protected] ~]# array=(1 2 3 4 5)
[[email protected] ~]# echo ${array[@]:1:3} #<==從下标為1的元素開始截取,共取3個數組元素。
2 3 4
[[email protected] data]# array=({a..z}) #<==将變量的結果指派給數組變量。
[[email protected] data]# echo ${array[@]}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[[email protected] data]# echo ${array[@]:1:3} #<==從下标為1的元素開始截取,共取3個數組元素。
b c d
[[email protected] data]# echo ${array[@]:0:2} #<==從下标為0的元素開始截取,共取2個數組元素。
a b
數組元素部分内容的替換如下:
[[email protected] data]# array=(1 2 3 1 1)
[[email protected] data]# echo ${array[@]/1/b} #<==把數組中的1替換成b,原數組未被修改,和sed很像。
b 2 3 b b
提示:調用方法是:${數組名[@或*]/查找字元/替換字元} 該操作不會改變原先數組内容,如果需要修改,可以看上面例子,重新定義數組。
數組元素部分内容的删除如下:
[[email protected] data]# array=(one two three four five)
[[email protected] data]# echo ${array[@]}
one two three four five
[[email protected] data]# echo ${array[@]#o*} #<==從左邊開始比對最短的,并删除。
ne two three four five
[[email protected] data]# echo ${array1[@]##o*} #<==從左邊開始比對最長的,并删除。
two three four five
[[email protected] data]# echo ${array[@]%f*} #<==從右邊開始比對最短的,并删除。
one two three
[[email protected] data]# echo ${array[@]%%f*} #<==從右邊開始比對最長的,并删除。
one two three
提示:數組也是變量,是以也适合于前面講解過的變量的子串處理的功能應用。
數組的其他相關知識通過man bash然後搜Arrays來了解。
[[email protected] script]# cat 6.mysqlTable.sh
#!/bin/bash
##############################################################
# File Name: 6.mysqlTable.sh
# Version: V1.0
# Author: vita
# Organization:
# Created Time : 2019-04-01 15:39:12
# Description:
##############################################################
mysqlCommand="/application/mysql/bin/mysql -uroot -poldboy123"
mysqlDump="/application/mysql/bin/mysqldump -uroot -poldboy123"
databases=$($mysqlCommand -e "show databases;" 2>/dev/null |grep -Ev 'Database|schema')
for database in $databases;do
for table in $($mysqlCommand -e "show tables from $database;" 2>/dev/null|sed 1d );do
$mysqlDump -B $database $table |gzip>./log/${database}_${table}.sql.gz 2>/dev/null
done
done
13.7SSH免秘鑰批量分分發檔案
[[email protected] script]# cat 7.distributeFile.sh
#!/bin/bash
##############################################################
# File Name: 7.distributeFile.sh
# Version: V1.0
# Author: vita
# Organization:
# Created Time : 2019-04-01 15:57:04
# Description:
##############################################################
source /etc/init.d/functions
[ $# -ne 2 ]&&{
echo "Usage $0 localpath remotepath"
exit 1
}
for ip in 7;do
scp -r $1 [email protected].$ip:$2
[ $? -eq 0 ]&&{
action "10.0.0.$ip has recevied file" /bin/true
}||{
action "10.0.0.$ip has not received file" /bin/false
}
done
[[email protected] script]#
[[email protected] script]# sh 7.distributeFile.sh /oldboy/ /ol
10.0.0.7 has recevied file [ OK ]
[[email protected] script]# sh 7.distributeFile.sh /oldb /ol
/oldb: No such file or directory
10.0.0.7 has not received file [FAILED]
13.8破解RAND
這個是最快的,因為循環少了很多
[[email protected] script]# cat 8-2.encodeRandom.sh
#!/bin/bash
##############################################################
# File Name: 8.encodeRandom.sh
# Version: V1.0
# Author: vita
# Organization:
# Created Time : 2019-04-01 16:08:14
# Description:
##############################################################
array=(21029299 00205d1c a3da1677 1f6d12dd 890684b)
for i in {1..32767};do
echo "$i $(echo "$i"|md5sum)">>log/encode.log
done
for md5Num in ${array[*]};do
echo "$(grep "$md5Num" log/encode.log)"
done
[[email protected] script]#
[[email protected] script]# sh 8-2.encodeRandom.sh
25667 2102929901ee1aa769d0f479d7d78b05 -
1346 00205d1cbbeb97738ad5bbdde2a6793d -
25345 a3da1677501d9e4700ed867c5f33538a -
7041 1f6d12dd61b5c7523f038a7b966413d9 -
10082 890684ba3685395c782547daf296935f -
不用數組
[[email protected] script]# cat 8.encodeRandom.sh
#!/bin/bash
##############################################################
# File Name: 8.encodeRandom.sh
# Version: V1.0
# Author: vita
# Organization:
# Created Time : 2019-04-01 16:08:14
# Description:
##############################################################
for i in {1..32767};do
for md5Num in 21029299 00205d1c a3da1677 1f6d12dd 890684b;do
num=$(echo "$i"|md5sum|grep "$md5Num"|wc -l)
if [ $num -eq 0 ];then
continue
else
echo "${i}----${md5Num}"
continue
fi
done
done
使用數組
[[email protected] script]# cat 8-1.encodeRandom.sh
#!/bin/bash
##############################################################
# File Name: 8.encodeRandom.sh
# Version: V1.0
# Author: vita
# Organization:
# Created Time : 2019-04-01 16:08:14
# Description:
##############################################################
array=(21029299 00205d1c a3da1677 1f6d12dd 890684b)
for i in {1..32767};do
for md5Num in ${array[*]};do
num=$(echo "$i"|md5sum|grep "$md5Num"|wc -l)
if [ $num -eq 0 ];then
continue
else
echo "${i}----${md5Num}"
continue
fi
done
done
[[email protected] script]#
[[email protected] script]# sh 8-1.encodeRandom.sh
1346----00205d1c
7041----1f6d12dd
10082----890684b
25345----a3da1677
25667----21029299
13.9監測url
[[email protected] script]# cat 9.checkUrl.sh
#!/bin/bash
##############################################################
# File Name: 9.checkUrl.sh
# Version: V1.0
# Author: vita
# Organization:
# Created Time : 2019-04-01 17:12:15
# Description:
##############################################################
source /etc/init.d/functions
url_list=(
http://blog.oldboyedu.com
http://blog.etiantian.org
http://oldboy.blog.51cto.com
http://10.0.0.7
)
checkUrl() {
curl -s -t 3 -o /dev/null $1
[ $? -eq 0 ]&&{
action "$1 can be accessed!" /bin/true
}||{
action "$1 can not be accessed!" /bin/false
}
}
dealUrl() {
for url in ${url_list[*]};do
checkUrl $url
done
}
main() {
dealUrl
}
main
[[email protected] script]#
[[email protected] script]# sh 9.checkUrl.sh
http://blog.oldboyedu.com can be accessed! [ OK ]
http://blog.etiantian.org can not be accessed! [FAILED]
http://oldboy.blog.51cto.com can be accessed! [ OK ]
http://10.0.0.7 can not be accessed! [FAILED]
13.10防止DOS
[[email protected] oldboy]# cat access_2010-12-8.log |awk -F '[ ]' '{IP[$1]++}END{for (key in IP) print key,IP[key]}'
59.33.26.105 44
123.122.65.226 31
124.115.4.18 40
[[email protected] script]# cat 10-Dos.sh
#!/bin/bash
##############################################################
# File Name: 10-Dos.sh
# Version: V1.0
# Author: vita
# Organization:
# Created Time : 2019-04-01 17:58:54
# Description:
##############################################################
awk '{IP[$1]++}END{for (key in IP) print key,IP[key]}' ./log/access_2010-12-8.log>./log/dosResult.log
exec < ./log/dosResult.log
while read line;do
times=$(echo "$line"|awk '{print $2}')
ip=$(echo "$line"|awk '{print $1}')
if [ $times -gt 10 ];then
iptables -A INPUT -s $ip -j DROP
fi
done
[[email protected] script]#
13.11按單詞排序
[[email protected] log]# awk -F "[ .,]" '{for (i=1;i<NF;i++)array[$i]++}END{for (word in array)print array[word],word}' 12.log |sort -rn
2 the
2 support
2 squid
2 and
1 users
1 training
1 to
1 sections
1 resources
1 provides
方法二:
[[email protected] log]# tr " |,|." "\n" <12.log |awk '{array[$1]++}END{for (key in array)print array[key],key}'|sort -rn
2 the
2 support
2 squid
2 and
2
1 users
1 training
1 to
1 sections
1 resources
1 provides
1 project
[[email protected] log]# awk -F "" '{for (i=1;i<=NF;i++)array[$i]++}END{for (word in array)print word,array[word]}' 12.log |sort -rn|grep -Ev '^ |^,|^\.'|sort -n
a 10
b 4
c 4
d 8
e 17
f 3
g 2
h 2
i 14
j 1
l 5
m 6
n 14
o 18
P 1
p 7
q 2
r 12
s 19
t 15
u 8
v 1
w 1
y 2
[[email protected] log]# grep -o "[a-Z]" 12.log
t
h
e
s
q
u
i
13.12MYSQL啟動腳本
#!/bin/bash
# chkconfig: 2345 21 81
##############################################################
# File Name: mysqld
# Version: V1.0
# Author: oldboy
# Organization: www.oldboyedu.com
##############################################################
lockfile=/var/lock/subsys/mysqld
. /etc/init.d/functions
mysqld_pid_file_path="/application/mysql/data/web01.pid"
mysqld_safe=/application/mysql/bin/mysqld_safe
start(){
/bin/sh $mysqld_safe --datadir=/application/mysql/data --pid-file=$mysqld_pid_file_path &>/dev/null &
retval=$?
sleep 2
if [ $retval -eq 0 ]
then
action "mysql startup ok" /bin/true
touch $lockfile
return $retval
else
action "mysql startup fail" /bin/false
return $retval
fi
}
stop(){
if test -s "$mysqld_pid_file_path"
then
mysqld_pid=`cat $mysqld_pid_file_path`
if (kill -0 $mysqld_pid &>/dev/null)
then
kill $mysqld_pid
retval=$?
sleep 2
if [ $retval -eq 0 ]
then
action "mysql stop ok" /bin/true
rm $lockfile
return $retval
else
action "mysql stop fail" /bin/false
return $retval
fi
else
echo " mysqld process is not exist."
return 2
fi
else
echo "$mysqld_pid_file_path is not exist,or mysqld does not startup."
fi
}
case "$1" in
start)
start
retval=$?
;;
stop)
stop
retval=$?
;;
restart)
stop
sleep 2
start
retval=$?
;;
*)
echo "usage:$0 {start|stop|restart}"
exit 1
esac
exit $retval