Parsing Comma separated Arguments

Hi unix guru's

I want to execute a shell script like ksh printdetails.ksh Andy,Bob,Daisy,Johnson

like passing all the four names in the as the arguments and these arguments are varies between 1 to 10.
How to pass these names to the shell script variable.
and also i want to know the count of arguments passed to script.

Thanks in advance.

Here's one way.

#!/bin/ksh

VARS=$1

set -A A_NAME $( echo "$VARS" | sed 's/,/ /g' )

NUM_VARS=${#A_NAME[@]}

while [ -n "${A_NAME}" ]
do
   echo "name = ${A_NAME}"
   (( i = i + 1 ))
done

echo "Total Names:  ${NUM_VARS}"
./names.ksh mike,bill,jim,john

name = mike
name = bill
name = jim
name = john

Total Names:  4

And here is another way:

#!/bin/ksh
oIFS="$IFS"; IFS=, ; set -- $1 ; IFS="$oIFS"
for i in "$@"; do
  echo name = $i
done
echo Total Names: $#
$>./test Cooper,"Billy Bob",Trevor,Smithers
name = Cooper
name = Billy Bob
name = Trevor
name = Smithers
Total Names: 4
1 Like

Hi Scrutinizer

is there any way that we can do this job workout $1
because when i use $1 it was defaultly assuming my environment variable

Hi Reddy, I do not understand what you mean exactly, could you be specific and/or give an example?