$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