1、移动变量

脚本 sh05.sh

#!/bin/bash
# Program
# Program shows the effect of shift function
# History:
# // zengdp First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH echo "Total parameter number is ==> $#"
echo "You whole parameter is ==> '$@'"
shift
echo "Total parameter number is ==> $#"
echo "You whole parameter is ==> '$@'"
shift
echo "Total parameter number is ==> $#"
echo "You whole parameter is ==> '$@'"

运行 sh sh05.sh one two three four five six

得到

Total parameter number is     ==>
You whole parameter is ==> 'one two three four five six'
Total parameter number is ==>
You whole parameter is ==> 'two three four five six'
Total parameter number is ==>
You whole parameter is ==> 'five six'

光看结果你就可以知道啦,那个 shift 会移动变量,而且 shift 后面可以接数字,代表拿掉最前面的几个参数的意思。 上面的运行结果中,第一次进行 shift 后他的显示情况是『 one two three four five six』,所以就剩下五个啦!第二次直接拿掉三个,就变成『 two three four five six 』啦!

2、 当使用shift移动一个变量时,使用while输出第一个变量,从而输出在输入时所有的变量

文件名为 test13.sh

 #!/bin/bash
# demostrating the shift command count=
while [ -n "$1" ]
do
echo "Parameter #$count=$1"
count=$[$count + ]
shift
done

脚本执行一个while循环,测试第一个参数的长度,当第一个参数值长度为0时,结束循环,测试第一个参数后,使用shift命令将所有参数左移一个位置

sh test13.sh rich barbara katie jessica

输出为:

Parameter #=rich
Parameter #=barbara
Parameter #=katie
Parameter #=jessica

3、从参数中分离选项

执行shell脚本时经常会遇到既需要使用选项有需要使用参数的情况,在Linux中的标准方式是通过特殊字符吗将二者分开,这个特殊字符吗告诉脚本选项结束和普通参数开始的位置
对于Linux,这个特殊字符吗就是双破折号(--),shell使用双破折号知识选项列表的结束,发现双破只好后,脚本就能够安全的将剩余的命令行参数作为参数而不是选项处理

文件名为 test16.sh

 #!/bin/bash
# extracting options and parameters while [ -n "$1" ]
do
case "$1" in
-a) echo "Found the -a option" ;;
-b) echo "Found the -b option";;
-c) echo "Found the -c option" ;;
--) shift
break ;;
*) echo "$1 is not an option";;
esac
shift
done count=
for param in $@
do
echo "Parameter #$count: $param"
count=$[ $count + ]
done

这个脚本使用break命令使得遇到双破折号时从while循环中跳出,因为提前从循环中跳出,所以需要保证加入一个shift命令将双破折号从参数变量中丢弃

第一个测试,尝试使用普通选项和参数集运行脚本

sh test16.sh -a -b -c test1 test2 test3

输出为:

Found the -a option
Found the -b option
Found the -c option
test1 is not an option
test2 is not an option
test3 is not an option

第二次测试,使用双破折号将命令行中选项和参数分开

sh test16.sh -a -b -c -- test1 test2 test3

输出结果为

Found the -a option
Found the -b option
Found the -c option
Parameter #: test1
Parameter #: test2
Parameter #: test3

这时可以看到输出的结果中,因为识别到'--'符号,便跳出了while循环,然后使用下一个循环中输出剩下的变量

