Home > database >  How to pass Named Parameter in Shell Script
How to pass Named Parameter in Shell Script

Time:11-05

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 [email protected] -PWD=xxxxxx -JOBID=8a809e2496 -PROJECTID=80e2ea54b231f
OR
./getopts.sh [email protected] PWD=xxxxxx JOBID=8a809e2496 PROJECTID=80e2ea54b231f

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

CodePudding user response:

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 [email protected] -P xxxxxx -J 8a809e2496 -I 80e2ea54b231f
USER: [email protected]
PWD: xxxxxx
JOBID: 8a809e2496
PROJECTID: 80e2ea54b231f
  • Related