【Shell专项】数组与函数的使用

张开发
2026/4/10 14:07:01 15 分钟阅读

分享文章

【Shell专项】数组与函数的使用
第四章 数组和函数4.1 数组4.1.1 简介变量用一个固定的字符串代替一个不固定字符串。数组用一个固定的字符串代替多个不固定字符串。4.1.2 类型普通数组只能使用整数作为数组索引关联数组可以使用字符串作为数组索引变量切片有个索引的概念。一个索引整数对应一个字符。普通数组中的索引对应一个字符串。关联数组数组中的索引可以使用字符串。4.2 普通数组4.2.1 定义数组# array2(tom jack alice)# array3(cat /etc/passwd) 希望是将该文件中的每一个行作为一个元数赋值给数组array3# array4(ls /var/ftp/Shell/for*)# array5(tom jack alice bash shell)# colors($red $blue $green $recolor)# array6(1 2 3 4 5 6 7 linux shell [20]saltstack)方法2 一次赋一个值 数组名[下标]变量值# array1[0]pear# array1[1]apple# array1[2]orange# array1[3]peach查看数组[rootlocalhost ~]# declare -a | grep array1declare-aarray1([0]pear [1]apache [2]orange [3]peach)查看数组[rootlocalhost ~]# echo ${array1[]}pear apache orange peach4.2.2 访问数组元素访问数组元数# echo ${array1[0]} 访问数组中的第一个元数# echo ${array1[]} 访问数组中所有元数 等同于 echo ${array1[*]}# echo ${#array1[]} 统计数组元素的个数# echo ${!array2[]} 获取数组元素的索引# echo ${array1[]:1} 从数组下标1开始# echo ${array1[]:1:2} 从数组下标1开始访问两个元素4.3 关联数组4.3.1 定义关联数组方法一 一次赋一个值 数组名[索引]变量值[rootshell-test ~]# declare -A aaa[rootshell-test ~]# aaa[1]123[rootshell-test ~]# aaa[2]456[rootshell-test ~]# aaa[3]789[rootshell-test ~]#[rootshell-test ~]# echo 你是个${aaa[1]}你是个123[rootshell-test ~]# echo 你是个${aaa[2]}你是个456[rootshell-test ~]# echo 你是个${aaa[3]}你是个789 查看[rootshell-test ~]# echo ${#aaa[]}4[rootshell-test ~]# echo ${#aaa[*]}44.3.2 查看数组declare-A[rootshell-test ~]# declare -A | grep aaadeclare-Aaaa([3]789[2]456[1]123[0]222222222)4.4 函数4.4.1 定义函数# 方法1函数名(){函数要实现的功能代码}# 方法2function函数名{函数要实现的功能代码}4.4.2 调用函数函数名 函数名 参数1 参数2 xxx4.4.3 示例1-工具箱#!/bin/bashshow_menu(){catEOF BOX工具箱 1.查看磁盘容量信息 2.查看内存容量信息 3.查看CPU负载信息 4.查看帮助 5.退出BOX ..... EOF}while:doshow_menuread-pInput Your Number: menu_numcase$menu_numin1)echo*** Disk_Info ***df-hT;;2)echo*** Mem_Info ***free-m;;3)echo*** CPU_Info ***uptime;;4)show_menu;;5)exitesacdone4.4.4 示例2-阶乘的实现传参#!/bin/bashfff(){f1for((i1;i5;i))dof$[$f*$i]doneecho$1的阶乘是$f}fff1fff2fff3fff4fff5fff6# 1的阶乘是120# 2的阶乘是120# 3的阶乘是120# 4的阶乘是120# 5的阶乘是120# 6的阶乘是120# 由脚本外部外部传递参数[rootshell-test ~]# vim f.sh#!/bin/bashfff(){f1for((i1;i5;i))dof$[$f*$i]doneecho$1的阶乘是$f}fff$1fff$2fff$3[rootshell-test ~]# ./f.sh 3 4 53的阶乘是1204的阶乘是1205的阶乘是1204.4.5 示例3-阶乘的实现数组传参#!/bin/bashnum(12345)array(){x1foriin$*dox$[$x$i]echo$xdone}array${num[*]}[rootshell-test ~]# ./sz.sh24711164.5 影响Shell程序的内置命令 是一个命令而不是布尔值什么也不做但总是返回退出码0表示成功exit退出当前 Shell 脚本exit[n]true是一个命令而不是布尔值什么也不做但总是返回退出码0表示成功break跳出当前循环for、while、untilbreak[n]continue跳过本次循环的剩余部分进入下一次迭代shift4.5.1exit[rootshell-test ~]# vim a1.sh#!/bin/bashecho第 1 行exit0echo第 2 行这行不会执行[rootshell-test ~]# ./a1.sh第1行4.5.2true[rootshell-test ~]# vim a1.sh#!/bin/bashiftruethenecho条件成立因为 true 永远返回 0fi[rootshell-test ~]# ./a1.sh条件成立因为true永远返回04.5.3:[rootshell-test ~]# vim a1.sh#!/bin/bashif:thenecho条件成立因为 true 永远返回 0fi[rootshell-test ~]# ./a1.sh条件成立因为true永远返回04.5.4break[rootshell-test ~]# vim a1.sh#!/bin/bashforiin12345doif[$i-eq3]thenbreakfiecho数字:$idoneecho循环结束了[rootshell-test ~]# ./a1.sh数字:1数字:2循环结束了4.5.5shift[rootshell-test ~]# vim t1.sh#!/bin/bashecho参数个数:$#echo第一个参数:$1shiftechoshift 后第一个参数:$1[rootshell-test ~]# chmod x t1.sh[rootshell-test ~]# ./t1.sh 1 2 3参数个数:3第一个参数:1shift后第一个参数:2

更多文章