0
点赞
收藏
分享

微信扫一扫

shell脚本函数

boom莎卡拉卡 2021-09-23 阅读 101

1.函数的作用:

1.函数是命令的集合 是一个完成特定功能的代码块
函数是使用一个名称 来定义一堆命令
2.函数便于复用 可以在任意的shell中调用函数 类似functions
3.函数类似变量 只能先定义 在调用 如果不调用则不会执行  shell中只要定义了就会执行
1)函数的传参
2)函数的本地变量
3)函数的返回值

2.定义函数的三种方法

#!/bin/bash
fun1(){
     echo "第一种定义函数的方法"
}

function fun2 {
     echo "第二种定义函数的方法"
}
function fun3(){
     echo "第三种定义函数的方法"
}
#调用函数直接写函数名称即可 必须先定义在调用
fun1
fun2
fun3
2.1其他脚本中调用
root@shell /server/scripts/day5]# cat test.sh
#!/bin/sh
. ./fun.sh
fun1
echo done................
[root@shell /server/scripts/day5]# sh test.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函数的脚本传参
[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 计算结果为 $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 计算结果为 $total
}
fun
[root@shell /server/scripts/day5]# sh fun.sh
计算结果为 20

[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
     for i in `seq 10`
     do
           total=$[$1+$i]
     done
     echo 计算结果为 $total
}
fun $3
fun $2
fun $1
[root@shell /server/scripts/day5]# sh fun.sh 10 20 30
计算结果为 40
计算结果为 30
计算结果为 20
2.5只在当前的fun函数生效
函数的本地变量 local
[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
local num=10                # 只在当前的fun函数中生效
     for i in `seq 10`
     do
           total=$[$num+$i]
     done
     echo 计算结果为 $total
}
fun
echo $num
[root@shell /server/scripts/day5]# sh fun.sh
计算结果为 20

3.函数返回值 return

[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
  echo 100
  return 1
}
result=`fun`
echo "当前函数的返回值是: " $?
echo "当前函数的直接结果是: " $result
[root@shell /server/scripts/day5]# sh fun.sh
当前函数的返回值是:  1
当前函数的直接结果是:  100

[root@shell /server/scripts/day5]# cat fun.sh
#!/bin/bash
fun(){
  echo 100
  return 1
}
fun
echo "当前函数的返回值是: " $?
echo "当前函数的直接结果是: " $result
[root@shell /server/scripts/day5]# sh fun.sh
100
当前函数的返回值是:  1
当前函数的直接结果是: 
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.数组

普通数组

关联数组
4.1普通数组
数组语法结构:
变量=值
礼盒=一个水果
礼盒=苹果  礼盒=梨子 礼盒=香蕉 礼盒=茄子

数组=多个值
数组名称[索引名称]=值     索引名称默认从0开始  索引就是值的编号 索引也称为下标
框子[礼盒1]=苹果
框子[礼盒2]=梨
框子[礼盒3]=黄瓜
4.2普通数组的定义方式
第一种:

[root@shell ~]# array[1]=shell
[root@shell ~]# array[2]=mysql
[root@shell ~]# array[3]=docker

第二种定义方式  使用默认方式定义多个值  常用的定义方式
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如何查看数组
如何查看数组
declare -a   # 在内存中定义的数组

取消数组
unset array


查看数组中的值
[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

查看数组的索引
[root@shell ~]# echo ${!array[*]}
0 1 2
查看总共的索引数量
[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 是通的


使用下标遍历数组
[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关联数组:
关联数组和普通数组的区别
普通数组只能使用数字作为索引
关联数组可以使用字符串作为索引
定义方式和普通数组相同 不同是要声明关联数组
必须先声明为关联数组
[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
去大保健找了几个妹子 妹子每天服务次数不同 问每个妹子每天服务了多少次!
小红
小红
小紫
小绿
小紫
小兰

直接运算
let 小红++
let 小红++
let 小紫++

echo $小红 === 2次

第一步: 上面的人名 对应了数组的索引
我们去的是红浪漫的场地
红浪漫[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 出现了 ${honglangman[$i]} 次
done
[root@shell ~]# sh honglang.sh
小兰 出现了 1 次
小紫 出现了 2 次
小绿 出现了 1 次
小红 出现了 3 次
[root@shell ~]# cat honglang.txt 
小红
小红
小紫
小绿
小紫
小兰
小红
统计web的服务器IP地址出现的次数

[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 出现了 ${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 出现了 ${array[$i]} 次
done

[root@shell ~]# sh for.sh 
172.16.1.61 出现了 51 次
127.0.0.1 出现了 998 次
10.0.0.61 出现了 100 次
取出随机数的对应的数字
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随机生成数
[root@shell ~]# cat ran.sh
#!/bin/sh
> z.txt
echo "抓阄开始小板凳准备好"
while true
do
    read -p "请输入你的姓名: " name
        if [ $name = "exit" ];then
           clear
           echo "抓阄结束排名如下"
           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 幸运号码 $ran"|tee -a z.txt
done
举报

相关推荐

0 条评论