当前位置:网站首页>shell脚本------函数的格式,传参,变量,递归,数组
shell脚本------函数的格式,传参,变量,递归,数组
2022-08-09 11:42:00 【AKA|布鲁克林欧神仙】
目录
一,shell函数定义
- 将命令序列按格式写在一起,可方便重复使用命令序列
二,格式
1,格式一:
function 函数名 {
命令序列
}2,格式二:
函数名 () {
命令序列
}三,函数返回值
return表示退出函数并返回一个退出值,脚本中可以用$?变量显示该值。
使用原则:
- 函数一结束就取返回值,因为$?变量只返回执行的最后一条命令的退出状态码
- 退出状态码必须是0-255.超出时值将为除以256取余


四,函数传参


五,函数变量的作用范围
函数在shell脚本中仅在当前shell环境中有效
shell脚本中变量默认全局有效
将变量限定在函数内部使用local命令




六,递归


七,数组
一,数组的定义
数组是存放相同类型数据的集合,在内存中开辟了连续的空间,通常配合循环使用
二、数组的分类
普通数组:不需要声明直接定义,下标索引只能是整数
关联数组:需要用declare -A声明否则系统不识别,索引可以是字符串
三、数组的定义方式
第一种:直接把要加入数组的元素用小括号括起来,中间用空格分开 num=(11 22 33 44)
第二种:精确的给每一个下标索引定义一个值加入数组,索引数字可以不连续 num=([0]=55 [1]=66 [2]=77 [4]=88)
第三种:先把要加入数组的元素全部先赋值给一个变量,然后引用这个变量加入到数组 list=“11 12 13 14” num=($list)
第四种:可以把命令的结果用小括号括起来添加到数组,那么数组会以空格或者制表符区分每一个元素 num=(cat /etc/passwd)
[[email protected] ~]# echo ${#num[*]}
111
3.1、数组格式
如:arr=(11 22 33 44)
数组名称:arr
数组元素:11,12,12,14,数组元素默认是以空格分隔的
数组长度:4,有4个元素
数组下标:每个数组元素都有一个下标索引,一般从0开始,33是数组第三个元素,但是它的下标索引是23.3、元素删除
元素删除
[[email protected] ~]# unset fruit[0]
[[email protected] ~]#
[[email protected] ~]# declare -a | grep fruit
declare -a fruit='([1]="banana" [2]="orange")'3.4、数组遍历
#!/bin/bash
arr=(1 2 3 4 5 6)
for i in ${arr[*]} 或for i in ${arr[@]}
do
echo $i
done
3.5、冒泡排序
#!/bin/bash
read -p "请输入数组:" a
num=($a)
echo "你输入的数组为:${num[*]}"
for ((i=0;i<${#num[*]};i++))
do
for ((j=i+1;j<${#num[*]};j++))
do
if [ ${num[i]} -gt ${num[j]} ]
then
temp=${num[i]}
num[i]=${num[j]}
num[j]=$temp
fi
done
done
echo "排序后的数组为:${num[*]}"
边栏推荐
- Chinese valentine's day?Programmers don't exist
- F280049库函数API编程、直接寄存器控制编程和混合编程方法
- Django 无法链接mysql数据库
- _main C:/ti/ccs1011/ccs/tools/compiler/ti-cgt-c2000_20.2.1.LTS/lib/rts2800_fpu32.lib<ar在线升级跳转疑问
- win10 outlook邮件设置
- Summary of learning stages (knapsack problem)
- PAT1009
- Installation of gdb 10.2
- ACM01 Backpack problem
- Two ways to enter the Oracle database
猜你喜欢

富媒体在客服IM消息通信中的秒发实践

PTA 实验7-5 输出大写英文字母(10 分)

链表基本操作(详解)

fork creates multiple child processes

proto3-2语法

x86 Exception Handling and Interrupt Mechanism (3) Interrupt Handling Process
![[现代控制理论]5_系统的可控性_controllability](/img/a6/29c89163290eb03b4ce28274d036ca.png)
[现代控制理论]5_系统的可控性_controllability

Win10调整磁盘存储空间详解
![[Essence] Analysis of the special case of C language structure: structure pointer / basic data type pointer, pointing to other structures](/img/cc/9e5067c9cedaf1a1797c0509e67f88.png)
[Essence] Analysis of the special case of C language structure: structure pointer / basic data type pointer, pointing to other structures

排序--快排(图解)
随机推荐
Win10调整磁盘存储空间详解
bat文件(批处理文件)运行时一闪而过解决方法
Redis的下载安装
BISS绝对值编码器_TI方案_线路延迟补偿
ACM01 Backpack problem
抗积分饱和 PID代码实现,matlab仿真实现
学长告诉我,大厂MySQL都是通过SSH连接的
PAT 1015 进制转换
This application has no explicit mapping for /error, so you are seeing this as a fallback
PAT1011
Open3D point cloud average point spacing evaluation
LeetCode 1413.逐步求和得到正数的最小值
【概率论】正态分布的由来——从大一同学的视角出发
结构体变量的首地址获取注意事项
ZOJ 1729 & ZOJ 2006(最小表示法模板题)
二重指针-char **、int **的作用
Paper Sharing | ACL2022 | Argument Relation Extraction Based on Transfer Learning
Semaphore SIGCHLD use, how to make the parent that the child performs over, how to make the distinction between multiple child processes. The end
_main C:/ti/ccs1011/ccs/tools/compiler/ti-cgt-c2000_20.2.1.LTS/lib/rts2800_fpu32.lib<ar在线升级跳转疑问
VS Code有趣插件