`
- 浏览:
784993 次
- 性别:
- 来自:
上海
-
给shell脚本传参数Passing arguments to a shell script
Simple explanation
$0 is the name of the command
$1 first parameter
$2 second parameter
$3 third parameter etc. etc
$# total number of parameters
$@ all the parameters will be listed
Passing arguments to a shell script
http://osr600doc.sco.com/en/SHL_automate/_Passing_to_shell_script.html
Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. In addition, any arguments you type after the script name on the shell command line are passed to the script as a series of variables.
The following parameters are recognized:
$*
Returns a single string (``$1, $2 ... $n'') comprising all of the positional parameters separated by the internal field separator character (defined by the IFS environment variable).
$@
Returns a sequence of strings (``$1'', ``$2'', ... ``$n'') wherein each positional parameter remains separate from the others.
$1, $2 ... $n
Refers to a numbered argument to the script, where n is the position of the argument on the command line. In the Korn shell you can refer directly to arguments where n is greater than 9 using braces. For example, to refer to the 57th positional parameter, use the notation ${57}. In the other shells, to refer to parameters with numbers greater than 9, use the shift command; this shifts the parameter list to the left. $1 is lost, while $2 becomes $1, $3 becomes $2, and so on. The inaccessible tenth parameter becomes $9 and can then be referred to.
$0
Refers to the name of the script itself.
$#
Refers to the number of arguments specified on a command line.
For example, create the following shell script called mytest:
echo There are $# arguments to $0: $*
echo first argument: $1
echo second argument: $2
echo third argument: $3
echo here they are again: $@
When the file is executed, you will see something like the following:
$ mytest foo bar quux
There are 3 arguments to mytest: foo bar quux
first argument: foo
second argument: bar
third argument: quux
here they are again: foo bar quux
$# is expanded to the number of arguments to the script, while $* and $@ contain the entire argument list. Individual parameters are accessed via $0, which contains the name of the script, and variables $1 to $3 which contain the arguments to the script (from left to right along the command line).
Although the output from $@ and $* appears to be the same, it may be handled differently, as $@ lists the positional parameters separately rather than concatenating them into a single string. Add the following to the end of mytest:
function how_many {
print "$# arguments were supplied."
}
how_many "$*"
how_many "$@"
The following appears when you run mytest:
$ mytest foo bar quux
There are 3 arguments to mytest: foo bar quux
first argument: foo
second argument: bar
third argument: quux
here they are again: foo bar quux
1 arguments were supplied.
3 arguments were supplied.
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
深入讲解了python函数传参的原理,值得一看
Shell Programming in Unix, Linux and OS X is a thoroughly updated revision of Kochan and Wood’s classic Unix Shell Programming tutorial. Following the methodology of the original text, the book ...
命令和参数之间必需用空格隔开,参数和参数之间也必需用空格隔开。 一行不能超过256个字符;大小写有区分。 二、特殊字符含义 文件名以“.”开头的都是隐藏文件/目录,只需在文件/目录名前加“.”就可隐藏它。...
that initial choice is close to a good solution. We devised a method called “affinity propagation,” which takes as input measures of similarity between pairs of data points. Real-valued messages are...
arm EABI/EABIHF port of pascalscript. Changes: * Implement aligned passing of 64-bit parameters for EABI/... * Rewrite return value handling to avoid passing pointers to unsafe stack space. * Use
Message Passing Interface (MPI),Message Passing Interface (MPI)
Adapting a File-like Object to a True File Object Recipe 2.16. Walking Directory Trees Recipe 2.17. Swapping One File Extension for Another Throughout a Directory Tree Recipe 2.18. Finding a ...
用于LDPC的译码程序,使用message passing 算法,可以选择迭代次数, 调整信噪比,用LLR计算
MPI: A Message-Passing Interface Standard第三版
You can customize this behavior by passing parameters to the Connection Layer API (all keyword arguments to the Elasticsearch class will be passed through). If what you want to accomplish is not ...
Bypassing non-executable-stack during exploitation using return-to-libc.pdf
Interest-aware Message-Passing GCN for Recommendat.md
In this report, we adopt it to investigate the kinetic behavior of cold atoms passing through a far red-detuned Gaussian beam. We demonstrate that the aberration has significant influence on the ...
mpi 2.2 standard的文档。mpi的介绍文件
Null passed to a callee that requires a non-null argument 此警告就是某属性说好的不能为空,你又在某地方写了XX = nil 所以冲突了 这个警告比较新,是xcode6.3开始 为了让OC也能有swift的?和!的功能,你在声明...
The main reason for making a virtual function inline is to place its definition in the class, either for convenience or to document its behavior, e.g., for accessors and mutators. The -inl.h Files...
What’s in This Book? This book contains a mix of theoretical and practical chapters. For the practical chapters, I’ve developed and made available a networking library...the script you want to execute.
2018年新书,介绍了消息传递接口(Message Passing Interface,MPI)基础知识及最新版的开发和优化技巧。
2.6. Passing Command Line Arguments to a Module 2.7. Modules Spanning Multiple Files 3. Preliminaries 3.1. Modules vs Programs 4. Character Device Files 4.1. Character Device Drivers 5. The /proc File...