Shell 腳本的參數解析工具

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 |
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/sUEyRu4HMU9Gm2RHz8xpBw