当前位置:网站首页>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}。{ }的作用是为了帮助解释器识别参数的边界,这跟使用变量时加{ }是一样的效果。
边栏推荐
- [no title] free test questions for constructor municipal direction general foundation (constructor) and theoretical test for constructor municipal direction general foundation (constructor) in 2022
- C语言指针的进阶(下)
- win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
- Intelligent water and fertilizer integrated control system
- Memory size end
- 内存大小端
- 电视机尺寸与观看距离
- Share 7 books I read in the first half of 2022
- 基础:2.图像的本质
- Configuration and startup of Chang'an chain synchronization node
猜你喜欢

Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates

SPL-介绍(一)

AES简单介绍

Field agricultural irrigation system

Introduction to R language
![Matlab [functions and images]](/img/8a/d2f68b5a7ed396ad20234c0aa24953.jpg)
Matlab [functions and images]

基于Gazebo的无人机管道检测

"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis

View drawing process analysis
![Matlab [function derivation]](/img/ba/9fb9da8a458d0c74b29b21a17328fc.png)
Matlab [function derivation]
随机推荐
3. Detailed explanation of Modbus communication protocol
《单片机原理及应用》—定时器、串行通信和中断系统
你了解数据是如何存储的吗?(C整型和浮点型两类)
MATLAB【函数求导】
Agrometeorological environment monitoring system
Pipeline detection of UAV Based on gazebo
Intelligent water supply system solution
[Yu Yue education] Shandong Vocational College talking about railway reference materials
Advanced API
SPL-安装与基本使用(二)
Do you know how data is stored? (C integer and floating point)
为什么LTD独立站就是Web3.0网站!
Only in China! Alicloud container service enters the Forrester leader quadrant
The use of word in graduation thesis
电脑小技巧
MAVROS发送自定义话题消息给PX4
Serial port to WiFi module communication
MySQL8.0学习记录17 -Create Table
性能提升2-3倍!百度智能云第二代昆仑芯服务器上线
2022.2.15