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. mount源码分析 【转】

    转自:http://blog.chinaunix.net/uid-10769062-id-3230811.html Busybox- 在util-linux/mount.c的line:1609行首先映 ...

  2. 为Docker容器配置固定IP

    当docker以桥接的方式启动容器时,容器内部的IP是经过DHCP获取的,例如:172.17.0.8/32,且每重启依次IP都会发生变动.某些特殊的情况下,需要容器内有自己固定的一个内部IP.我的实现 ...

  3. BZOJ 1588:营业额统计(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意:中文题意. 思路:每一个点每一个点插入Splay,然后插入新的一个点之后,查这个节点的前 ...

  4. discuz模板文件列表

    template/default/common模板公共文件夹,全局相关     |--block_forumtree.htm 树形论坛版块分支js文件     |--block_thread.htm特 ...

  5. Python+Django+Eclipse 在Windows下快速开发自己的网站

    一.配置开发环境 我的开发环境是:Python3.3.2 + Django1.5.2 + Eclipse 1.安装Python 下载地址:http://www.python.org/getit/ 安装 ...

  6. 百度地图瓦片原理 | 百度map使用教程

    百度地图瓦片原理: http://blog.csdn.net/mygisforum/article/details/22997879 百度map使用教程: http://www.myexception ...

  7. Maximum Value(哈希)

    B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  8. Communication System(dp)

    Communication System Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25006 Accepted: 8925 ...

  9. Let the Balloon Rise 分类: HDU 2015-06-19 19:11 7人阅读 评论(0) 收藏

    Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  10. MBB类似jquery.bxslider插件轮播效果

    首先,如图一:当打开页面是,产品出现了淡入淡出切换轮播状态,当鼠标点击左边的小图时,就会切换出相对应的大图,当鼠标移开的时候,这个轮播就会停止自动轮播,只有人工手动才能进行切换:是一个不错的体验! 如 ...