1. 程式人生 > >shell腳本接參數優美用法

shell腳本接參數優美用法

shell getopt getopts

shell腳本接參數優美用法


學習 bash



  • shell腳本接參數優美用法

    • 2.1 getopt命令的用法

    • 2.2 示例

    • 1. 示例腳本

    • 2. 轉載閱讀


1. 示例腳本

#!/usr/bin/env bash
#Bash腳本接參數優美用法
#Create on 2016-11-18
[email protected]: 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 [email protected] 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

運行結果如下:

[[email protected] 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


本文出自 “ygqygq2” 博客,請務必保留此出處http://ygqygq2.blog.51cto.com/1009869/1931050

shell腳本接參數優美用法