Shell 脚本的参数解析工具

杰哥的IT之旅

共 6775字,需浏览 14分钟

 ·

2021-04-06 17:54

公众号关注“杰哥的IT之旅”,

选择“星标”,重磅干货,第一时间送达!


Argbash是一个代码生成器,它为你的脚本生成一个量身定制的解析库。与其他bash模块的通用代码不同,它生成你的脚本所需的最少代码。此外,如果你不需要100%符合那些CLI标准的话,你可以生成更简单的代码。

Shell脚本的参数解析工具

1. 使用空格分隔

使用空格作为参数分隔

实际用法

./myscript.sh -e conf -s /etc -l /usr/lib /etc/hosts

实现脚本

#!/bin/bash

POSITIONAL=()
while [[ $# -gt 0 ]]; do
  key="$1"

  case $key in
    -e|--extension)
      EXTENSION="$2"
      shift # past argument
      shift # past value
      ;;
    -s|--searchpath)
      SEARCHPATH="$2"
      shift # past argument
      shift # past value
      ;;
    -l|--lib)
      LIBPATH="$2"
      shift # past argument
      shift # past value
      ;;
    --default)
      DEFAULT=YES
      shift # past argument
      ;;
      *)
    POSITIONAL+=("$1"# save it in an array for later
      shift # past argument
      ;;
  esac
done

set -- "${POSITIONAL[@]}" # restore positional parameters

echo FILE EXTENSION  = "${EXTENSION}"
echo SEARCH PATH     = "${SEARCHPATH}"
echo LIBRARY PATH    = "${LIBPATH}"
echo DEFAULT         = "${DEFAULT}"
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)
if [[ -n $1 ]]; then
  echo "Last line of file specified as non-opt/last argument:"
  tail -1 "$1"
fi

2. 使用等号分隔

使用等号作为参数分隔

实际用法

./myscript.sh -e=conf -s=/etc -l=/usr/lib /etc/hosts

实现脚本

#!/bin/bash

for key in "$@"do
  case $key in
    -e=*|--extension=*)
      EXTENSION="${key#*=}"
      shift # past argument=value
      ;;
    -s=*|--searchpath=*)
      SEARCHPATH="${key#*=}"
      shift # past argument=value
      ;;
    -l=*|--lib=*)
      LIBPATH="${key#*=}"
      shift # past argument=value
      ;;
    --default)
      DEFAULT=YES
      shift # past argument with no value
      ;;
    *)
      ;;
  esac
done

echo "FILE EXTENSION  = ${EXTENSION}"
echo "SEARCH PATH     = ${SEARCHPATH}"
echo "LIBRARY PATH    = ${LIBPATH}"
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)
if [[ -n $1 ]]; then
  echo "Last line of file specified as non-opt/last argument:"
  tail -1 $1
fi |

3. 使用 getopts 工具

使用第三方工具进行参数解析

实际用法

./myscript.sh -h

./myscript.sh -v -f

实现脚本

#!/bin/sh

# 重置以防止在前面的shell中使用getopts工具(这是一个POSIX变量)
OPTIND=1

# 初始化变量名称
OUTPUT_FILE=""
VERSION=0

# getopts的缺点就是它只能处理短选项,如-h,而不能是--help格式
while getopts "h?vf:" key; do
    case "$key" in
    h|\?)
        show_help
        exit 0
        ;;
    v)
        VERSION=1
        ;;
    f)
        output_file=$OPTARG
        ;;
    esac
done

shift $((OPTIND-1))

"${1:-}" = "--" ] && shift

echo "verbose=$VERSION, output_file='$output_file', Leftovers: $@" |

4. 使用 argbash 工具

动态的参数解析工具

这个工具主要提供脚本参数的解析功能,而且不再引用任何第三方库的情况下。就我使用而言,一般会比普通脚本多30多行而且,但是效果非常好。

详细信息可以通过官方网站地址了解。

https://argbash.io/generate#results

#!/bin/bash
# This is a rather minimal example Argbash potential
# Example taken from http://argbash.readthedocs.io/en/stable/example.html


# [可选参数]
# ARG_OPTIONAL_SINGLE([option], [o], [optional argument help msg])

# [可选布尔参数]
# ARG_OPTIONAL_BOOLEAN([print], , [boolean optional argument help msg])

# [固定参数]
# ARG_POSITIONAL_SINGLE([positional-arg], [positional argument help  msg], )

# [帮助信息]
# ARG_HELP([The general script's help msg])
# ARGBASH_GO


# [ <-- needed because of Argbash

echo "Value of --option: $_arg_option"
echo "print is $_arg_print"
echo "Value of positional-arg: $_arg_positional_arg"

# ] <-- needed because of Argbash |

文章作者: Escape
文章链接: https://escapelife.github.io/posts/9b814911.html

推荐阅读

一文读懂 Shell 中各种括号的作用

神奇,原来 Shell 脚本还能这么玩!

Linux Shell 自动交互/人机交互的 3 种方法

必会的 24 道 Shell 脚本面试题

编写可靠 Shell 脚本需注意的几点建议呢?

使用 Shell 在多服务器上批量操作

Shell 脚本的 10 个有用的“面试问题和解答”

Shell 脚本中 '$' 符号的多种用法

黑客神技:利用 Shell 脚本掩盖 Linux 服务器上的操作痕迹

编写 Shell 脚本的最佳实践

这些 Shell 分析服务器日志命令集锦,收藏好了~

Shell脚本编写及常见面试题

Shell 命令执行可视化和告警工具

史上最全的 Linux Shell 文本处理工具集锦

23 个非常实用的 Shell 拿来就用脚本实例

Shell 脚本攻略之小试牛刀

浏览 51
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报