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循环,然后使用下一个循环中输出剩下的变量

最新文章

  1. 带后台服务配置的tomcat使用
  2. 快排查找第K小的数
  3. io端口与io内存详解
  4. HTML基础(三)——css样式表
  5. 转载 Servlet3.0中使用注解配置Servle
  6. [BTS]The join order has been enforced because a local join hint is used.;Duplicate key was ignored.".
  7. 我的工具箱之Securecrt6.5.0
  8. 关于mysql的基础知识
  9. JAXB注解【转】
  10. 问题记录-Activity跳转后显示空白界面
  11. (转)文件系统缓存dirty_ratio与dirty_background_ratio两个参数区别
  12. FPGA知识大梳理(三)verilogHDL语法入门(2)知识汇总
  13. 201521123096《Java程序设计》第十周学习总结
  14. 217. Contains Duplicate (leetcode)
  15. ITU-T Technical Paper: IP网络测量模型
  16. 第一节: dingo/API 最新版 V2.0 之安装讲解(连载)
  17. SNMP弱口令漏洞的使用
  18. [转载]要提高SQL查询效率where语句条件的先后次序应如何写
  19. Python(十)之GUI编程
  20. Linux下dmesg命令处理故障和收集系统信息的7种用法

热门文章

  1. Linux计划任务,自动删除n天前的旧文件【转】
  2. win7中搜索文件内容的方法
  3. Apache httpd和JBoss构建高可用集群环境
  4. android bluetooth蓝牙移植
  5. office 2013 产品秘钥
  6. JAVA基础知识之网络编程——-基于TCP通信的简单聊天室
  7. 网页撤销后ubuntu本地撤销
  8. HTML5之结构元素
  9. jquery easyui中文培训文档
  10. C# 发送邮件代码