云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > linux学习笔记

linux学习笔记

  • 62 次阅读
  • 3 次下载
  • 2025/5/6 22:16:51

选项。

现在当你使用复杂的选项运行脚本时,事情就更好了:

$ ./test18 -ac Found the -a option Found the -c option $

当然,所有的原始功能工作都很好

$ ./test18 -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’ $

现在事情看起来很漂亮。然而,还有一个小bug,潜伏在getopt命令。看看这个例子:

$ ./test18 -a -b test1 -cd \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命令不善于处理有空格的参数值。将空格作为参数分隔符,而不是跟随双引号,将两者结合成一个参数。对我们来说幸运的是,还有一个解决这个问题的解决方案。 The more advanced getopts

getopts命令是建立在bash shell上的。它看起来很像getopt表哥,但有一些扩展功能。

不像getopt,所有的在命令行中的选项和参数都只有一个输出。 当它无参运行时,它的退出状态大于零。这使得在命令行中使用循环来分析所有的参数是非常好的。 getopts命令的格式是:

getopts optstring variable

optstring的值与用getopt命令的相似。如果列表有效选项要求参数值,往往伴随着一个冒号。为了抑制错误信息,以冒号来开始optstring。

Getopts使用的环境变量有两个,一个是OPTARG,另一个是OPTIND。 让我们看一个使用getopts命令的简单例子:

$ cat test19 #!/bin/bash

# simple demonstration of the getopts command

搜索更多关于: linux学习笔记 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

选项。 现在当你使用复杂的选项运行脚本时,事情就更好了: $ ./test18 -ac Found the -a option Found the -c option $ 当然,所有的原始功能工作都很好 $ ./test18 -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’ $ 现在事情看起来很漂亮。然而,还有一个小bug,潜伏在getopt命令。看看这个例子:

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com