Passing arguments to a shell script

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).
  • [email protected]
  • 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: [email protected]

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 [email protected] 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 [email protected] and $* appears to be the same, it may be handled differently, as [email protected] 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 "[email protected]"

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.
时间: 2024-10-18 02:40:56

Passing arguments to a shell script的相关文章

Unix/Linux shell脚本编程学习--Shell Script II

Shell Script II 10.Shell echo命令 echo "OK!\n”   #显示换行 echo "It is a test" echo无拼接字符时后一般可以不使用”引号”,从上面可看出,双引号可有可无,单引号主要用在原样输出中. 显示结果重定向保存至文件: vim myfile 创建文件 echo "It is a test" > myfile cat myfile 查看文件内容 若需要原样输出字符串(不进行转义),请使用单引号.

Why to Write Shell Script

Why to Write Shell Script ? Shell script can take input from user, file and output them on screen. Useful to create our own commands. Save lots of time. To automate some task of day today life. System Administration part can be also automated.

学习shell script

如果你的想要管理好你的主机,那么就要好好学习自动管理系统的有效工具--hell script!基本上,shell script有点像早期的批处理文件,即将很多命令整合起来一次执行,但是shell script拥有更强大的功能,它可以进行类似程序的编写,并且不需要经过编译就能够执行,非常方便我们对系统进行管理. 今天我们主要通过对一些shell script的习题来加深对脚本知识的学习. 习题1:对成绩进行判断,要求输入一个正整数,以60分,85分为界输出不同的评语. #!/bin/bash #

shell编程 Shell script 的默认变量($0, $1...)

Shell script 的默认变量($0, $1...) 我们知道指令可以带有选项与参数,例如 ls -la 可以察看包含隐藏文件的所有属性与权限.那么 shell script 能不能在脚本文件名后面带有参数呢?很有趣喔!举例来说,如果你想要重新启动系统登录文件的功能,可以这样做: [[email protected] ~]# file /etc/init.d/syslog /etc/init.d/syslog: Bourne-Again shell script text executab

Scheduled Job Running Shell Script Fails With ORA-27369

Scheduled Job Running Shell Script FailsWith ORA-27369 Applies to: Oracle Server - Enterprise Edition -Version: 10.1.0.2 to 10.2.0.4 Oracle Server - Standard Edition -Version: 10.1.0.2 to 10.2.0.4 including Version:11.2.0.4 Sun Solaris SPARC (64-bit)

執行shell script與subshell

兩種方法 喚起新shell再執行shell scripts 在目前shell執行shell scripts 喚起另一個shell來執行的scripts在scripts檔頭最前面前要加 #! /bin/sh 第一種方法是在shell script 文字檔前指出shell scripts解讀的程式在那(也就是 我們的shell)然後把文字檔的執行權限打開,照一般執行可執行檔方式執行或者叫 一個shell來解釋文字檔test.sh. $ test.sh $ /bin/sh test.sh $ ( .

linux shell script: Basic concept01 - String

basic concept01: String本文所有的测试例如无特殊说明,均based on fish shell 就从字符串说起吧,啥是字符串就不用解释了,我们来看几个简单的例子 ?> ~ set param abc ?> ~ echo "string with blank and $param surrounded with double quotation marks" string with blank and abc surrounded with double

SSH防止暴力破解 shell script

这是我的第一个Shell Script,写的乱乱糟糟,试验了一下,还是可用的,目前已经在我自己的WEB服务器上跑起来了!!~~ #!/bin/bash #这个shell script 用来防止SSH暴力破解 #Auther:Aaron Guo #Date:Jan 8 2016 #Version:1.2 # 指定该SHELL的日志文件 logfile="/var/log/blocked_ip" # 获取现在时间,用来grep /var/log/secure. (格式:mm dd HH)

跟鸟哥学Linux之——shell script

从程序员的角度来看,Shell本身是一种用C语言编写的程序,从用户的角度来看,Shell是用户与Linux操作系统沟通的桥梁.用户既可以输入命令执行,又可以利用 Shell脚本编程,完成更加复杂的操作.在系统管理等领域,Shell编程起着不可忽视的作用.前期我对shell编程做了一个初步的认识,现总结如下: 思维导图: 知识分为了两部分: 基本知识: 在基本知识中我将其分为了:执行方法.运算式.变量和判断式四部分.1.执行方法就是在Linux中shell文件执行的命令,用的多了自然会没有问题.2