当前位置:网站首页>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}
。{ }的作用是为了帮助解释器识别参数的边界,这跟使用变量时加{ }是一样的效果。
边栏推荐
- Nacos - 配置管理
- Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process
- MATLAB【函数和图像】
- Foundation: 3 Opencv getting started images and videos
- IT 技术电子书 收藏
- AVL树的理解和实现
- [no title] free test questions for constructor municipal direction general foundation (constructor) and theoretical test for constructor municipal direction general foundation (constructor) in 2022
- R语言入门
- Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
- Redis源码学习(29),压缩列表学习,ziplist.c(二)
猜你喜欢
Qt的模型与视图
TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
《MATLAB 神经网络43个案例分析》:第30章 基于随机森林思想的组合分类器设计——乳腺癌诊断
factory type_id::create过程解析
TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘
What is 1cr0.5mo (H) material? 1cr0.5mo (H) tensile yield strength
Field agricultural irrigation system
VSYNC+三重缓存机制+Choreographer
Audio-AudioRecord create(一)
Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
随机推荐
Principle and application of single chip microcomputer - principle of parallel IO port
C语言学生信息管理系统
19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
[detailed explanation of Huawei machine test] judgment string subsequence [2022 Q1 Q2 | 200 points]
【C】 Summary of wrong questions in winter vacation
[MFC development (17)] advanced list control list control
个人装修笔记
Maneuvering target tracking -- current statistical model (CS model) extended Kalman filter / unscented Kalman filter matlab implementation
In depth learning training sample amplification and tag name modification
NIO-零拷贝
嵌入式工程师面试题3-硬件
Leetcode t40: combined sum II
The use of word in graduation thesis
[no title] free test questions for constructor municipal direction general foundation (constructor) and theoretical test for constructor municipal direction general foundation (constructor) in 2022
SPL-介绍(一)
《单片机原理及应用》-片外拓展
《微机原理》-绪论
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
毕业论文中word的使用1-代码域标公式
Configuration and startup of Chang'an chain synchronization node