https://www.cnblogs.com/FrankTan/archive/2010/03/01/1634516.html

sh参数处理方法

    * 手工处理方式
* getopts #好像不支持长选项,但更简单点
* getopt # 支持长选项
    *    $0 : ./test.sh,即命令本身,相当于C/C++中的argv[0]
* $1 : -f,第一个参数.
* $2 : config.conf
* $3, $4 ... :类推。
* $# 参数的个数,不包括命令本身,上例中$#为4.
* $@ :参数本身的列表,也不包括命令本身,如上例为 -f config.conf -v --prefix=/home
* $* :和$@相同,但"$*" 和 "$@"(加引号)并不同,"$*"将所有的参数解释成一个字符串,而"$@"是一个参数数组。如下例所示:

1.参数&选项 optstring parameters

-f为选项,它需要一个参数,即config.conf
-v 也是一个选项,但它不需要参数
--prefix长选项,可以是等号,也可以是空格. ./test.sh -f config.conf -v --prefix=/home
./test.sh -f config.conf -v --prefix /home
./test.sh -a -b -c                :短选项,各选项不需参数
./test.sh -abc :短选项,和上一种方法的效果一样,只是将所有的选项写在一起。 ./test.sh -a args -b -c :短选项,其中-a需要参数,而-b -c不需参数。
./test.sh --a-long=args --b-long :长选项

getopt参数说明

TEMP=getopt -o ab:c:: --long a-long,b-long:,c-long:: -n 'example.bash' -- "$@"

  1. -o表示短选项
  2. --long表示长选项
  3. 一个冒号表示有参数
  4. 两个冒号表示该选项有一个可选参数,可选参数必须紧贴选项,如-carg 而不能是-c arg
比如我们使用
./test -a -b arg arg1 -c 你可以看到,命令行中多了个arg1参数,在经过getopt和set之后,命令行会变为:
-a -b arg -c -- arg1
$1指向-a,$2指向-b,$3指向arg,$4指向-c,$5指向--,而多出的arg1则被放到了最后。 while循环这个参数列表: 逐次裁掉参数, arg1裁掉-a --> -b arg -c -- arg1
$1指向了-b, $2指向了arg

一个例子

TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: -n 'example.bash' -- "$@"`

eval set -- "$TEMP"

while true ; do
echo "-------: $1"
case "$1" in
-a|--a-long) echo "Option a" ; shift ;; # -a -b arg -c -- arg1裁掉-a --> -b arg -c -- arg1
-b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;; # -b arg -c -- arg1 裁掉-b arg--> -c -- arg1
-c|--c-long)
# c has an optional argument. As we are in quoted mode,
# an empty parameter will be generated if its optional
# argument is not found.
case "$2" in
"") echo "Option c, no argument"; shift 2 ;;
*) echo "Option c, argument \`$2'" ; shift 2 ;;
esac ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done

达到这样的目的: ./start --delay=10

DELAY=60
# read the options
TEMP=`getopt -o d: --long delay:,ServerID:,appname: -n 'test.sh' -- "$@"`
echo $TEMP
eval set -- "$TEMP" while true ; do
case "$1" in
-d|--delay) DELAY=$2 ; shift 2 ;;
--ServerID) ServerID=$2 ; shift 2 ;;
--appname) appname=$2 ; shift 2 ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done sleep ${DELAY}

https://blog.csdn.net/wh211212/article/details/53750366

http://blog.51cto.com/1008305/1152722

[sh]getopt参数解析的更多相关文章

  1. Python命令行参数解析模块getopt使用实例

    Python命令行参数解析模块getopt使用实例 这篇文章主要介绍了Python命令行参数解析模块getopt使用实例,本文讲解了使用语法格式.短选项参数实例.长选项参数实例等内容,需要的朋友可以参 ...

  2. Zookeeper + Hadoop2.6 集群HA + spark1.6完整搭建与所有参数解析

    废话就不多说了,直接开始啦~ 安装环境变量: 使用linx下的解压软件,解压找到里面的install 或者 ls 运行这个进行安装 yum install gcc yum install gcc-c+ ...

  3. python命令行参数解析模块argparse和docopt

    http://blog.csdn.net/pipisorry/article/details/53046471 还有其他两个模块实现这一功能,getopt(等同于C语言中的getopt())和弃用的o ...

  4. Python学习笔记之参数解析

    python提供了两种方法进行命令行的参数解析,分别是getopt和optparse类中的模块OptionParser,下面分别详细了解这两个模块: 1.getopt模块 首先复习C语言的命令行解析: ...

  5. kafka auto.offset.reset参数解析

    kafka auto.offset.reset参数解析 1.latest和earliest区别 2.创建topic 3.生产数据和接收生产数据 4.测试代码 auto.offset.reset关乎ka ...

  6. python获取命令行传参的两种种常用方法argparse解析getopt 模块解析

    方法一:argparse解析 #!/usr/bin/env python3 # -*- coding:utf-8 -*- # @Time: 2020/5/20 10:38 # @Author:zhan ...

  7. 写个C#命令行参数解析的小工具

    最近测试工作做的比较多因此时常要创建一些控制台类型的应用程序.因为程序有不同的参数开关,需要在程序启动的时候通过命令行来给程序传递各种开关和参数.直接操作args有些不方便,所以就写了个解析参数的小工 ...

  8. Python--命令行参数解析Demo

    写没有操作界面的程序时,最讨厌的就是参数解析问题,尤其是很多参数那种,下面是一个小Demo,拿出来与各位分享: # -*- coding:utf8 -*- import os import datet ...

  9. Node基础:url查询参数解析之querystring

    模块概述 在nodejs中,提供了querystring这个模块,用来做url查询参数的解析,使用非常简单. 模块总共有四个方法,绝大部分时,我们只会用到 .parse(). .stringify() ...

随机推荐

  1. [原]jenkins(五)---jenkins添加项目

    /** * lihaibo * 文章内容都是根据自己工作情况实践得出. http://www.cnblogs.com/horizonli/p/5332258.html 版权声明:本博客欢迎转发,但请保 ...

  2. mtr

    一般在windows 来判断网络连通性用ping 和tracert,ping的话可以来判断丢包率,tracert可以用来跟踪路由,在Linux中有一个更好的网络连通性判断工具,它可以结合ping ns ...

  3. Unity3D Shader 马赛克后期效果

    //效果图 //Shader代码 Shader "Hidden/Mosaic" { Properties { _MainTex ("Texture", 2D) ...

  4. Spring学习笔记--Spring简介

    1.spring:给软件行业带来了春天; 2.spring的理念:spring框架的初衷是使的现有的更加实用,spring不是创造轮子(技术或框架),而是使现有的轮子更好的运转;spring本身是一个 ...

  5. got positional argument after named arguments.原因

  6. D. Who killed Cock Robin 湖北省大学程序设计竞赛

    链接:https://www.nowcoder.com/acm/contest/104/C来源:牛客网 The Sparrow is for trial, at next bird assizes,w ...

  7. [development][security][suricata] suricata 使用与调研

    0: OISF:https://oisf.net/ 1: suricata是什么 https://suricata-ids.org/ 2:安装 https://redmine.openinfosecf ...

  8. mongodb操作文件

    mongodb操作文件,主要是通过GridFS类.存储文件主要存放在fs中,其中的fs是数据库默认的.并且GridFS是直接与数据库打交道,与collection集合无关. ============= ...

  9. python之gunicorn的配置

    https://www.cnblogs.com/cwp-bg/p/8780204.html python常见的web部署搭配nginx+gunicorn,下面记录一下gunicorn的配置使用. 安装 ...

  10. LeetCode 897 Increasing Order Search Tree 解题报告

    题目要求 Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the r ...