Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Count & catch arguments from parameter file 2

Status
Not open for further replies.

tommytf

Programmer
Apr 30, 2004
1
FR
Hi,
I am new and i am looking for counting number of arguments from a parameter file and for exporting them into dedicated variables.

parameter file :
[SOURCE]
VARSRC=login1/pwd1,login2/pwd2,login3/pwd3
[TARGET]
VARTGT=login1/pwd1,login2/pwd2,login3/pwd3,login4/pwd4

Q1 : How can I know how many arg I have in source and store it into a variable (count_src=3) and in target (count_tgt=4) ?
Q2 : How can I export each on them into dedicated variables (login1 to var1, pwd2 to var2, etc.) ?

Context :
- The number of couple login/pwd can change in SRC and TGT
- ksh on AIX

Many Thanks in advance.

TTF.
 
count_src=$(echo $VARSRC | sed 's/[^,]//g' | wc -c)
count_tgt=$(echo $VARTGT | sed 's/[^,]//g' | wc -c)

or

count_src=$(echo $VARSRC | awk -F"," 'END{print NF}')
count_tgt=$(echo "VARTGT | awk -F"," 'END{print NF}')

and probably lots of other ways to do this.



Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
You can try something like this:
eval $(awk -F, 'BEGIN{print "count_src=0;count_tgt=0"}
/^VARSRC=/{
printf "count_src=%d\n",NF
sub(/^VARSRC=/,"",$1)
for(i=1;i<=NF;++i)if(split($i,a,"/")==2)
printf "export srclog%d=\"%s\" srcpwd%i=\"%s\"\n",i,a[1],i,a[2]
}
/^VARTGT=/{
printf "count_tgt=%d\n",NF
sub(/^VARTGT=/,"",$1)
for(i=1;i<=NF;++i)if(split($i,a,"/")==2)
printf "export tgtlog%d=\"%s\" tgtpwd%i=\"%s\"\n",i,a[1],i,a[2]
}' /path/to/parameterfile)
echo "count_src=$count_src"
while [ $count_src -gt 0 ]; do
eval echo "src$count_src:\$srclog$count_src,\$srcpwd$count_src"
((count_src-=1))
done
echo "count_tgt=$count_tgt"
while [ $count_tgt -gt 0 ]; do
eval echo "tgt$count_tgt:\$tgtlog$count_tgt,\$tgtpwd$count_tgt"
((count_tgt-=1))
done

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top