当前位置:网站首页>Shell pass parameters
Shell pass parameters
2022-06-24 08:36:00 【Chen Bucheng I】
We can execute Shell Script time , Pass parameters to script , The format of obtaining parameters in the script is :$n.n Represents a number ,1 For executing the first parameter of the script ,2 For executing the second parameter of the script , And so on ……
example
In the following example, we pass three parameters to the script , And output respectively , among $0 For the filename of the execution ( Include file path ):
#!/bin/bashecho "Shell Pass parameter instance !";echo " File name of execution :$0";echo " The first parameter is zero :$1";echo " The second parameter is :$2";echo " The third parameter is zero :$3";
Set executable permissions for scripts , And execute the script , The output is as follows :
$ chmod +x test.sh$ ./test.sh 123ShellPass parameter instance !File name of execution :./test.shThe first parameter is zero :1The second parameter is :2The third parameter is zero :3
in addition , There are also a few special characters for processing parameters :
Processing parameters | explain |
|---|---|
$# | The number of parameters passed to the script |
$* | Display all the parameters passed to the script in a single string . Such as ”$*” use 「”」 In a nutshell 、 With ”$1 $2 … $n” Output all parameters in the form of . |
$$ | The current process the script runs ID Number |
$! | Of the last process running in the background ID Number |
And $* identical , But use quotation marks , And return each parameter in quotation marks . Such as ”[email protected]” use 「”」 In a nutshell 、 With ”$1” “$2” … “$n” Output all parameters in the form of . | |
$- | Show Shell Current options used , And set Same command function . |
$? | Display the exit status of the last command .0 No mistakes , Any other value indicates an error . |
#!/bin/bashecho "Shell Pass parameter instance !";echo " The first parameter is zero :$1";echo " The number of parameters is :$#";echo " The passed parameter is displayed as a string :$*";
Execute the script , The output is as follows :
$ chmod +x test.sh$ ./test.sh 123ShellPass parameter instance !The first parameter is zero :1The number of parameters is :3The passed parameter is displayed as a string :123
$* And [email protected] difference :
The same thing : All references to all parameters .Difference : Only in double quotes . Suppose you write three parameters when the script runs1、2、3,, be" * "Equivalent to"1 2 3"( Passed a parameter ), and"@"Equivalent to"1""2""3"( Three parameters are passed ).#!/bin/bashecho "-- \$* demonstration ---"for i in"$*";doecho $idoneecho "-- \[email protected] demonstration ---"for i in"[email protected]";doecho $idone
边栏推荐
- Building a static website with eleventy
- Matlab求解线性方程组Ax=b
- 问题3 — messageBox弹框,修改默认背景色
- 小样本故障诊断 - 注意力机制代码 - BiGRU代码解析实现
- 软件过程与项目管理期末复习与重点
- Fund raising, trading and registration
- Common date formatter and QT method for obtaining current time
- Synthesize video through ffmpeg according to m3u8 file of video on the network
- ZUCC_编译语言原理与编译_实验05 正则表达式、有限自动机、词法分析
- Question bank and simulation examination for operation certificate of refrigeration and air conditioning equipment in 2022
猜你喜欢

Longhorn installation and use

The article takes you to understand the security of Windows operating system and protect your computer from infringement

【关于运维和网工的差别,一文说透】

PAT 1157:校庆

ZUCC_编译语言原理与编译_实验03 编译器入门

2022年流动式起重机司机特种作业证考试题库及在线模拟考试

Synthesize video through ffmpeg according to m3u8 file of video on the network

Pat 1157: school anniversary

Two methods of QT exporting PDF files

OpenCV to realize the basic transformation of image
随机推荐
2021-03-04 comp9021 class 6 notes
【毕业季】你好陌生人,这是一封粉色信笺
QT writing security video monitoring system 36 onvif continuous movement
MAYA重新拓布
leetcode 1268. Search Suggestions System(搜索推荐系统)
Building a static website with eleventy
Question 4 - datepicker date selector, disabling two date selectors (start and end dates)
Tool functions – get all files in the project folder
貸款五級分類
JUC个人简单笔记
日本大阪大学万伟伟研究员介绍基于WRS系统机器人的快速集成方法和应用
[xinliu-s6 new model +sa 3-star Xinghai] the new two-way server of the third generation chip was launched and the product was updated~
ZUCC_ Principles of compiling language and compilation_ Experiment 08 parsing LR parsing
ZUCC_编译语言原理与编译_实验04 语言与文法
Qt导出PDF文件的两种方法
487. 最大连续1的个数 II ●●
Win10 cloud, add Vietnamese
ZUCC_编译语言原理与编译_实验02 FSharp OCaml语言
数据库,查询本月借出书的数量,如果高于10本时,显示“本月借出书大于10本”,否则显示“本月借出书小于10本”
2022茶艺师(中级)上岗证题库及在线模拟考试