1.函數(shù)的作用:
1.函數(shù)是命令的集合 是一個完成特定功能的代碼塊
函數(shù)是使用一個名稱 來定義一堆命令
2.函數(shù)便于復(fù)用 可以在任意的shell中調(diào)用函數(shù) 類似functions
3.函數(shù)類似變量 只能先定義 在調(diào)用 如果不調(diào)用則不會執(zhí)行 shell中只要定義了就會執(zhí)行
1)函數(shù)的傳參
2)函數(shù)的本地變量
3)函數(shù)的返回值
2.定義函數(shù)的三種方法
#!/bin/bash
fun1(){
echo "第一種定義函數(shù)的方法"
}
function fun2 {
echo "第二種定義函數(shù)的方法"
}
function fun3(){
echo "第三種定義函數(shù)的方法"
}
#調(diào)用函數(shù)直接寫函數(shù)名稱即可 必須先定義在調(diào)用
fun1
fun2
fun3
2.1其他腳本中調(diào)用
root@shell /server/scripts/day5]# cat test.sh
#!/bin/sh
. ./fun.sh
fun1
echo done................
[root@shell /server/scripts/day5]# sh test.sh
第一種定義函數(shù)的方法
done................
2.2舉例
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
if [ -f $1 ];then
echo "$1 存在"
else
echo "$1 不存在"
fi
}
fun /etc/passwd
[root@shell /server/scripts/day5]# sh fun.sh
/etc/passwd 存在
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
if [ -f $2 ];then
echo "$2 存在"
else
echo "$2 不存在"
fi
}
fun /etc/passwd /etc/hosts
[root@shell /server/scripts/day5]# sh fun.sh
/etc/hosts 存在
2.3函數(shù)的腳本傳參
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
if [ -f $1 ];then
echo "$1 存在"
else
echo "$1 不存在"
fi
}
fun $1
[root@shell /server/scripts/day5]# sh fun.sh /etc/passwd
/etc/passwd 存在
[root@shell /server/scripts/day5]# sh fun.sh /etc/passwddddd
/etc/passwddddd 不存在
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
if [ -f $1 ];then
echo "$1 存在"
else
echo "$1 不存在"
fi
}
fun $2 $1
[root@shell /server/scripts/day5]# sh fun.sh /etc/hosts /etc/passwd
/etc/passwd 存在
2.4賦值
#!/bin/bash
num=10
fun(){
for i in `seq 10`
do
total=$[$num+$i]
total=10+1
total=11
total=10+2
total=12
total=10+3
total=13
total=$[$total+$i]
done
echo 計算結(jié)果為 $total
}
fun
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
num=10
fun(){
for i in `seq 10`
do
total=$[$num+$i]
done
echo 計算結(jié)果為 $total
}
fun
[root@shell /server/scripts/day5]# sh fun.sh
計算結(jié)果為 20
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
for i in `seq 10`
do
total=$[$1+$i]
done
echo 計算結(jié)果為 $total
}
fun $3
fun $2
fun $1
[root@shell /server/scripts/day5]# sh fun.sh 10 20 30
計算結(jié)果為 40
計算結(jié)果為 30
計算結(jié)果為 20
2.5只在當(dāng)前的fun函數(shù)生效
函數(shù)的本地變量 local
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
local num=10 # 只在當(dāng)前的fun函數(shù)中生效
for i in `seq 10`
do
total=$[$num+$i]
done
echo 計算結(jié)果為 $total
}
fun
echo $num
[root@shell /server/scripts/day5]# sh fun.sh
計算結(jié)果為 20
3.函數(shù)返回值 return
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
echo 100
return 1
}
result=`fun`
echo "當(dāng)前函數(shù)的返回值是: " $?
echo "當(dāng)前函數(shù)的直接結(jié)果是: " $result
[root@shell /server/scripts/day5]# sh fun.sh
當(dāng)前函數(shù)的返回值是: 1
當(dāng)前函數(shù)的直接結(jié)果是: 100
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
echo 100
return 1
}
fun
echo "當(dāng)前函數(shù)的返回值是: " $?
echo "當(dāng)前函數(shù)的直接結(jié)果是: " $result
[root@shell /server/scripts/day5]# sh fun.sh
100
當(dāng)前函數(shù)的返回值是: 1
當(dāng)前函數(shù)的直接結(jié)果是:
3.1案例: 使用返回值來判斷文件是否存在
[root@shell /server/scripts/day5]# sh fun.sh /etc/passwd
[root@shell /server/scripts/day5]# echo $?
50
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
if [ -f $1 ];then
return 50
else
return 100
fi
}
fun $1
[root@shell /server/scripts/day5]# sh fun.sh /etc/passwdddddddddddddd
[root@shell /server/scripts/day5]# echo $?
100
result=$?
if [ $result -eq 50 ];then
echo "$1 文件存在"
elif [ $result -eq 100 ];then
echo "$1 文件不存在"
fi
fun $1
case $? in
50)
echo "$1 文件存在"
;;
100)
echo "$1 文件不存在"
;;
*)
echo USAGE
esac
4.數(shù)組
普通數(shù)組
關(guān)聯(lián)數(shù)組
4.1普通數(shù)組
數(shù)組語法結(jié)構(gòu):
變量=值
禮盒=一個水果
禮盒=蘋果 禮盒=梨子 禮盒=香蕉 禮盒=茄子
數(shù)組=多個值
數(shù)組名稱[索引名稱]=值 索引名稱默認(rèn)從0開始 索引就是值的編號 索引也稱為下標(biāo)
框子[禮盒1]=蘋果
框子[禮盒2]=梨
框子[禮盒3]=黃瓜
4.2普通數(shù)組的定義方式
第一種:
[root@shell ~]# array[1]=shell
[root@shell ~]# array[2]=mysql
[root@shell ~]# array[3]=docker
第二種定義方式 使用默認(rèn)方式定義多個值 常用的定義方式
array=(shell mysql docker)
array=(10.0.0.1 10.0.0.2 10.0.0.7 10.0.0.61)
第三種定義 混合定義
array=(shell mysql [5]=docker [10]=kvm)
第四種定義 命令
array=(`ls`)
[root@shell /opt]# ll
total 0
-rw-r--r-- 1 root root 0 Jun 30 11:04 1.txt
-rw-r--r-- 1 root root 0 Jun 30 11:04 2.txt
-rw-r--r-- 1 root root 0 Jun 30 11:04 3.txt
declare -a array='([0]="1.txt" [1]="2.txt" [2]="3.txt")'
4.3如何查看數(shù)組
如何查看數(shù)組
declare -a # 在內(nèi)存中定義的數(shù)組
取消數(shù)組
unset array
查看數(shù)組中的值
[root@shell ~]# array=(mysql docker shell)
[root@shell ~]#
[root@shell ~]# echo ${array[*]}
mysql docker shell
[root@shell ~]# echo ${array[@]}
mysql docker shell
查看某個索引中的值
[root@shell ~]# echo ${array[2]}
shell
[root@shell ~]#
[root@shell ~]# echo ${array[0]}
mysql
[root@shell ~]# echo ${array[1]}
docker
查看數(shù)組的索引
[root@shell ~]# echo ${!array[*]}
0 1 2
查看總共的索引數(shù)量
[root@shell ~]# echo ${#array[*]}
3
案例
[root@shell ~]# array=(10.0.0.1 10.0.0.2 10.0.0.7)
[root@shell ~]# echo ${array[*]}
10.0.0.1 10.0.0.2 10.0.0.7
[root@shell ~]#
[root@shell ~]#
[root@shell ~]# echo ${array[*]}
10.0.0.1 10.0.0.2 10.0.0.7
[root@shell ~]#
[root@shell ~]#
[root@shell ~]# for i in ${array[*]};do echo $i;done
10.0.0.1
10.0.0.2
10.0.0.7
[root@shell ~]# for i in ${array[*]};do ping -c1 -W1 $i;done
[root@shell /server/scripts/day5]# cat array.sh
#!/bin/sh
ip=(
10.0.0.1
10.0.0.2
10.0.0.7
)
for i in ${ip[*]}
do
ping -c1 -W1 $i &>/dev/null
[ $? -eq 0 ] && echo "$i 是通的"
done
[root@shell /server/scripts/day5]# sh array.sh
10.0.0.1 是通的
10.0.0.2 是通的
10.0.0.7 是通的
使用下標(biāo)遍歷數(shù)組
[root@shell /server/scripts/day5]# cat array.sh
#!/bin/sh
ip=(
10.0.0.1
10.0.0.2
10.0.0.7
)
for i in ${!ip[*]}
do
ping -c1 -W1 ${ip[$i]} &>/dev/null
[ $? -eq 0 ] && echo "${ip[$i]} 是通的"
done
[root@shell /server/scripts/day5]# sh array.sh
10.0.0.1 是通的
10.0.0.2 是通的
10.0.0.7 是通的
4.4關(guān)聯(lián)數(shù)組:
關(guān)聯(lián)數(shù)組和普通數(shù)組的區(qū)別
普通數(shù)組只能使用數(shù)字作為索引
關(guān)聯(lián)數(shù)組可以使用字符串作為索引
定義方式和普通數(shù)組相同 不同是要聲明關(guān)聯(lián)數(shù)組
必須先聲明為關(guān)聯(lián)數(shù)組
[root@shell ~]# declare -A array
[root@shell ~]# array[index1]=shell
[root@shell ~]# array[index2]=mysql
[root@shell ~]# array[index3]=docker
[root@shell ~]# echo ${array[*]}
shell mysql docker
[root@shell ~]# echo ${!array[*]}
index1 index2 index3
去大保健找了幾個妹子 妹子每天服務(wù)次數(shù)不同 問每個妹子每天服務(wù)了多少次!
小紅
小紅
小紫
小綠
小紫
小蘭
直接運(yùn)算
let 小紅++
let 小紅++
let 小紫++
echo $小紅 === 2次
第一步: 上面的人名 對應(yīng)了數(shù)組的索引
我們?nèi)サ氖羌t浪漫的場地
紅浪漫[0]=小紅
紅浪漫[1]=小紫
紅浪漫[2]=小綠
紅浪漫[3]=小蘭
#!/bin/sh
declare -A honglangman
for i in `cat honglang.txt`
do
let honglangman{$i}++
let honglangman{小紅}++
let honglangman{小紫}++
let honglangman{小綠}++
done
for i in ${!honglangman[*]} #查看紅浪漫里面總共多少個姑娘 每個姑娘的名字
do
echo $i
done
[root@shell ~]# cat honglang.sh
#!/bin/sh
declare -A honglangman
for i in `cat honglang.txt`
do
let honglangman[$i]++
done
for i in ${!honglangman[*]} #查看紅浪漫里面總共多少個姑娘 每個姑娘的名字
do
echo $i 出現(xiàn)了 ${honglangman[$i]} 次
done
[root@shell ~]# sh honglang.sh
小蘭 出現(xiàn)了 1 次
小紫 出現(xiàn)了 2 次
小綠 出現(xiàn)了 1 次
小紅 出現(xiàn)了 3 次
[root@shell ~]# cat honglang.txt
小紅
小紅
小紫
小綠
小紫
小蘭
小紅
統(tǒng)計web的服務(wù)器IP地址出現(xiàn)的次數(shù)
[root@shell ~]# cat for.sh
#!/bin/sh
declare -A array
while read line
do
ip=`echo $line|awk '{print $1}'`
let array[$ip]++
done</var/log/nginx/access.log
for i in ${!array[*]}
do
echo $i 出現(xiàn)了 ${array[$i]} 次
done
[root@shell ~]# cat for.sh
#!/bin/sh
declare -A array
while read line
do
ip=`echo $line|awk '{print $1}'`
let array[$ip]++
done</var/log/nginx/access.log
for i in ${!array[*]}
do
echo $i 出現(xiàn)了 ${array[$i]} 次
done
[root@shell ~]# sh for.sh
172.16.1.61 出現(xiàn)了 51 次
127.0.0.1 出現(xiàn)了 998 次
10.0.0.61 出現(xiàn)了 100 次
取出隨機(jī)數(shù)的對應(yīng)的數(shù)字
echo $RANDOM|md5sum|cut -c1-8
cf7e5204 81acd18d 19eb7c1e f519ac25 3c87c04a 52867e9 27bee3b
1232 f519ac25
6166 19eb7c1e
7186 27bee3b8
9746 cf7e5204
11411 81acd18d
[root@shell ~]# cat for.sh
#!/bin/sh
for i in `echo {0..32767}`
do
ran=`echo $i|md5sum|cut -c1-8`
if [[ "$ran" =~ "cf7e5204" || "$ran" =~ "81acd18d" || "$ran" =~ "19eb7c1e" || "$ran" =~ "52867e9" || "$ran" =~ "27bee3b" || "$ran" =~ "f519ac25" || "$ran" =~ "3c87c04a" ]];then
echo $i $ran
fi
done
輸入姓名 抓鬮1-100隨機(jī)生成數(shù)
[root@shell ~]# cat ran.sh
#!/bin/sh
> z.txt
echo "抓鬮開始小板凳準(zhǔn)備好"
while true
do
read -p "請輸入你的姓名: " name
if [ $name = "exit" ];then
clear
echo "抓鬮結(jié)束排名如下"
cat z.txt|sort -rnk 3|head
exit
fi
while true
do
ran=`echo $((RANDOM%100+1))`
if [ `cat z.txt|grep -w $ran|wc -l` -eq 1 ];then
continue
else
break
fi
done
echo "$name 幸運(yùn)號碼 $ran"|tee -a z.txt
done