Shell 命令行解析 getopt工具

摘要:
Linux下使用getopt工具进行命令行解析,可以同时处理长选项和短选项。/bin/bash#-o表示段选项,--long表示长选项#选项后面没有冒号表示没有参数,1个冒号表示必须有参数,2个冒号表示可选参数#对于具有可选参数的选项,没有没有提供值,会返回一个空字符串,并且会占用一个位置TEMP=`getopt-oab:c::--longa-long,b-long:,c-long::-n'example.bash'--"$@"`if[$?!

Linux下使用getopt工具进行命令行解析,可以同时处理长选项和短选项。

NAME
       getopt - parse command options (enhanced)

SYNOPSIS
       getopt optstring parameters
       getopt [options] [--] optstring parameters
       getopt [options] -o|--options optstring [options] [--] parameters

使用方法:

#!/bin/bash

# -o表示段选项,--long表示长选项
# 选项后面没有冒号表示没有参数,1个冒号表示必须有参数,2个冒号表示可选参数
# 对于具有可选参数的选项,没有没有提供值,会返回一个空字符串,并且会占用一个位置

TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: -n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

eval set -- "$TEMP"

while true; do
    case "$1" in
        -a|--a-long)
            echo "Option a"
            shift
            ;;
        -b|--b-long)
            echo "Option b, argument \`$2'"
            shift 2
            ;;
        -c|--c-long)
            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

echo "Remaining arguments:"
for arg do
   echo '--> '"\`$arg'" ;
done

双横岗的含义:

例如:set -- "$progname" "$@"

From help set:

  --  Assign any remaining arguments to the positional parameters.
      If there are no remaining arguments, the positional parameters
      are unset.

The reason for -- is to ensure that even if "$progname" or "$@" contain dashes, they will not be interpreted as command line options.

set changes the positional parameters, which is stored in $@. So in this case, it appends "$progname" to the beginning of the positional parameters received by the script.

免责声明:文章转载自《Shell 命令行解析 getopt工具》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇《Win32多线程程序设计》学习笔记 第10章 MFC 中的线程javascript中的insertBefore方法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

shell替换

如果表达式中包含特殊字符,Shell 将会进行替换。例如,在双引号中使用变量就是一种替换,转义字符也是一种替换。举个例子: #!/bin/bash a=10 echo -e"Value of a is $a " 运行结果: Value of a is 10 这里 -e 表示对转义字符进行替换。如果不使用 -e 选项,将会原样输出: Value...

pg_ctl — 启动、停止、重启 PostgreSQL

pg_ctl 名称 pg_ctl -- 启动、停止、重启 PostgreSQL 语法 pg_ctl start [-w] [-s] [-D datadir] [-l filename] [-o options] [-p path]pg_ctl stop [-W] [-s] [-D datadir] [-m s[mart] | f[ast] | i[m...

《Linux命令行与shell脚本编程大全》第二十五章 创建与数据库、web及电子邮件相关的脚本

25.1 MySQL数据库 /* 但是我在虚拟机上安装的时候居然不提示输入密码。 这个可以参考http://blog.csdn.net/sinat_21302587/article/details/76870457 导致登录的时候不知道账号密码。 默认的账号密码在 /etc/mysql/debian.cnf上。如下图,user和password就是账号密码...

linux中ulimit作用

一、作用 Linux对于每个用户,系统限制其最大进程数。为提高性能,可以根据设备资源情况,设置各linux 用户的最大进程数。 ulimit主要是用来限制进程对资源的使用情况的,它支持各种类型的限制,常用的有: 内核文件的大小限制 进程数据块的大小限制 Shell进程创建文件大小限制 可加锁内存大小限制 常驻内存集的大小限制...

在Linux命令行中操作PDF

pdftk 命令提供了许多处理 PDF 的命令行操作,包括合并页面、加密文件、添加水印、压缩文件,甚至还有修复 PDF。 虽然 PDF 通常被认为是相当稳定的文件,但在 Linux 和其他系统上你可以做很多处理。包括合并、拆分、旋转、拆分成单页、加密和解密、添加水印、压缩和解压缩,甚至还有修复。 pdftk 命令能执行所有甚至更多操作。 “pdftk” 代...

redis 命令行连接去除warning提示

[root@localhost monitor]# echo "config get *" | redis-cli -a 123456a? Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe. 1) "dbfi...