当前位置:网站首页>Shell 函数
Shell 函数
2022-08-04 03:09:00 【Drw_Dcm】
目录
一、函数的定义
1.
function 函数名{
command
} 规范用法
2.
函数名() { 最常用,最简洁
comnand
}
函数定义完之后并不会自动执行,需要调用才行,好处在于可以写一段功能代码作为函数,有需要就直接调用
定义的时候出现语法错误也没关系,不调用就不会报错
函数返回值
return表示退出函数并返回一个退出值,脚本中可以用$?变量显示该值
使用原则
1.函数一结束就取返回值,因为$?变量只返回执行的最后一条命令的退出状态码
2.退出状态码必须是0~255, 超出时值将为取余256
二、函数的调用
直接在脚本里定义函数的代码块后写函数名即可完成调用
#!/bin/bash
function fun1 { 定义了一个函数叫做fun1
echo "this is a function!" 函数体的功能是打印"this is a function!
}
fun1 直接写函数名就会运行函数体内的代码
注意:1.函数名必须是唯一,如果先定义了一个,再用同样的名称定义,第二个会覆盖第一个的功能,出现意外的结果,所以这里一定要注意不要重名!
2.调用函数之前必须先进行定义!

边栏推荐
- sql注入一般流程(附例题)
- [Original] Start the XPS/OXPS reader that comes with Windows 10
- [Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
- Polygon zkEVM network node
- 2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
- 小程序+新零售,玩转行业新玩法!
- 十一种概率分布
- 4-way two-way HDMI integrated business high-definition video optical transceiver 8-way HDMI high-definition video optical transceiver
- Exclude_reserved_words 排除关键字
- FPGA parsing B code----serial 3
猜你喜欢
随机推荐
db2中kettle报错 Field [XXX] is required and couldn‘t be found 解决方法
FFmpeg —— 通过修改yuv,将视频转为黑白并输出(附源码)
DIY电工维修如何拆卸和安装开关面板插座
【源码】使用深度学习训练一个游戏
FPGA parsing B code----serial 3
仿牛客论坛项目梳理
JVM内存和垃圾回收-07.堆
STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目
MCU C language -> usage, and meaning
马尔可夫链
Zabbix set up email alert + enterprise WeChat alert
Good bosses, please ask the flink CDC oracle to Doris, found that the CPU is unusual, a run down
复制带随机指针的链表
STM8S-----选项字节
多线程间的通信方式你知道几种?
系统太多,多账号互通如何实现?
STM8S105k4t6c---------------Light up LED
There are n steps in total, and you can go up to 1 or 2 steps each time. How many ways are there?
Why use Selenium for automated testing
[Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this







