1、读取参数:位置参数变量是标准的数字: $0是程序名, $1是第一个参数, $2是第二个参数...

   #!/bin/bash
# using one command line parameter factorial=
for (( number = ; number <= $; number++ ))
do
factorial=$[ $factorial * $number ]
done
echo The factorial of $ is $factorial

执行:

# ./test1.sh
The factorial of is

2、输入多个命令行选项,则在命令行上每个参数都必须用空格分开:

   #!/bin/bash
# testing two command line parameters total=$[ $ * $ ]
echo The first parameter is $
echo The second parameter is $
echo The total value is $total

执行:

# ./test2.sh
The first parameter is
The second parameter is
The total value is

3、如果脚本需要多个9个命令行参数,在变量数字周围加花括号:

   #!/bin/bash
# handling lots of parameters total=$[ ${} * ${} ]
echo The tenth parameter is ${}.
echo The eleventh parameter is ${}.
echo The total is $total

执行:

# ./test4.sh
The tenth parameter is .
The eleventh parameter is .
The total is

4、测试参数

   #!/bin/bash
# testing parameters before use if [ -n "$1" ] # -n 参数测试 $1(第一个参数)是否为null
then
echo Hello $, glad to meet you.
else
echo "Sorry, you did not identify yourself"
fi

执行:

# ./test7.sh
Sorry, you did not identify yourself ./test7.sh frank
Hello frank, glad to meet you.

