Liunx shell编程及自动化运维实现--第四章数组和函数

read  -p   -s      -s可以隐藏输入的内容,加上echo "" 可换行

一、数组

数组简介:

        变量:用一个固定的字符串,代替一个不固定的字符串

        数组:用一个固定的字符串,代替多个不固定的字符串

类型:

  • 普通数组:只能使用整数作为数组索引,按顺序调用
  • 关联数组:可以使用字符串作为数组索引,无法排序

${hero[1]}

declare -A position 

position=([up]=baiqi [center]=anqila [down]=houyi)

¥{}

1.普通数组

1.1定义数组

方法一:一次附一个值

数组名[下标]=变量值

array[0]=pear

array[1]=apple

array[2]=orange

array[3]=peach

查看数组

declare  -a | grep array

echo  ${array[@]}

echo  ${array[*]}

echo  ${array[0]}

echo  ${!array[@]}   查看下标

方法二:一次定义一组

array1=(tom jack alice)

array2=(`cat /etc/passwd`)                希望是将该文件中的每一个行作为一个元素给数组

array3=(`ls /var/ftp/Shell/for*`)

array4=(tom jack alice "bash shell")

red=111  blue=222

colors=($red $blue)

array5=(0 1 2 3 4 5 6 7 "Liunx shell" [20]=saltstack)        可以设置索引号为20

1.2访问数组元素

echo  ${array1[0]}    访问数组的第一个元素

echo  ${array1[@]}   访问数组中的所有元素,等同于${array1[*]}

echo  ${#array1[@]}  统计数组元素的个数

echo  ${!array2[@]}   获取数组元素的索引

echo  ${array1[@]:1}   从数组下标1开始

echo  ${array1[@]:1:2}   从数组下标1开始,访问两个元素

2.关联数组

2.1定义关联数组

方法一:先声明关联数组

数组名[索引]=变量名

declare  -A   ass_array1            定义申明该数组为关联数组

ass_array1[index1]=pear

ass_array1[index2]=apple

ass_array1[index3]=orange

ass_array1[index4]=peach

查看

echo  ${ass_array1[*]}

方法二:

declare  -A   ass_array2

ass_array2=([index1]=tom [index2]=jack [index3]=alice)

2.2查看数组

declare  -A

2.3访问数组元素

echo $(ass_array2[index2]}           访问数组中的第二个元素

echo $(ass_array2[@]}                 访问数组中所有元素,等同于echo ${array1[*]}

echo $(#ass_array2[@]}                获得数组元素的个数

echo $(!ass_array2[@]}                 获得数组元素的索引

3.数组和循环

通过循环定义和显示数组

通过数组统计数据

4.案例

i++ 从0开始   ++i从1开始

1.for脚本快速定义数组

#!/bin/bash

#定义数组
for  aaa in `cat /etc/hostss`
do
hosts[i++]=$aaa
done

#调用数组,查看
for i in ${!hosts[*]}
do
echo  "第 $i 个索引,它的值为: ${hosts[$i]} "
done

如何解决for的空格分割的问题,使用IFS=$'\n' 重新定义分隔符

另外,如果脚本中还有for怎么办?存储变量,还原变量

2.数组统计性别

定义性别文件

vim  sex.txt

jack   m    henan

alice  f     beijing

tom    m    hebei

定义脚本统计性别

awk  '{print $1}' sex.txt                 $几代表第几列

while  用read进行输入重定向  一次读一行

[root@192 ~]# cat while.sh 
#!/bin/bash
while read aaa
do
echo $aaa
done < sex.txt
[root@192 ~]# bash while.sh
jack m heibei
tom f beijing
alice f shangdong
[root@192 ~]# 

for循环是根据空格和回车进行分割

#!/bin/bash
declare -A sex

while read line 
do
type=`echo $line | awk '{print $2}'`
let sex[$type]++
done < sex.txt

for i in ${!sex[*]}
do 
echo "suoyin $i : zhi: ${sex[$i]} "
done

3.使用数组统计,用户shell的类型和数量

!/bin/bash
declare -A shells
while  read line
do 

type=`echo $line | awk -F: '{print $7}'`                 -F分隔符  ¥7 第七列
let shells[$type]++
done < /etc/passwd

echo ${shells[*]}
echo ${!shells[*]}

4.while脚本快速定义数组

#!/bin/bash                #hosts文件内容的行数,成为了while的循环次数
while read line
do 
hosts[++i]=$line
done < /etc/hosts

for i in ${!hosts[*]}
do
echo $i ${hosts[$i]}
done

二、函数

概述

概念:函数是一段完成特定功能的代码片段(块),在shell中定义了函数,就可以使代码模块化,便于复用代码,注意函数必须先定义才可以使用

重点:传参$1,$2 局部变量local   返回值return $?

1.定义函数

方法一:

函数名 (){

函数要实现的功能代码

}

方法二:

function  函数名{

函数要实现的功能代码

}

2.调用函数

函数名

函数名  参数1  参数2

3.示例

3.1初识函数

需求:通过shell脚本,编写系统工具箱编写循环脚本,功能菜单

#!/bin/bash

function jianjie {
cat <<EOF
----------system tools----------
1.cipan info
2.neicun info
3.cpu  info
4.wangluo  info
5.jincheng  info
6.quit
----------system tools----------
EOF
}

function isnotexit {
read -p "jixu Y,tuichu N:" yn
if [ "$yn" =  Y ]
then clear
else  exit 66
fi
}


while :
do

jianjie

read -p "please input your choose:" num

case $num in 
1)
echo "===========cipan info=========="
df -hT
echo "===========cipan info=========="

isnotexit

;;
2)
echo "===========neicun info=========="
free -m
echo "===========neicun info=========="
isnotexit
;;
3)
echo "===========CPU info=========="
uptime
echo "===========CPU info=========="
isnotexit
;;
4)
echo "===========wangluo info=========="
read -p "please input select nete post:" hao
netstat -anpt | head -2 
netstat -anpt | grep $hao
echo "===========wangluo info=========="
isnotexit
;;
5)
echo "===========jincheng info=========="
read -p "please input select jingcheng name:" pss
ps aux  | head -1
ps  aux  |  grep $pss
echo "===========jincheng info=========="
isnotexit
;;
6)
exit  88
;;
*)

