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:
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