Shell脚本的参数解析工具

系统 Linux
Argbash是一个代码生成器,它为你的脚本生成一个量身定制的解析库。与其他bash模块的通用代码不同,它生成你的脚本所需的最少代码。

[[399027]]

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

Shell脚本的参数解析工具

1. 使用空格分隔

使用空格作为参数分隔

实际用法 

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

实现脚本 

  1. #!/bin/bash  
  2. POSITIONAL=()  
  3. while [[ $# -gt 0 ]]; do  
  4.   key="$1"  
  5.   case $key in  
  6.     -e|--extension)  
  7.       EXTENSION="$2"  
  8.       shift # past argument  
  9.       shift # past value  
  10.       ;;  
  11.     -s|--searchpath)  
  12.       SEARCHPATH="$2"  
  13.       shift # past argument  
  14.       shift # past value  
  15.       ;;  
  16.     -l|--lib)  
  17.       LIBPATH="$2"  
  18.       shift # past argument  
  19.       shift # past value  
  20.       ;;  
  21.     --default)  
  22.       DEFAULT=YES  
  23.       shift # past argument  
  24.       ;;  
  25.       *)  
  26.     POSITIONAL+=("$1") # save it in an array for later  
  27.       shift # past argument  
  28.       ;;  
  29.   esac  
  30. done  
  31. set -- "${POSITIONAL[@]}" # restore positional parameters  
  32. echo FILE EXTENSION  = "${EXTENSION}"  
  33. echo SEARCH PATH     = "${SEARCHPATH}"  
  34. echo LIBRARY PATH    = "${LIBPATH}"  
  35. echo DEFAULT         = "${DEFAULT}"  
  36. echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)  
  37. if [[ -n $1 ]]; then  
  38.   echo "Last line of file specified as non-opt/last argument:"  
  39.   tail -1 "$1"  
  40. fi 

2. 使用等号分隔

使用等号作为参数分隔

实际用法 

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

实现脚本 

  1. #!/bin/bash  
  2. for key in "$@"; do  
  3.   case $key in  
  4.     -e=*|--extension=*)  
  5.       EXTENSION="${key#*=}"  
  6.       shift # past argument=value  
  7.       ;;  
  8.     -s=*|--searchpath=*)  
  9.       SEARCHPATH="${key#*=}"  
  10.       shift # past argument=value 
  11.       ;;  
  12.     -l=*|--lib=*)  
  13.       LIBPATH="${key#*=}"  
  14.       shift # past argument=value  
  15.       ;;  
  16.     --default)  
  17.       DEFAULT=YES  
  18.       shift # past argument with no value  
  19.       ;;  
  20.     *)  
  21.       ;;  
  22.   esac  
  23. done  
  24. echo "FILE EXTENSION  = ${EXTENSION}"  
  25. echo "SEARCH PATH     = ${SEARCHPATH}"  
  26. echo "LIBRARY PATH    = ${LIBPATH}"  
  27. echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)  
  28. if [[ -n $1 ]]; then  
  29.   echo "Last line of file specified as non-opt/last argument:"  
  30.   tail -1 $1  
  31. fi | 

3. 使用 getopts 工具

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

实际用法 

  1. ./myscript.sh -h  
  2. ./myscript.sh -v -f 

实现脚本 

  1. #!/bin/sh  
  2. # 重置以防止在前面的shell中使用getopts工具(这是一个POSIX变量)  
  3. OPTIND=1  
  4. # 初始化变量名称  
  5. OUTPUT_FILE=""  
  6. VERSION=0  
  7. # getopts的缺点就是它只能处理短选项,如-h,而不能是--help格式  
  8. while getopts "h?vf:" key; do  
  9.     case "$key" in  
  10.     h|\?)  
  11.         show_help  
  12.         exit 0  
  13.         ;;  
  14.     v)  
  15.         VERSION=1  
  16.         ;;  
  17.     f)  
  18.         output_file=$OPTARG  
  19.         ;;  
  20.     esac  
  21. done  
  22. shift $((OPTIND-1))  
  23. [ "${1:-}" = "--" ] && shift  
  24. echo "verbose=$VERSION, output_file='$output_file', Leftovers: $@" | 

4. 使用 argbash 工具

动态的参数解析工具

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

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

https://argbash.io/generate#results 

  1. #!/bin/bash  
  2. # This is a rather minimal example Argbash potential  
  3. # Example taken from http://argbash.readthedocs.io/en/stable/example.html  
  4. # [可选参数]  
  5. # ARG_OPTIONAL_SINGLE([option], [o], [optional argument help msg])  
  6. # [可选布尔参数] 
  7. # ARG_OPTIONAL_BOOLEAN([print], , [boolean optional argument help msg])  
  8. # [固定参数] 
  9. # ARG_POSITIONAL_SINGLE([positional-arg], [positional argument help  msg], )  
  10. # [帮助信息]  
  11. # ARG_HELP([The general script's help msg])  
  12. # ARGBASH_GO  
  13. # [ <-- needed because of Argbash  
  14. echo "Value of --option: $_arg_option"  
  15. echo "print is $_arg_print"  
  16. echo "Value of positional-arg: $_arg_positional_arg"  
  17. # ] <-- needed because of Argbash |  

 

责任编辑:庞桂玉 来源: 良许Linux
相关推荐

2022-08-30 08:52:04

shell脚本Linux

2018-04-04 08:59:22

LinuxUNIXShell

2010-06-23 15:55:36

Linux Bash

2014-05-15 10:07:29

2021-05-31 10:55:23

shell脚本Linux

2023-07-31 08:45:10

Shell脚本

2019-08-09 13:50:08

shellLinux

2021-08-30 07:50:42

脚本语言命令行

2013-09-04 09:59:49

监控 Shell 脚本

2021-11-08 10:45:07

Python命令工具

2017-08-11 17:20:07

LinuxShell

2020-04-26 19:12:29

shell脚本Linux

2020-12-16 08:51:31

Shell脚本用途

2020-10-21 11:55:44

Shell编程语言Linux

2013-08-19 12:46:27

2013-08-30 10:25:22

Shell主机监控

2018-08-28 16:02:59

LinuxShellBash

2020-10-27 07:51:12

Shell脚本日期

2011-09-27 13:52:41

2020-06-17 10:42:54

shellshell脚本Linux
点赞
收藏

51CTO技术栈公众号