Using getopts to process long and short command line options

后端 未结 30 1450
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 22:52

I wish to have long and short forms of command line options invoked using my shell script.

I know that getopts can be used, but like in Perl, I have not

30条回答
  •  有刺的猬
    2020-11-21 23:04

    Maybe it's simpler to use ksh, just for the getopts part, if need long command line options, as it can be easier done there.

    # Working Getopts Long => KSH
    
    #! /bin/ksh
    # Getopts Long
    USAGE="s(showconfig)"
    USAGE+="c:(createdb)"
    USAGE+="l:(createlistener)"
    USAGE+="g:(generatescripts)"
    USAGE+="r:(removedb)"
    USAGE+="x:(removelistener)"
    USAGE+="t:(createtemplate)"
    USAGE+="h(help)"
    
    while getopts "$USAGE" optchar ; do
        case $optchar in
        s)  echo "Displaying Configuration" ;;
            c)  echo "Creating Database $OPTARG" ;;
        l)  echo "Creating Listener LISTENER_$OPTARG" ;;
        g)  echo "Generating Scripts for Database $OPTARG" ;;
        r)  echo "Removing Database $OPTARG" ;;
        x)  echo "Removing Listener LISTENER_$OPTARG" ;;
        t)  echo "Creating Database Template" ;;
        h)  echo "Help" ;;
        esac
    done
    

提交回复
热议问题