5、特殊参数变量(参数计数 $#)

   #!/bin/bash
# getting the number of parameters
echo There were $# parameters supplied.

执行:

# ./test8.sh
There were parameters supplied. ./test8.sh 1 2 3
There were 3 parameters supplied.

6、使用参数前测试参数的总数

   #!/bin/bash
# testing parameters if [ $# -ne ]
then
echo Usage: test a b
else
total=$[ $ + $ ]
echo The total is $total
fi

执行:

# ./test9.sh
The total is 3 # ./test9.sh
Usage: test a b

7、不需要知道有多少个参数个数,抓取最后一个参数 ${!#}

   #!/bin/bash
params=$#
echo The number of parameter is $params
echo The last parameter is ${!#}

执行:

# sh test10.sh
The number of parameter is #参数的总个数
The last parameter is 4 #最后一个参数的值

8、$*  $@变量提供了对所有参数的快速访问, $*变量会将命令行上提供的所有参数当作单个单词保存,$@变量会将命令行上提供的所有参数当做同一个字符串中多个独立的词。

   #!/bin/bash
# testing $* and $@ echo "Using the \$* method: $*"
echo "Using the \$@ method: $@"

执行:

# ./test11 rich katie jessica
Using the $* method: rich katie jessica
Using the $@ method: rich katie jessica

差异:

   #!/bin/bash
# testing $* and $@
count=1 #赋值的时候注意不要有空格
for param in "$*" #将所有参数变为一个参数
do
echo "\$* parameter #$count = $param"
count=$[ $count + ]
done count=
for param in "$@" #各个参数独立
do
echo "\$@ parameter #$count = $param"
count=$[ $count + ]
done
~

执行:

# ./test12.sh rich barbara katie jessica
$* parameter # = rich barbara katie jessica
$@ parameter # = rich
$@ parameter # = barbara
$@ parameter # = katie
$@ parameter # = jessica

可见, $*会将所有的参数当成单个单数,而$@变量会单独处理每个参数。

9、shift的用法,遍历命令行参数

   #!/bin/bash
#demonstrating the shift command count=
while [ -n "$1" ] #注意要加冒号
do
echo "parameter #$count = $1"
count=$[ $count + ]
shift #参数变量逐次减一
done
~

执行:

# ./test13.sh a b c d e
parameter # = a
parameter # = b
parameter # = c
parameter # = d
parameter # = e

10、shift命令提供一个参数执行多位移动

   #!/bin/bash
# demonstrating a multi-position shift echo "The original parameters: $*"
shift
echo "Here's the new first parameter: $1"

执行:

# ./test14.sh
The original parameters:
Here's the new first parameter: 3

每天写点shell--命令行参数的更多相关文章

  1. shell 命令行参数(getopt和getopts)

    getopt 命令 使用getopt命令,可以解析任何命令行选项和参数,但是用法比较复杂.getopt的命令用法如下: $ getopt --help 用法: getopt optstring par ...

  2. shell 命令行参数(基本)

    命令行参数 $0 表示程序名.$1 至 \$9则是位置参数.$# 表示参数的个数.$* 将所有参数当做一个整体来引用$@ 把每个参数作为一个字符串返回,可以使用for循环来遍历$? 最近一个执行的命令 ...

  3. 写个C#命令行参数解析的小工具

    最近测试工作做的比较多因此时常要创建一些控制台类型的应用程序.因为程序有不同的参数开关,需要在程序启动的时候通过命令行来给程序传递各种开关和参数.直接操作args有些不方便,所以就写了个解析参数的小工 ...

  4. python 命令行参数学习(二)

    照着例子看看打打,码了就会.写了个命令行参数调用进行运算的脚本. 参考文章链接:http://www.jianshu.com/p/a50aead61319 #-*-coding:utf-8-*- __ ...

  5. kettle文件自动化部署(shell脚本执行):命令行参数传入

    shell脚本中调用kitchen 和 pan去执行,job和transformation文件.分 windows和 dos系统两种. 举个简单的小例子 shell脚本: export JAVA_HO ...

  6. Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本

    Shell 命令行,写一个自动整理 ~/Downloads/ 文件夹下文件的脚本 在 mac 或者 linux 系统中,我们的浏览器或者其他下载软件下载的文件全部都下载再 ~/Downloads/ 文 ...

  7. Shell 参数(2) --解析命令行参数工具:getopts/getopt

    getopt 与 getopts 都是 Bash 中用来获取与分析命令行参数的工具,常用在 Shell 脚本中被用来分析脚本参数. 两者的比较 (1)getopts 是 Shell 内建命令,geto ...

  8. Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数

    特殊变量列表 变量 含义 $0 当前脚本的文件名 $n 传递给脚本或函数的参数.n 是一个数字,表示第几个参数.例如,第一个参数是$1,第二个参数是$2. $# 传递给脚本或函数的参数个数. $* 传 ...

  9. Linux Shell 05 位置变量(命令行参数)

    在Linux shell 脚本中可能会用到一些命令行参数,常见如下: $0:脚本名称 $#:执行脚本时传入的参数个数,不包括脚本名称 $@:所有参数 $*:所有参数 $1...$9:第1个参数.... ...

  10. 【Shell脚本学习8】Shell特殊变量:Shell $0, $#, $*, $@, $?, $$和命令行参数

    前面已经讲到,变量名只能包含数字.字母和下划线,因为某些包含其他字符的变量有特殊含义,这样的变量被称为特殊变量. 例如,$ 表示当前Shell进程的ID,即pid,看下面的代码: $echo $$ 运 ...

随机推荐

  1. 记一次与a标签相遇的小事

    最近做的一个项目,按钮使用的是a标签做的,样子还不错.不过正是这个a标签把我坑死了,有一个场景是点击a标签去调后台服务,为了防止用户频繁点击按钮提交,在去请求后台服务的时候肯定要先把按钮的事件给禁止掉 ...

  2. TCP协议学习

    一.TCP参考模型  VS OSI参考模型 二.TCP/IP分层模型的四个协议层分别完成以下的功能 第一层 网络接口层 网络接口层包括用于协作IP数据在已有网络介质上传输的协议.实际上TCP/IP标准 ...

  3. 【原】移动web点5像素的秘密

    最近和一个朋友聊天,朋友吐露了工作上的一些不开心,说自己总是喜欢跟别人比较,活得比较累,这种感觉大部分人经历过,往往觉得是自己心态不好,其实不然,这是人性,此时应该快速摆脱这种状态,想到DOTA大9神 ...

  4. PIC12F508/505/509/510/506/519/526/527单片机破解芯片解密方法!

    IC芯片解密PIC12F508/505/509/510/506/519/526/527单片机破解 单片机芯片解密型号: PIC12F508解密 | PIC12F505解密 | PIC12F506解密  ...

  5. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  6. [LeetCode] Scramble String 爬行字符串

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...

  7. HV000030: No validator could be found for type: java.lang.Integer.

    在写接口时,一般去查找在类的Integer属性上加了不属于整型的校验,比如@NotEmpty,@Length等 @JSONField(name = "deviceNum") @No ...

  8. 常look的Git命令

    常用的Git命令   命令  简要说明 git add 添加至暂存区 git add–interactive 交互式添加 git apply   应用补丁 git am  应用邮件格式补丁 git a ...

  9. maven和svn区别

    构建工具-maven,版本控制工具-svn. 一.只有svn的情况        首先考虑没有maven的情况.这样的话,项目组每个开发人员,都需要在本地check out所有的源码. 每次提交之前, ...

  10. Python的多线程(threading)与多进程(multiprocessing )

    进程:程序的一次执行(程序载入内存,系统分配资源运行).每个进程有自己的内存空间,数据栈等,进程之间可以进行通讯,但是不能共享信息. 线程:所有的线程运行在同一个进程中,共享相同的运行环境.每个独立的 ...