当前位置:网站首页>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.调用函数之前必须先进行定义!
边栏推荐
- Simple record of Flink principle flow chart
- Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
- 高效IO模型
- TOML配置文件格式,YAML最有力的竞争者
- 验证码业务逻辑漏洞
- STM8S项目创建(STVD创建)---使用 COSMIC 创建 C 语言项目
- STM8S-----option byte
- 2 Gigabit Optical + 6 Gigabit Electric Rail Type Managed Industrial Ethernet Switch Supports X-Ring Redundant Ring One-key Ring Switch
- keytool命令
- 2022.8.3-----leetcode.899
猜你喜欢
高效IO模型
Simple record of Flink principle flow chart
STM8S105K4T6------Serial port sending and receiving
4路双向HDMI综合业务高清视频光端机8路HDMI高清视频光端机
十一种概率分布
用户与用户互发红包/支付宝C2C/B2C现金红包php源码示例/H5方式/兼容苹果/安卓
目标检测-中篇
Homemade bluetooth mobile app to control stm8/stm32/C51 onboard LED
sql注入一般流程(附例题)
Countdown to 2 days, the "New Infrastructure of Cultural Digital Strategy and Ecological Construction of Cultural Art Chain" will kick off soon
随机推荐
FPGA解析B码----连载3
What is the source of flinkcdc consuming mysql binlog data without sqltype=delete
怎样提高网络数据安全性
WPE详细教程
基地址:环境变量
TOML配置文件格式,YAML最有力的竞争者
十一种概率分布
董明珠直播时冷脸离场,员工频犯低级错误,自家产品没人能弄明白
In a more general sense, calculating the displacement distance and assumptions
Dong mingzhu live cold face away, when employees frequency low-level mistakes, no one can understand their products
sqoop ETL工具
2022年最新海南建筑八大员(材料员)模拟考试试题及答案
FPGA parsing B code----serial 3
如何读取 resources 目录下的文件路径?
[Playwright Test Tutorial] 5 minutes to get started
Mockito unit testing
6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
Why use Selenium for automated testing
Mini program + new retail, play the new way of playing in the industry!
FFmpeg —— 通过修改yuv,将视频转为黑白并输出(附源码)