shift移动变量
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移动变量的更多相关文章
- 10_bash_变量_条件判断及运算_sed_循环
shell编程: 编译器.解释器编程语言:机器语言.汇编语言.高级语言 静态语言:编译型语言 强类型(变量):变量在使用前,必须事先声明,甚至还需要初始化 事先转换成可执行格式 C/C++.C#.Ja ...
- 每天写点shell--命令行参数
1.读取参数:位置参数变量是标准的数字: $0是程序名, $1是第一个参数, $2是第二个参数... #!/bin/bash # using one command line parameter fa ...
- Perl 的面向对象编程
转自 http://net.pku.edu.cn/~yhf/tutorial/perl/perl_13.html 拓展阅读 http://bbs.chinaunix.net/forum.php?mod ...
- netbeans使用技巧总结+快捷键大全
部分经常用的快捷键: 使用快捷键Alt+Enter显示修复错误的方法. 4.导航 a)Alt+Shift+O :转到类. b)Ctrl+Tab :在打开的源文件中进行切换. c)Alt+Right ...
- C++程序结构---1
C++ 基础教程Beta 版 原作:Juan Soulié 翻译:Jing Xu (aqua) 英文原版 本教程根据Juan Soulie的英文版C++教程翻译并改编. 本版为最新校对版,尚未定稿.如 ...
- bash shell学习-shell script基础 (笔记)
A chain no stronger than its weakest link. "一着不慎,满盘皆输" 参考资料:鸟哥的Linux私房菜 基础学习篇(第三版) Linux ...
- perl5 第十三章 Perl的面向对象编程
第十三章 Perl的面向对象编程 by flamephoenix 一.模块简介二.Perl中的类三.创建类四.构造函数 实例变量 五.方法六.方法的输出七.方法的调用八.重载九.析构函数十.继承十一. ...
- js01-javascript语法标准和数据类型
语法规则 (1)JavaScript对换行.缩进.空格不敏感. 备注:每一条语句末尾要加上分号,虽然分号不是必须加的,但是为了程序今后要压缩,如果不加分号,压缩之后将不能运行. (2)所有的符号,都是 ...
- shell 构建脚本基础
bash -v test.sh 启用 verbose 调试模式 bash -n test.sh 启用语法检查调试模式 bash -x test.sh 遍历脚本执行过程 一.基础参数 1.shell ...
随机推荐
- ubuntu下配置java环境【转】
转自:http://www.cnblogs.com/fnng/archive/2013/01/30/2883815.html 配置JDK环境 下载 登录oracle的网站去下载JDK7 http:// ...
- [转]微软SerialPort秘籍[SerialPort为什么死锁程序的分析]
既然是秘籍,显然是写一些大家不常找到的,MSDN里遗漏提示大家注意的东西. 用过.net 2.0中,自带SerialPort的人,大多都遇到过.莫名其妙的执行Close的时候会死掉的问题.而Wince ...
- easyrtc-server在ubuntu14.04上的安装方法
easyrtc 官网 http://easyrtc.com/ 1.安装nodejs,安装npm (不知道如何安装请google一下) 2. 查看运行easyrtc 所需要的js 包,在easyrtc ...
- SDUT 2408:Pick apples
Pick apples Time Limit: 1000MS Memory limit: 165536K 题目描述 Once ago, there is a mystery yard which on ...
- Ramsey theorem in Combinarotics
- Round Numbers(组合数学)
Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10484 Accepted: 3831 Descri ...
- Babelfish 分类: 哈希 2015-08-04 09:25 2人阅读 评论(0) 收藏
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36398 Accepted: 15554 Descripti ...
- 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏
RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...
- 1. WP8.1学习笔记
数据绑定 含义:将对象绑定到控件上 2.基本名词 控件:绑定目标 对象:绑定源(数据源) 控件与对象属性的联系:路径 如何绑定 创建对象,设置控件 在控件需要数据绑定的地方使用拓展语法 <But ...
- eclipse出现每次修改代码报heap over错误,然后必须重启tomcat问题,修改过程
首先我在eclipse安装目录下找到eclipse.ini文件 修改了里边的参数 C:/Program Files/Java/jdk1.6.0_39/bin/javaw.exe--launcher.a ...