Getopts help

Hi All,

I am writing a script to pass the getopts argument to the function which I have. But it as soon as I execute the script, the argument is taking it as blank. I tried using multiple way to check but its not working.

Can someone please let me know what wrong in this code.

function1()
{
processing details 
wget -i $base_dir/$APPNAME.txt --directory-prefix=downloads --http-user=$SVN_USR --http-password=$SVN_PASS -o $base_dir/$APPNAME.log
}

while getopts ":ackpsuv" OPT; do
        case "$OPT" in
                a) APPNAME=$OPTARG;;
                c)
                echo ;;
                k) KEYSTOREPSSWD="$OPTARG;;
                p) SVN_PASS="$OPTARG;;
                s)
                uploadSVN
                ;;
                u) SVN_USR=$OPTARG;;
                v) KEYSTORE=$OPTARG;;
        *) echo "Internal error!" ; exit 1 ;;
    esac
done

if [ -z "${APPNAME}" ] && [ -z "${SVN_USR}" ] && [ -z "${SVN_PASS}" ] 
then

function1 ${APPNAME} ${SVN_USR} ${SVN_PASS}

else
        echo "Check usage"
fi

As you can see, none of the variables are being taken by the function.

execution output

$ksh -x test.ksh -a "appname" -u "user" -p "pass" 
+ getopts :ackpsuv OPT
+ APPNAME=''
+ getopts :ackpsuv OPT
+ [ -z '' ]
+ [ -z '' ]
+ [ -z '' ]
+ [ -z '' ]
+ echo 'Downloading the required perevious year files from SVN Repo..'
+ wget -i ./.txt --directory-prefix=downloads --http-user= --http-password= -o ./.log
+ grep 'Authorization failed.' ./.log
+ wc -l
+ error=0
+ [[ 0 -ne 0 ]]
+ tail -1 ./.log
+ cut '-d ' -f2
+ completed=URLs
+ [[ URLs -ne 6 ]]
+ echo 'All the required files not downloaded! Please check.'
All the required files not downloaded! Please check.
+ exit 3

Please can someone help me in this case?

Regards
Sid

You seem to have at least four errors in your script:

  • You have a colon preceding ALL option chars. man ksh :
  • Two of your $OPTARG expansions have leading unpaired double quotes making the "p" case part of a string.
  • Your -z test is wrong - don't test for zero but for non-zero.
  • "processing" is NOT a valid command.
1 Like

Thank you very much for your quick response on this.

Apologies for the earlier code, that was just a typo while I was editing and I have accidentally deleted the double quotes.

I am passing (-a "appname") as a mandatory argument when I am executing and the rest are optional. But when I am assigning each OPTARG to the respective variable then those variables become null when I am passing the variable values in the function arguments.

I am removed the -z option and tried with the following code and now I got blank.

Is it something I am doing it wrongly? Please kindly advise. Thanks

if [ "${APPNAME}" != "" ] && [ "${SVN_USR}" != "" ] && [ "${SVN_PASS}" != "" ] && [ "${KEYSTOREPSSWD}" != "" ]
then
downloadfiles ${APPNAME} ${SVN_USR} ${SVN_PASS}
else
        echo "Check usage"
fi

$ ksh -x test.ksh -a "appname" -u "user" -p "pass" 
+ APPNAME=''
+ SVN_USR=''
+ SVN_PASS=''
+ KEYSTOREPSSWD=''
+ getopts :ackpsuv OPT
+ APPNAME=''
+ getopts :ackpsuv OPT
+ [ '' != '' ]
+ echo 'Check usage'
Check usage
+ [ '' != '' ]
+ echo 'Check usage'
Check usage
+ exit 1

The option character should be FOLLOWED by a colon, NOT preceded!

1 Like

Thank you RudiC,

It worked fine after I shift the colon. Much appreciated.

Regards:b:
Sid