文档章节

Passing arguments to a shell script

Zoker
 Zoker
发布于 2014/09/19 13:47
字数 450
阅读 296
收藏 1

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


本文转载自:http://osr600doc.sco.com/en/SHL_automate/_Passing_to_shell_script.html

Zoker

Zoker

粉丝 550
博文 31
码字总数 10404
作品 1
深圳
其他
私信 提问
linux shell program-argument,file exits-date,oracle shell

Bash warning - argument expected http://stackoverflow.com/questions/29178135/bash-warning-argument-expected Bash Shell: Check File Exists or Not http://www.cyberciti.biz/faq/uni......

Oscarfff
2016/10/13
6
0
PySpark-使用Python在Spark上编程

Python Programming Guide The Spark Python API (PySpark) exposes the Spark programming model to Python. To learn the basics of Spark, we recommend reading through theScala progra......

openthings
2016/03/09
1K
0
Flask扩展flask-script文档中文翻译

Flask扩展flask-script文档中文翻译 Flask-Script扩展提供向Flask插入外部脚本的功能。包括运行一个开发用的服务器,一个定制的Python shell,设置数据库的脚本,cronjobs,以及其他的运行在...

青崖白鹿
2013/09/03
0
1
AirFlow-Tutorial

AirFlow-Tutorial https://airflow.incubator.apache.org/tutorial.html This tutorial walks you through some of the fundamental Airflow concepts, objects, and their usage while writ......

openthings
2018/07/10
0
0
Sass 3.3.0 发布,CSS 预处理器

Sass 3.3.0 发布,于此同时还发布了 Sass 3.2.15 版本:当父选择器换行符后面跟着逗号时,支持 。 Sass 3.3.0 变得更加灵活,值得注意的更新内容如下: SassScript Maps Source Maps 关键字变...

oschina
2014/03/09
2.8K
4

没有更多内容

加载失败,请刷新页面

加载更多

Android双向绑定原理简述

Android双向绑定原理简述 双向绑定涉及两个部分,即将业务状态的变化传递给UI,以及将用户输入信息传递给业务模型。 首先我们来看业务状态是如何传递给UI的。开启dataBinding后,编译器为布局...

tommwq
今天
4
0
Spring系列教程八: Spring实现事务的两种方式

一、 Spring事务概念: 事务是一系列的动作,它们综合在一起才是一个完整的工作单元,这些动作必须全部完成,如果有一个失败的话,那么事务就会回滚到最开始的状态,仿佛什么都没发生过一样。...

我叫小糖主
今天
8
0
CentOS 的基本使用

1. 使用 sudo 命令, 可以以 root 身份执行命令, 必须要在 /etc/sudoers 中定义普通用户 2. 设置 阿里云 yum 镜像, 参考 https://opsx.alibaba.com/mirror # 备份mv /etc/yum.repos.d/CentO...

北漂的我
昨天
4
0
Proxmox VE技巧 移除PVE “没有有效订阅” 的弹窗提示

登陆的时候提示没有有效的订阅You do not have a valid subscription for this server. Please visit www.proxmox.com to get a list of available options. 用的是免费版的,所以每次都提示......

以谁为师
昨天
5
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部