shell之getopt的用法


1,在編寫帶有多參數的shell腳本時,需要用到getopt,語法比較多,先留個例子,以備后續查閱

 

filepath=$(cd `dirname $0`; pwd)

show_usage="args: [-s , -e , -n , -c]\
                                  [--sdate=, --edate=, --numprocs=, --cfile=]"

if [[ -z $@ ]];then
echo $show_usage
exit 0
fi

GETOPT_ARGS=`getopt -o s:e:n:c: -al sdate:,edate:,numprocs:,cfile: -- "$@"`

#echo "$GETOPT_ARGS"
eval set -- "$GETOPT_ARGS"

while [ -n "$1" ]
do
        case "$1" in
                -s|--sdate) sdate=$2; shift 2;;
                -e|--edate) edate=$2; shift 2;;
                -n|--numprocs) numprocs=$2; shift 2;;
                -c|--cfile) cfile=$2; shift 2;;
                --) break ;;
                *) echo $1,$2,$show_usage; break ;;
        esac
done

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM