i am executing shell script called ./myscript.sh with 2 options like below
./myscript.sh -d /root/ -n "dhoni" "kohli"
first option is -d and value is /root/
second option is -n and values are dhoni and kohli for this in the current example
But each time while executing this script number of names passed to this script for -names option may vary
the code i have written for this is
EMPNAMES=("$@")
while getopts "d:n:" arg; do
case "$arg" in
d) PATH="$OPTARG"
;;
n) EMPNAMES="$OPTARG"
;;
for arg in "${EMPNAMES[@]}"; do
echo "$arg"
done
it should print
dhoni
kohli
But it is printing
dhoni
/root/
-names
dhoni
kohli
sh, orbash? Please tag only for the one you're actually using to run your script: They're two very different shells, and the syntax you're using isn't available at all with/bin/sh.PATHwill stop your script from being able to run any external programs, because it's changing the PATH variable used to execute other software; if you named inpathinstead, you'd avoid that bug).