加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在bash中同时支持短期和长期期权?

发布时间:2020-12-15 20:06:52 所属栏目:安全 来源:网络整理
导读:参见英文答案 Using getopts in bash shell script to get long and short command line options28个答案我想支持短和长选项在bash脚本,所以一个可以: $ foo -ax --long-key val -b -y SOME FILE NAMES 可能吗? getopt支持长选项。 http://linux.about.co
参见英文答案 > Using getopts in bash shell script to get long and short command line options28个答案我想支持短和长选项在bash脚本,所以一个可以:
$ foo -ax --long-key val -b -y SOME FILE NAMES

可能吗?

getopt支持长选项。

http://linux.about.com/library/cmd/blcmdl1_getopt.htm

这里是一个使用你的参数的例子:

#!/bin/bash

OPTS=`getopt -o axby -l long-key: -- "$@"`
if [ $? != 0 ]
then
    exit 1
fi

eval set -- "$OPTS"

while true ; do
    case "$1" in
        -a) echo "Got a"; shift;;
        -b) echo "Got b"; shift;;
        -x) echo "Got x"; shift;;
        -y) echo "Got y"; shift;;
        --long-key) echo "Got long-key,arg: $2"; shift 2;;
        --) shift; break;;
    esac
done
echo "Args:"
for arg
do
    echo $arg
done

输出$ foo -ax –long-key val -b -y某些文件的名称:

Got a
Got x
Got long-key,arg: val
Got b
Got y
Args:
SOME
FILE
NAMES

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读