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

Bash的命令行参数验证库

发布时间:2020-12-15 21:46:02 所属栏目:安全 来源:网络整理
导读:我正在寻找一个可重用的代码片段,它为bash执行命令行参数验证. 理想情况下类似于Apache Commons CLI提供的功能: Commons CLI支持不同类型的选项: POSIX类似选项(即tar -zxvf foo.tar.gz) GNU就像长选项(即du –human-readable –max-depth = 1) 附加值的短
我正在寻找一个可重用的代码片段,它为bash执行命令行参数验证.

理想情况下类似于Apache Commons CLI提供的功能:

Commons CLI支持不同类型的选项:

> POSIX类似选项(即tar -zxvf foo.tar.gz)
> GNU就像长选项(即du –human-readable –max-depth = 1)
>附加值的短期权(即gcc -O2 foo.c)
>单个连字符的长选项(即ant -projecthelp)
> ……

它会自动为程序生成“使用”消息,如下所示:

usage: ls
 -A,--almost-all          do not list implied . and ..
 -a,--all                 do not hide entries starting with .
 -B,--ignore-backups      do not list implied entried ending with ~
 -b,--escape              print octal escapes for nongraphic characters
    --block-size <SIZE>   use SIZE-byte blocks
 -c                       with -lt: sort by,and show,ctime (time of last
                          modification of file status information) with
                          -l:show ctime and sort by name otherwise: sort
                          by ctime
 -C                       list entries by columns

我会在我的Bash脚本开头包含此代码段,并在脚本中重复使用它.

必须有这样的东西.我不相信我们都在编写这种效果或类似的代码:

#!/bin/bash

NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS=3

number_of_supplied_command_line_arguments=$#

function show_command_usage() {
  echo usage:
  (...)
}

if (( number_of_supplied_command_line_arguments < NUMBER_OF_REQUIRED_COMMAND_LINE_ARGUMENTS )); then
  show_command_usage
  exit
fi

...

解决方法

这是我使用的解决方案(在网上找到它,可能在这里,不记得肯定).请注意,GNU getopt(/usr/bin/getopt)使用-a选项支持单个破折号长选项(ant -projecthelp样式),但是我没有使用它,所以它没有在示例中显示.

此代码解析3个选项: – host值或-h值,– port value或-p value和–table value或-t value.如果未设置所需参数,则对其进行测试

# Get and parse options using /usr/bin/getopt
OPTIONS=$(getopt -o h:p:t: --long host:,port:,table: -n "$0" -- "$@")
# Note the quotes around `$OPTIONS': they are essential for handling spaces in 
# option values!
eval set -- "$OPTIONS"

while true ; do
    case "$1" in
            -h|--host) HOST=$2 ; shift 2 ;;
            -t|--table)TABLE=$2 ; shift 2 ;;
            -p|--port)
                    case "$2" in
                            "") PORT=1313; shift 2 ;;
                            *)  PORT=$2; shift 2 ;;
                    esac;;
            --) shift ; break ;;
            *) echo "Internal error!" ; exit 1 ;;
    esac
done
if [[ -z "$HOST" ]] || [[-z "$TABLE" ]] || [[ -z "$PORT" ]] ; then
    usage()
    exit
if

使用getopts shell内置的替代实现(这仅支持小选项):

while getopts ":h:p:t:" option; do
    case "$option" in
         h) HOST=$OPTARG ;;
         p) PORT=$OPTARG ;;
         t) TABLE=$OPTARG ;;
        *) usage(); exit 1 ;;
    esac
done
if [[ -z "$HOST" ]] || [[-z "$TABLE" ]] || [[ -z "$PORT" ]] ; then
    usage()
    exit
if

shift $((OPTIND - 1))

进一步阅读GNU getopt和getopts bash builtin

(编辑:李大同)

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

    推荐文章
      热点阅读