Chinaunix首页 | 论坛 | 博客
  • 博客访问: 110410
  • 博文数量: 37
  • 博客积分: 85
  • 博客等级: 民兵
  • 技术积分: 370
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-22 16:07
文章分类

全部博文(37)

文章存档

2014年(17)

2013年(17)

2012年(3)

我的朋友

分类: LINUX

2014-06-16 23:27:14

getopt: parse command options
格式:getopt options optstring parameters
optstring定义了命令行有效的选项字母,还定义了哪些选项字母需要参数值。在每个需要参数值的选项字母后加一个冒号。如果要去掉错误消息的话,用-q参数。
可以用set命令使getopt命令处理后的命令行选项和参数。set命令用双破折号可以命令行参数替换成set命令的命令行的值。
用法:set -- `getopt -q ab:cd "$@"` 现在原始的命令行参数变量的值会被getopt命令的输出替换,而getopt已经为提供了格式化好的命令行参数。

点击(此处)折叠或打开

  1. $cat test.sh
  2. #!/bin/bash

  3. set -- `getopt -q ab:c "$@"`
  4. while [ -n "$1" ]
  5. do
  6. case "$1" in
  7. -a) echo "Found the -a option";;
  8. -b) param="$2"
  9. echo "Found the -b option, with parameter value $param"
  10. shift;;
  11. -c) echo "Fond the -c option";;
  12. --) shift
  13. break;;
  14. *) echo "$1 is not an option";;
  15. esac
  16. shift
  17. done

  18. count=1
  19. for param in "$@"
  20. do
  21. echo "Parmaeter #$count: $parma"
  22. count=$[ $count + 1 ]
  23. done
脚本可以处理复杂的参数。
$./test.sh -ac
Found the -a option
Found the -c option
$./test.sh -a -b test1 -cd test2 test3 test4
Found the -a option
Found the -b option, with parameter value 'test1'
Found the -c option
Parameter #1: 'test2'
Parameter #2: 'test3'
Parameter #3: 'test4'
但是getopt不能处理带空格的参数值。e.g.
$./test.sh -a -b test1 -cd “test2 test3” test4
Found the -a option
Found the -b option, with parameter value 'test1'
Found the -c option
Parameter #1: 'test2'
Parameter #2: 'test3'
Parameter #3: 'test4'

getopts能够解决上面的这个问题。
getopts是bash shell的builtin命令。getopts每次只处理一个命令行上的检测到的参数,处理完所有的参数后,它会退出并返回一个大于零的退出状态码。所以可以在循环中解析所有命令行参数。
格式:getopts optring variable optstring和getopt中optstring残不多,如果要去掉错误消息的话可以在optstring之前加一个冒号,getopts命令将当前参数保存在命令行中定义的variable中。
getopts命令会用到两个环境变量。如果选项需要一个参数值,OPTARG环境变量就会保存这个值。OPTIND环境变量保存了参数列表中getopts正在出来的参数位置。

点击(此处)折叠或打开

  1. $cat test.sh
  2. #!/bin/bash

  3. while getopts :ab:c opt
  4. do
  5. case "$opt" in
  6. a) echo "Found the -a option";;
  7. b) echo "Found the -b option, with value $OPTARG";;
  8. c) echo "Found the -c option";;
  9. *) echo "Unknown option: $opt";;
  10. esac
  11. done
$./test.sh -ab test1 -c
Found the -a option
Found the -b option, with value test1
Found the -c option

$./test.sh -ab "test1  test2 " -c
Found the -a option
Found the -b option, with value test1 test2
Found the -c option
getopts命令在解析命令行选项时,它会移除开头的单破折号,所以在这里的case定义中不需要单破折号。
getopts能够正确处理有空格的命令行选项。
getopts能够将命令行上找到的所有未定义的选项统一输出成问号。
$./test.sh -d
Unknown option: ?
getopts命令知道什么时候停止处理选项,并将参数留给你处理。在getopts处理每个选项时,它会将OPTIND环境变量值增一。在getopts完成处理时,你可以将OPTIND值和shift命令一起使用来移动参数:

点击(此处)折叠或打开

  1. $cat test.sh
  2. #!/bin/bash
  3. while getopts :ab:cd opt
  4. do
  5. case "$opt" in
  6. a) echo "Found the -a option";;
  7. b) echo "Found the -b option, with value $OPTARG";;
  8. c) echo "Found the -c option";;
  9. d) echo "Found the -d option";;
  10. *) echo "Unknown option: $opt";;
  11. esac
  12. done
  13. shift $[ $OPTIND - 1 ]
  14. count=1
  15. for param in "$@"
  16. do
  17. echo "Parameter $count: $param"
  18. count=$[ $count + 1 ]
  19. done
$./test.sh -a -b test1 -d test2 test3 test4
Found the -a option
Found the -b option, with value test1
Found th -d option
Parameter 1: test2
Parameter 2: test3
Parameter 3: test4





阅读(1463) | 评论(0) | 转发(0) |
0

上一篇:diff && patch

下一篇:http与https的区别

给主人留下些什么吧!~~