First of all, please use [code][/code] tags around your code to preserve formatting (and start using tab indenting in your code if that's how it originally looked
). It took me a couple of minutes of reformatting to figure out what your script is supposed to do.
Here's what it looks like after I cleaned it up a bit. Note that I substituted $() for `` backticks,
as recommended, and "elif" for "else if". I haven't changed anything else yet.
Code:
first_time="yes"
altcount=0
save_telephone_number=""
havealtnumber="no"
while read line; do
attr=$(echo $line | cut -f1 -d":")
value=$(echo $line | cut -f2 -d":" | sed "s/^ //")
case $attr in
telephonenumber)
if [ "$first_time" = "yes" ]; then
save_telephone_number=$value
first_time="no"
elif [ "$havealtnumber" = "yes" ]; then
echo "$save_telephone_number,$umcosdn,howdoiprintarray"
havealtnumber="no"
save_telephone_number=$value
fi
;;
umcosdn)
umcosdn=$value
;;
umsecondaryphonenumber)
altarray[$altcount]=$value
((altcount++))
havealtnumber="yes"
;;
*) ;;
esac
done < /tmp/primary-alt.txt
The main problem I see is that you can't print the data you're collecting while inside the loop, because that only operates on one line at a time. What you need to do is redesign the thing so that the loop collects all the data you want to print, then print everything at once after you exit it.
Of course, the printing of the array itself is fairly straightforward.
Code:
echo ${altarray[@]}| tr " " ","