`
darrenzhu
  • 浏览: 797351 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

给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.
分享到:
评论

相关推荐

    Passing arguments to Python functions

    如果尝试直接传递位置参数给一个只接受关键字参数的函数,将会导致错误,因为 **kwargs 需要键值对形式的输入。 **错误示例:** ```python twostar(1, 2, 3) # TypeError: twostar() takes 0 positional arguments...

    Shell.Programming.in.Unix.Linux.and.OS.X.4th.Ed

    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 ...

    2009 达内Unix学习笔记

    命令和参数之间必需用空格隔开,参数和参数之间也必需用空格隔开。 一行不能超过256个字符;大小写有区分。 二、特殊字符含义 文件名以“.”开头的都是隐藏文件/目录,只需在文件/目录名前加“.”就可隐藏它。...

    Bypassing non-executable-stack during exploitation using return-to-libc.pdf

    在IT安全领域,尤其是针对软件漏洞的利用与防御机制中,“绕过非执行堆栈进行利用,使用返回至libc技术”(Bypassing non-executable-stack during exploitation using return-to-libc)是一个高度专业且复杂的主题...

    Clustering by Passing Messages Between Data Points

    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...

    pascalscript.zip

    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),Message Passing Interface (MPI)

    Python Cookbook, 2nd Edition

    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 译码算法

    用于LDPC的译码程序,使用message passing 算法,可以选择迭代次数, 调整信噪比,用LLR计算

    elasticsearch-py-6.3.1.pdf

    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 ...

    MPI: A Message-Passing Interface Standard - Version 2.2 - Sep.4. 2009

    消息传递接口(Message Passing Interface,简称MPI)是并行计算领域中的一个重要标准,它为编写可移植的并行程序提供了一种方法。MPI 2.2版本是基于之前版本进行改进的结果,主要包含了对原有功能的增强和澄清,并...

    passing-l-array-to-a-function.rar_多维传递函数_数组、函数

    本主题将深入探讨如何在编程环境中,特别是在C++或类似的静态类型语言中,将多维数组作为参数传递给函数,以及这样做的实际应用和潜在注意事项。 1. **数组的基本概念** - 数组:数组是存储同一类型元素的固定大小...

    Interest-aware Message-Passing GCN for Recommendat.md

    Interest-aware Message-Passing GCN for Recommendat.md

    Cold atoms passing through a thin laser beam: a Fourier optics approach

    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 ...

    iOS-Null passed to a callee that requires a non-null argument警告

    Null passed to a callee that requires a non-null argument 此警告就是某属性说好的不能为空,你又在某地方写了XX = nil 所以冲突了 这个警告比较新,是xcode6.3开始 为了让OC也能有swift的?和!的功能,你在声明...

    1Z0-811 Exam Guide to Have a Cakewalk in Oracle Java SE Certific

    - **Method Invocation:** Understand method invocation and passing arguments. - **Overloading and Overriding:** Learn about method overloading and overriding. 3. **Decision Statements:** - **If-...

    函数参数的传递

    在C语言中,函数参数的传递主要有三种方式:值传递、地址传递和引用传递。这三种方式各自有不同的特性和应用场景,理解它们的工作原理对于编写高效和正确的程序至关重要。 1. 值传递(Value Passing) 值传递是最...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    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...

Global site tag (gtag.js) - Google Analytics