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. Spring项目启动时执行初始化方法

    一.applicationContext.xml配置bean <bean id="sensitiveWordInitUtil" class ="com.hx.daz ...

  2. declare和typeset DEMO

    declare=typeset,用法完成相同. declare不指定变量:显示所有变量的值. -r选项,把指定变量定义为只读变量: [xiluhua@vm-xiluhua][~]$ declare - ...

  3. Docker centos 安装syslog

    在通常的Linux服务器中,有一些服务本身没有日志,只能通过 tail -f /var/log/messages来查看其运行日志,比如nrpe server.但是,如果想在docker容器中实现这个功 ...

  4. Windows下打包Python的exe可执行文件

    参考:http://www.cnblogs.com/Lands-ljk/p/5447723.html

  5. java 堆栈 静态

    所以静态变量和非静态变量的区别就在于静态变量可以用来计数,而非静态变量则不行. 理解了内存,就理解了一切,就理解了各种各样的语言.所有的语言无非都是这样:局部变量分配内存永远在栈里面,new出来的东西 ...

  6. Chrome开发者工具学习

    Chrome开发者工具分为8个大模块,每个模块功能为: 1.Element标签页:用于查看和编辑当前页面中的HTML和CSS元素. 左侧可以看到页面的源码,HTML和CSS元素,双击可以进行修改.右侧 ...

  7. bootstrap弹出层效果

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content ...

  8. 一些常用的SQL查询语句

    学习网站:http://www.w3cschool.cc/sql/sql-tutorial.html 一:查询所有表的属性 SELECT 'ALTER TABLE '+ CASE WHEN O.sch ...

  9. 《python核心编程》读书笔记--第16章 网络编程

    在进行网络编程之前,先对网络以及互联网协议做一个了解. 推荐阮一峰的博客:(感谢) http://www.ruanyifeng.com/blog/2012/05/internet_protocol_s ...

  10. awk用法小结(作者总结)

    http://www.chinaunix.net/old_jh/24/691456.html http://wenku.baidu.com/view/ebac4fc658f5f61fb736664d. ...