shell脚本接参数优美用法

shell脚本接参数优美用法

@(学习)[bash]

1. 示例脚本

#!/usr/bin/env bash
# shell脚本接参数优美用法
# Create on 2016-11-18
# @author: Chinge_Yang

args="$*"

function usage(){
    echo "Usage: `basename $0` options (-h HOST|-n NAME)"
}

function check(){
    echo "check"
}

if [ $# -lt 4 ]  
then
    usage
    exit 55     
fi  

# 用法: scriptname -options
# 注意: 必须使用破折号 (-) 
# 第一个冒号表示忽略错误,参数后接冒号,表示必须接值
while getopts ":h:n:" Option;do
  case $Option in
    h)
    host=$OPTARG
    ;;
    n)
    name=$OPTARG
    ;;
    *)
    usage
    ;;   # 默认情况的处理
  esac
done

shift $(($OPTIND - 1))
#  (译者注: shift命令是可以带参数的, 参数就是移动的个数)
#  将参数指针减1, 这样它将指向下一个参数.
#  $1 现在引用的是命令行上的第一个非选项参数,
#+ 如果有一个这样的参数存在的话.

check

exit 0

2. 转载阅读

转载阅读:shell中的getopt与getopts

在shell脚本之shift和getopts篇中有提到getopts,除了bash自带的内部变量getopts外,util-linux-ng包还提供了一个工具getopt ,该工具较bash内置的getopts更强大,其不仅支持短参-s,还支持–longopt的长参数,甚至支持-longopt的简化参数。相较于getopts ,getopt 不但支持长短选项,其还支持选项和参数放在一起写。

2.1 getopt命令的用法

getopt [options] -o|–options optstring [options] [–] parameters
选项说明:

-a:使getopt长参数支持”-“符号打头,必须与-l同时使用

-l:后面接getopt支持长参数列表

-n program:如果getopt处理参数返回错误,会指出是谁处理的这个错误,这个在调用多个脚本时,很有用

-o:后面接短参数列表,这种用法与getopts类似

-u:不给参数列表加引号,默认是加引号的(不使用-u选项),例如在加不引号的时候 –longoption “arg1 arg2” ,只会取到”arg1”,而不是完整的”arg1 arg2”

其有两种使用方法,如下

方法1:

ARGV=($(getopt -o 短选项1[:]短选项2[:]...[:]短选项n -l 长选项1,长选项2,...,长选项n -- "$@"))
for((i = 0; i < ${#ARGV[@]}; i++)) {
    eval opt=${ARGV[$i]}
    case $opt in
    -短选项1|--长选项1)
       process
       ;;
    # 带参数
    -短选项2|--长选项2)
       ((i++));
       eval opt=${ARGV[$i]}
       ;;
    ...
    -短选项n|--长选项n)
       process
       ;;
    --)
       break
       ;;
    esac
}

方法2:

ARGV=($(getopt -o 短选项1[:]短选项2[:]...[:]短选项n -l 长选项1,长选项2,...,长选项n -- "$@"))
eval set -- "$ARGV"
while true
do
    case "$1" in
    -短选项1|--长选项1)
        process
        shift
        ;;
    -短选项2|--长选项2)
        # 获取选项
        opt = $2
        process
        shift 2
        ;;
    ......
    -短选项3|--长选项3)
        process
        ;;
    --)
break
;;
esac
}

注意:如果getopt命令本身没有使用-o|–option选项的话,那么–后面的第一个参数被当做短选项。

2.2 示例

#!/bin/bash
# A small example program for using the new getopt(1) program.
# This program will only work with bash(1)
# Note that we use `"$@"' to let each command-line parameter expand to a
# separate word. The quotes around `$@' are essential!
# We need TEMP as the `eval set --' would nuke the return value of getopt.
TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: -n 'example.bash' -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
        case "$1" in
                -a|--a-long) echo "Option a" ; shift ;;
                -b|--b-long) echo "Option b, argument \`$2'" ; shift 2 ;;
                -c|--c-long)
                        # c has an optional argument. As we are in quoted mode,
                        # an empty parameter will be generated if its optional
                        # argument is not found.
                        case "$2" in
                                "") echo "Option c, no argument"; shift 2 ;;
                                *)  echo "Option c, argument \`$2'" ; shift 2 ;;
                        esac ;;
                --) shift ; break ;;
                *) echo "Internal error!" ; exit 1 ;;
        esac
done
echo "Remaining arguments:"
for arg do echo '--> '"\`$arg'" ; done 

运行结果如下:

[root@test bash]# sh get.sh -a par1 'another arg' --c-long 'wow!*\?' -cmore -b " very long "
Option a
Option c, no argument
Option c, argument `more'
Option b, argument ` very long '
Remaining arguments:
--> `par1'
--> `another arg'
--> `wow!*\?'

使用eval 的目的是为了防止参数中有shell命令,被错误的扩展。

平时使用时,可以使用的样例为:

ARGS=`getopt -a -o I:D:T:e:k:LMSsth -l instence:,database:,table:,excute:,key:,list,master,slave,status,tableview,help -- "$@"`
[ $? -ne 0 ] && usage
#set -- "${ARGS}"
eval set -- "${ARGS}"
while true
do
        case "$1" in
        -I|--instence)
                instence="$2"
                shift
                ;;
        -D|--database)
                database="$2"
                shift
                ;;
        -T|--table)
                table="$2"
                shift
                ;;
        -e|--excute)
                excute="yes"
                shift
                ;;
        -k|--key)
                key="$2"
                shift
                ;;
        -L|--list)
                LIST="yes"
                ;;
        -M|--master)
                MASTER="yes"
                ;;
        -S|--slave)
                SLAVE="yes"
                ;;
        -A|--alldb)
                ALLDB="yes"
                ;;
        -s|--status)
                STATUS="yes"
                ;;
        -t|--tableview)
                TABLEVIEW="yes"
                ;;
        -h|--help)
                usage
                ;;
        --)
                shift
                break
                ;;
        esac
shift
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ygqygq2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值