最近写脚本的时候遇到要使用getopt来检查命令行参数是否合法,下面是我的脚本[code] #!/bin/bash ARGS=`getopt -o ab:c:: -l a-long,b-long:,c-long:: -- "$@"` [ $? -ne 0 ] && usage [/code]可是在调用的时候我使用./test.sh --alon的时候它并没有提示错误,我用./test.sh --a-long-t的时候就会提示错误! 是不是getopt在解析的时候匹配不会精确匹配,有没有什么办法让它在./test.sh --alon的时候提示不认识这个选项?
by victorplus - Shell - 2012-07-29 14:26:17 阅读(3474) 回复(9)
LINUX与UNIX shell编程指南上的例子,在linux-bash下运行 代码如下 #!/bin/sh # getopt # modified getopt1 script # set the vars ...
今天学习 getopt命令时,用到了[code]ARGS=`getopt -a -o p:P:O:t:l:h -l process:,port:,time:,output:,user:,help -- "$@"`[/code]这里的 -- "$@" 有什么作用? 因为在后面的代码中 while true: do case $1 in -a) func1;shift;; -b) func2;shift;; --) shift;break;; esac shift done 如果没 --)这里 ,这个循环跳不出来!
1. getopt的作用是用来解析命令行选项,譬如:>mysql -u lvdbing -p test -h 192.168.1.20 2. getopt提供的函数包括:Python 2.6.1+ (r261:67515, Mar 2 2009, 13:11:28) [GCC 4.3.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import getopt >>> getopt.__all__ ['getoptError', 'error', 'getopt', 'gnu_getopt'] >>> 3. 帮助文档的例子:>>> s = '--condition=foo --testing ...
linux 环境下 [code] stan@stan /temp $ ls -al echo.c -rw-r--r-- 1 stan users 300 2008-07-25 12:00 echo.c stan@stan /temp $ ls echo.c -al -rw-r--r-- 1 stan users 300 2008-07-25 12:00 echo.c [/code] 选项参数放在选项之前貌似也是可以的嘛!以前不都是要求选项参数放在选项之后,是否次序无所谓啊?这个功能是由getopt实现的吗? Thanks
char *svalue=NULL; while((opt=getopt(argc,argv," :ls: ")!=-1)) . . . case 's': svalue=optarg; break; RUN: ./a -s file1 1.问题: 如果我想-s接收的是多个不定的参数请问怎么改呢? 如: -s file1 file2 或 -s file1 file2 file3 2.在书上看 opt=getopt(argc,argv," :ls::k ")!=-1 s后两个"::"表示接收两个参数?还是两个以上的参数呢?
我在一本书上看到:如果getopt()找不到符合的参数则会印出错信息,并将全域变量optopt设为“?”字符。
但是程序显示好像不是这样:[code]#include
#include <stdio.h>
#include <stdlib.h>