shift移动变量的更多相关文章

  1. 10_bash_变量_条件判断及运算_sed_循环

    shell编程: 编译器.解释器编程语言:机器语言.汇编语言.高级语言 静态语言:编译型语言 强类型(变量):变量在使用前,必须事先声明,甚至还需要初始化 事先转换成可执行格式 C/C++.C#.Ja ...

  2. 每天写点shell--命令行参数

    1.读取参数:位置参数变量是标准的数字: $0是程序名, $1是第一个参数, $2是第二个参数... #!/bin/bash # using one command line parameter fa ...

  3. Perl 的面向对象编程

    转自 http://net.pku.edu.cn/~yhf/tutorial/perl/perl_13.html 拓展阅读 http://bbs.chinaunix.net/forum.php?mod ...

  4. netbeans使用技巧总结+快捷键大全

      部分经常用的快捷键: 使用快捷键Alt+Enter显示修复错误的方法. 4.导航 a)Alt+Shift+O :转到类. b)Ctrl+Tab :在打开的源文件中进行切换. c)Alt+Right ...

  5. C++程序结构---1

    C++ 基础教程Beta 版 原作:Juan Soulié 翻译:Jing Xu (aqua) 英文原版 本教程根据Juan Soulie的英文版C++教程翻译并改编. 本版为最新校对版,尚未定稿.如 ...

  6. bash shell学习-shell script基础 (笔记)

    A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版)  Linux ...

  7. perl5 第十三章 Perl的面向对象编程

    第十三章 Perl的面向对象编程 by flamephoenix 一.模块简介二.Perl中的类三.创建类四.构造函数 实例变量 五.方法六.方法的输出七.方法的调用八.重载九.析构函数十.继承十一. ...

  8. js01-javascript语法标准和数据类型

    语法规则 (1)JavaScript对换行.缩进.空格不敏感. 备注:每一条语句末尾要加上分号,虽然分号不是必须加的,但是为了程序今后要压缩,如果不加分号,压缩之后将不能运行. (2)所有的符号,都是 ...

  9. shell 构建脚本基础

    bash -v test.sh 启用 verbose 调试模式 bash -n test.sh  启用语法检查调试模式 bash -x test.sh  遍历脚本执行过程 一.基础参数 1.shell ...

随机推荐

  1. java中使用反射做一个工具类,来为指定类中的成员变量进行赋值操作,使用与多个类对象的成员变量的赋值。

    //------------------------------------------------我是代码的分割线 // 首选是一个工具类,在该工具类里面,定义了一个方法,public void s ...

  2. Java中的TreeMap、Comparable、Comparator

    我们知道HashMap的存储位置是按照key这个对象的hashCode来存放的,而TreeMap则是不是按照hashCode来存放,他是按照实现的Comparable接口的compareTo这个方法来 ...

  3. MySQL存储引擎之InnoDB

    一.The InnoDB Engine Each InnoDB table is represented on disk by an .frm format file in the database ...

  4. OA系统如何使用考勤机数据

    通达OA系统使用考勤机数据目前有两种方法可以实现:一种是通过进行二次开发,将通达OA系统与考勤机结合起来使用:另一种是通过将考勤机的数据导出再导入OA系统中.进行二次开发的话,需要和定制开发工程师联系 ...

  5. greenplum集群安装

    一.环境配置 1.地址分配 192.168.1.201 mdw master 192.168.1.202 sdw1 segment1 192.168.1.203 sdw2 segment2 2.创建用 ...

  6. hdwiki 软件包结构

    HDWiki软件包结构转载自http://www.chinabaike.com/z/shenghuo/pc/2011/0414/814308.html           根目录下的PHP文件     ...

  7. 什么情况下用+运算符进行字符串连接比调用StringBuffer/StringBuilder对象的append性能好

    如果在编写代码的过程中大量使用+进行字符串评价还是会对性能造成比较大的影响,但是使用的个数在1000以下还是可以接受的,大于10000的话,执行时间将可能超过1s,会对性能产生较大影响.如果有大量需要 ...

  8. Java 流程控制语句

    java的流程控制: 1.顺序结构 2.选择结构 a.关系运算.逻辑运算.条件运算 b.if语句 c.if-else语句.if - else if -else语句 d.switch语句. 3.循环语句 ...

  9. 模拟http响应头

    1.打开需要模拟的页面请求头 2.编码要发送的数据 3.通过fsockopen函数发送请求头 代码如下 //方法二 $URL="http://device.test.com/admin/ma ...

  10. 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏

    Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...