使用getopt可以非常方便的格式话选项跟对应的参数,例子如下
#!/bin/bash
set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]
do
case "$1" in
-a)
echo "option -a";;
-b)
value="$2"
echo "option -b with para $value"
shift;;
-c) echo "option -c";;
--) shift
break;;
*) echo "$1 not option";;
esac
shift
done
count=1
for para in "$@"
do
echo "#$count=$para"
count=$[$count+1]
done
./test -ab test1 -cd test2 test3 test4
结果:option -a
option -b with para 'test1'
option -c
#1='test2'
#2='test3'
#3='test4'
getopt 用法:
getopt options optstring parameters
对于./test -ab test1 -cd "test2 test3" test4
这样的参数类型,getopt是无能为力的。这就需要getopts了
#!/bin/bash
while getopts :ab:c opt
do
case $opt in
a) echo "-a option";;
b) echo "-b option with value $OPTARG";;
c) echo "-c option";;
*) echo $opt not a option;;
esac
done
OPTARG 环境变量存放对应选项的参数
./test -ab "hello,world" -cdefg
结果:
-a option
-b option with value hello,world
-c option
? not a option
? not a option
? not a option
? not a option
命令参数的处理
#!/bin/bash
while getopts :ab:cd opt
do
case $opt in
a) echo "-a option";;
b) echo "-b option with value $OPTARG";;
c) echo "-c option";;
d) echo "-d option";;
*) echo $opt not a option;;
esac
done
count=1
shift $[$OPTIND-1]
for para in "$@"
do
echo "#$count=$para"
count=$[$count+1]
done
./test -abtest1 -cd test2 test3 test4
结果:
-a option
-b option with value test1
-c option
-d option
#1=test2
#2=test3
#3=test4
OPTIND环境变量存放getopts命令剩余的参数列表当前位值
用法:getopts optstring variable
分享到:
相关推荐
linux中getopt的用法
win10 vs2019下的 getopt及getopt_long getopt/getopt_long函数是GNU C中的函数
测试过,很好用~ getopt.c getopt.h为利用代码 其他两个为例子
getopt.h getopt.c getopt getopt_long 字符串 转 命令行 编译通过
getopt.h getopt.c getopt_long 函数说明
C#中的getopt封装,方便处理cmd命令。添加引用后,使用范例如下: using CodePoints; using System; ... public static void Main ( string [] args ) { int c = 0, aflag = 0, bflag = 0; string cvalue = "...
getopt库,包含源码,32位库,64位库。getopt库主要用于QRencode库的生成。经过亲自测试使用,运行正常。
java-getopt-1.0.14.jar
功能:C++命令行参数解析类...(控制台应用程序中使用)在 Windows 平台下使用 Linux 的 getopt, getopt_long, getopt_long_only 函数。压缩包内包含getopt.h,getopt.c和使用代码示例。在VS2010下编译通过,运行良好。
在 Windows 平台下使用 Linux 的 getopt, getopt_long, getopt_long_only 函数。
getopt源代码是用于linux和unix系统的函数源代码;非常有价值。希望对你有帮助。
今天小编就为大家分享一篇关于Shell脚本中使用getopts处理多命令行选项,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
缺少getopt.h头文件的,将文件下载后,放在你创建的工程下,并包含进你的代码里可以了。很好用! 适合C与C++,编译出错的话,可以关闭编译器重新编译!
getopt源文件
命令行解析函数getopt使用方法详解,更方便分析命令参数
提供Linux下的getopt.h和cpp,可以放到windows下的vc直接编译通过
getopt实验代码
shell默认使用链表来构建查询系统,但也提供了平衡二叉树的方式,在shell.h中有开启的开关,如不需要可以不添加avltree.c。 include "shell.h" 2,先对硬件进行基本的初始化。 撰写串口发送函数,形如 void usart_...
getopt/getopt_long函数是GNU C中的函数,在linux编程中很常用到。这里就不介绍了。 windows下没有找到类似的函数,于是乎从glibc中找出来修改为windows可用的命令行参数。 vs亲写可用
getopt函数用法 很详细 需要的下载