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 ...
随机推荐
- org.apache.cxf.transport.servlet.CXFServlet cannot be cast to javax.servlet.Servlet
java.lang.ClassCastException: org.apache.cxf.transport.servlet.CXFServlet cannot be cast to javax.se ...
- Hadoop Balance
一.hadoop balance工具的用法: 启动balance: bin/start-balancer.sh -threshold 5 停止balance: bin/stop-balancer.sh ...
- Apache2.2 + php-5.4.45-Win32-VC9-x86 配置
首先要注意一个问题是:网上有很多教程比如: 在Apache配置文件中添加php模块.在apache2\conf\httpd.conf中: LoadModule模块添加行: LoadModule php ...
- 杭电1020-Encoding
Problem Description Given a string containing only 'A' - 'Z', we could encode it using the following ...
- Unity脚本在层级面板中的执行顺序测试3
断断续续的写了3篇,以后有时间可以做成一个系列了 前面2篇测试了GameObject的顺序,以及Awake和OnEnable的时机: Unity脚本在层级面板中的执行顺序测试1 http://www. ...
- boost库学习之regex
一.背景 项目中许多地方需要对字符串进行匹配,比如根据指定的过滤字符串来过滤文件名.刚开始是排斥使用boost库的,第一,我不熟悉boost库:第二,如果引入第三方库,就会增加库的依赖,这样的后果是, ...
- ubuntu查看内存占用和查看cpu使用情况的简单方法(ubuntu内存管理)
单独查看内存使用情况的命令:free -m查看内存及cpu使用情况的命令:top也可以安装htop工具,这样更直观,安装命令如下:sudo apt-get install htop安装完后,直接输入命 ...
- A fatal error has been detected by the Java Runtime Environment(jdk 1.6的一个BUG)
几天做项目,生成一堆注解的实体,当实体数超过86个时,jvm报错: # # A fatal error has been detected by the Java Runtime Environmen ...
- 抓取Js动态生成数据且以滚动页面方式分页的网页
代码也可以从我的开源项目HtmlExtractor中获取. 当我们在进行数据抓取的时候,如果目标网站是以Js的方式动态生成数据且以滚动页面的方式进行分页,那么我们该如何抓取呢? 如类似今日头条这样的网 ...
- Struts2应用的开发流程
Struts2的开发流程 为了能够在eclipse中使用Struts2在进行开发时,需要根据需要导入一些有关的jar包: 在官网下载相关的压缩包,这里下载了两个:struts-2.3.30-all.z ...