1

Below is the shell script using getopts

#!/bin/bash

while getopts "USER:PWD:JOBID:PROJECTID:" flag
do
         case "${flag}" in
                USER) TEST_USER=${OPTARG};;
                PWD) TEST_PWD=${OPTARG};;
                JOBID) TEST_JOBID=${OPTARG};;
                PROJECTID) TEST_PROJECTID=${OPTARG};;
         esac
       
done
echo "USER: $TEST_USER";
echo "PWD: $TEST_PWD";
echo "JOBID: $TEST_JOBID";                  
echo "PROJECTID: $TEST_PROJECTID"; 

Continue of script i have not put here and if above commands work fine then my issue solve 

And Here what im running output in terminal, Which one is a correct way to get output command

./getopts.sh -USER=devops@gmail.com -PWD=xxxxxx -JOBID=8a809e2496 -PROJECTID=80e2ea54b231f
OR
./getopts.sh USER=devops@gmail.com PWD=xxxxxx JOBID=8a809e2496 PROJECTID=80e2ea54b231f

After running above command im getting empty response or string USER: PWD: JOBID: PROJECTID:

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Mohd Rashid
  • 101
  • 2
  • 9
  • As an aside, [you want to avoid upper case for your private variables.](https://stackoverflow.com/questions/673055/correct-bash-and-shell-script-variable-capitalization) – tripleee Nov 08 '21 at 13:51

1 Answers1

1

You may want to read the getopts manual page


    while getopts "U:P:J:I:" flag
    do
             case "${flag}" in
                    U) TEST_USER=${OPTARG};;
                    P) TEST_PWD=${OPTARG};;
                    J) TEST_JOBID=${OPTARG};;
                    I) TEST_PROJECTID=${OPTARG};;
             esac
    done
    echo "USER: $TEST_USER";
    echo "PWD: $TEST_PWD";
    echo "JOBID: $TEST_JOBID";
    echo "PROJECTID: $TEST_PROJECTID";
./getopts.sh -U devops@gmail.com -P xxxxxx -J 8a809e2496 -I 80e2ea54b231f
USER: devops@gmail.com
PWD: xxxxxx
JOBID: 8a809e2496
PROJECTID: 80e2ea54b231f

With getopt, it's more complicated, but it allows long options and "=".

    #!/bin/bash
    TEMP=$(getopt -n "$0" -a -l "user:,password:,jobid:,projectid:" -- -- "$@")

    [ $? -eq 0 ] || exit

    eval set --  "$TEMP"

    while [ $# -gt 0 ]
    do
             case "$1" in
                    --user) TEST_USER="$2"; shift;;
                    --password) TEST_PWD="$2"; shift;;
                    --jobid) TEST_JOBID="$2"; shift;;
                    --projectid) TEST_PROJECTID="$2"; shift;;
                    --) shift;;
             esac
             shift;
    done
    echo "USER: $TEST_USER";
    echo "PWD: $TEST_PWD";
    echo "JOBID: $TEST_JOBID";
    echo "PROJECTID: $TEST_PROJECTID";

Some tests:

$ ./test.sh -user=jules -password=kabas -jobid 5555 -projectid 999 -c
./test.sh: unrecognized option '-c'

$ ./test.sh -user=jules -password=kabas -jobid 5555 -projectid 999   
USER: jules
PWD: kabas
JOBID: 5555
PROJECTID: 999
Gerard H. Pille
  • 2,528
  • 1
  • 13
  • 17
  • i tried with this command its not working ./getopts.sh -U=devops@gmail.com -P=xxxxxx -J=8a809e2496 -I=80e2ea54b231f – Mohd Rashid Nov 05 '21 at 14:06
  • I've never seen the "=" being used on unix/linux commandline parameters. – Gerard H. Pille Nov 05 '21 at 14:18
  • my client want to have like this it is possible while getopts "user:password:jobId:projectId:" flag do case "${flag}" in user) TEST_USER=${OPTARG};; password) TEST_PWD=${OPTARG};; jobId) TEST_JOBID=${OPTARG};; projectId) TEST_PROJECTID=${OPTARG};; ./getopts.sh -user devops@gmail.com -password xxxxxx -jobId 8a809e2496 -projectId 80e2ea54b231f when i try im not getting any response or empty strings return please do let me know can i do it – Mohd Rashid Nov 08 '21 at 12:17
  • There's little that's impossible in IT. All depends on how much one wants to invest in the solution. See my edit. – Gerard H. Pille Nov 08 '21 at 13:37
  • [Why is testing “$?” to see if a command succeeded or not, an anti-pattern?](https://stackoverflow.com/questions/36313216/why-is-testing-to-see-if-a-command-succeeded-or-not-an-anti-pattern) – tripleee Nov 08 '21 at 13:51
  • They pick nits, don't they? – Gerard H. Pille Nov 08 '21 at 15:50
  • Thanks it solve my issue but i need script explanation why we are using TEMP=$(getopt -n "$0" -a -l -- -- "$@") [ $? -eq 0 ] || exit eval set -- "$TEMP" while [ $# -gt 0 ] case "$1" in esac shift; ...........any links to read – Mohd Rashid Nov 09 '21 at 10:39
  • I first read "man getopt" and that pointed me to /usr/share/doc/util-linux/examples/getopt-parse.*. Then some trial and error led me to the above script. – Gerard H. Pille Nov 09 '21 at 11:47
  • wow thanks @GerardH.Pille your are great i would like to connect with u to learn more about script or any other technologies which u have more experience in it – Mohd Rashid Nov 09 '21 at 13:25
  • That may be so, but I'm not a good teacher - much too impatient. But any time you have a problem, post it here and give me a heads up. – Gerard H. Pille Nov 09 '21 at 14:24