jianjie

;;
esac

done
 

3.2阶乘函数(传参)

制作函数用于阶乘

#!/bin/bash
fun1 () {
fff=1

#for (( i=1 ; i <= $1 ; i++  ))

for  i  in  ` seq $1`
do
#fff=$[$fff*$i]

fff*=$i
done

echo "$1 jiecheng:$fff"
}

fun1

3.3函数传参,数组传参

制作一个简单的阶乘脚本,通过数组给函数传参

#!/bin/bash
num=(1 2 3)
array () {

fac=1

for i in $*
do
fac=$[$fac*$i]
done

echo $fac
}

array ${num[*]}

数组的好处在于,多个数组时传参的效率就增高了

3.4函数结果,赋予数组

场景:用户获赠流量包(每人增加5G),结果运算

通过函数输出到数组

#!/bin/bash
num=(1 2 3)

array (){

local j

for i  in $* 
do
outarray[j++]=$[$i+5]
done

echo ${outarray[*]}

}

array ${num[*]}

总结:函数不仅可以从数组中调取值,还可以赋予数组值

三、影响Shell程序的内置命令

true

false

exit    退出整个程序

break   结束当前循环,或跳出本层循环  break 2  跳出两层循环

continue   忽略本次循环剩余的的代码,直接进行下一次循环

shift  使位置参数向左移动,默认移动1位,可以使用shift 2

echo默认输出换行符,echo  -n默认不输出换行符

循环嵌套的规则:外部循环一次,内部循环所有

for如果没有取值范围,则会调用程序的参数作为循环范围

shift

1for 循环不定义循环范围,循环取参数作为循环范围。

2 使用while循环,发现停不下来

3使用shift 移动参数的命令。结果得以实现

4另一个,创建用户的案例。理解参数位移

  • 28
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值