当前位置:网站首页>Shell脚本-位置参数(命令行参数)
Shell脚本-位置参数(命令行参数)
2022-07-01 08:36:00 【小蜗牛的路】
运行 Shell 脚本文件时我们可以给它传递一些参数,这些参数在脚本文件内部可以使用$n的形式来接收,例如,$1 表示第一个参数,$2 表示第二个参数,依次类推。
给脚本文件传递位置参数
脚本如下:
#!/bin/bash
echo "name: $1"
echo "age: $2"
执行命令:sh test.sh jack 18,其中jack是第一个位置参数,18是第二个位置参数,两者之间以空格分隔。输出如下:
name: jack
age: 18
给函数传递位置参数
脚本如下:
#!/bin/bash
#定义函数
function func(){
echo "name: $1"
echo "age: $2"
}
#调用函数
func jack 20
输出:
name: jack
age: 20
注意事项
如果参数个数太多,达到或者超过了 10 个,那么就得用${n}的形式来接收了,例如${10}、${23}。{ }的作用是为了帮助解释器识别参数的边界,这跟使用变量时加{ }是一样的效果。
边栏推荐
- The meaning of yolov5 training visualization index
- What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR
- 电视机尺寸与观看距离
- FreeRTOS学习简易笔记
- Pipeline detection of UAV Based on gazebo
- 中小企业固定资产管理办法哪种好?
- 明明设计的是高带宽,差点加工成开路?
- Guidelines and principles of did
- C语言指针的进阶(上篇)
- 目标检测的yolov3、4、5、6总结
猜你喜欢

3、Modbus通讯协议详解

SPL-介绍(一)

Guidelines and principles of did
![[detailed explanation of Huawei machine test] judgment string subsequence [2022 Q1 Q2 | 200 points]](/img/0f/972cde8c749e7b53159c9d9975c9f5.png)
[detailed explanation of Huawei machine test] judgment string subsequence [2022 Q1 Q2 | 200 points]

【C】 Summary of wrong questions in winter vacation

基于Gazebo的无人机管道检测

Only in China! Alicloud container service enters the Forrester leader quadrant

截图小妙招

Agrometeorological environment monitoring system

View drawing process analysis
随机推荐
Share 7 books I read in the first half of 2022
《微机原理》-绪论
又到年中,固定资产管理该何去何从?
Internet of things technology is widely used to promote intelligent water automation management
任务、线程、进程 区别
The data analyst will be ruined without project experience. These 8 project resources will not be taken away
基础:2.图像的本质
基于Gazebo的无人机管道检测
MATLAB【函数和图像】
Redis publish subscription
[MFC development (17)] advanced list control list control
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
2022 mechanical fitter (primary) examination summary and mechanical fitter (primary) reexamination examination
嵌入式工程师常见面试题2-MCU_STM32
用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
《微机原理》——微处理器内部及外部结构
Foundation: 3 Opencv getting started images and videos
SPL installation and basic use (II)
基础:3.opencv快速入门图像和视频
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?