/bin/bash# A small example program for using the new getopt(1) program.# This program will only work with bash(1)# An similar program using the tcsh(1) script language can be found# as parse.tcsh# Example input and output (from the bash prompt):# ./parse.bash -a par1 'another a...
2、getopts,这是shell内置的一种处理参数的方法,可以处理单个字符选项,例如 -h 192.168.1.1 这种形式的参数 3、getopt,这是unix自带的一种处理命令行参数的方法,既可以处理单个字符选项,也可以处理长选项,例如:–host 192.168.1.1 或–host=192.168.1.1 这里介绍下getopt的用法 使用介绍 代码语言:javascript 代码运行...
模式化选项:例如script_name MODE OPTIONS的MODE部分,可以是manage模式(--manage,-m),也可以使用add模式(--add,-a) 选项参数替代选项:例如head -n 3可以替换为head -3 这里介绍下用getopt解析参数后实现它们的思路。 在getopt解析完成后,假设返回结果保存到了$parameters变量中。 1.选项依赖性 这个其实很好实现...
2.getopts来处理,单个字符选项的情况(如:-n 10 -f file.txt等选项);3.getopt,可以处理单个字符选项,也可以处理长选项long-option(如:--prefix=/home等)。总结:一般小脚本手工处理也许就够了,getopts能处理绝大多数的情况,getopt较复杂、功能也更强大。下面分别进行简单的说明:1.直接手工处理位置参数必须要要...
处理命令行参数是一个相似而又复杂的事情,为此,C提供了getopt/getopt_long等函数, C++的boost提供了Options库,在shell中,处理此事的是getopts和getopt. getopts和getopt功能相似但又不完全相同,其中getopt是独立的可执行文件,而getopts是由Bash内置的。 先来看看参数传递的典型用法: ...
This is why the built-ingetopts is superior, albeit limited by the fact it only handles single-letter options. FBIWarnin ---x-wx 11 here's an example of using GNU getopt, from a script of called javawrap:---# NOTE: This requires GNU getopt. On Mac OS X, you get BSD getopt ...
这里放下如何用getopts解析长格式的输入参数方法: #!/usr/bin/env bash optspec=":hv-:" while getopts "$optspec" optchar; do case "${optchar}" in -) case "${OPTARG}" in loglevel) val="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 )) ...
OPTIND:初始化为1, 每次getopts处理完一个命令行选项后,OPTIOND就增加为getopts要处理的下一个选项的序号 OPTARG:包含了对应variable的选项的参数的值 options-strings开始部分没有冒号: 会按照系统的定义报错 a. 指定了非法选项,会报错:scriptname: illegal option -- p ...
处理命令行参数是一个相似而又复杂的事情,为此,C提供了getopt/getopt_long等函数, C++的boost提供了Options库,在shell中,处理此事的是getopts和getopt.getopts和getopt功能相似但又不完全相同,其中getopt是独立的可执行文件,而getopts是由Bash内置的。 先来看看参数传递的典型用法: ...
1、bash 内置的getopts: 先看简单的例子: #!/bin/bashwhile getopts 'd:Dm:f:t:' OPT; docase$OPTind)DEL_DAYS="$OPTARG";;D)DEL_ORIGINAL='yes';;f)DIR_FROM="$OPTARG";;m)MAILDIR_NAME="$OPTARG";;t)DIR_TO="$OPTARG";;?)echo"Usage:`basename$0`[options] filename"esacdoneshift$(($OP...