Counting items in variables, How come this works, However

Hi All,

To start with, I have been reading this site for years, Unfortunately I do not consider myself versed well enough with scripts to provide useful help to others. The Blind cannot lead the Blind!

Many of you have provided me with brain food and solutions over the years without even knowing it and for that... I thank you, for doing what you do!

Now my question is ...
WHY does the following work, It Prepends a valid ITEMCOUNT to each lineitem, yet

echo "Item count: ${ITEMCOUNT}"

prints 0 (Zero)!

And is there a solution.

#!/bin/sh

#set -x

ITEMCOUNT=0

CountItemsInList()
{
  #ls -dr /usr/home/* |
  echo "${1}" |
  while read ITEM
  do
    ITEMCOUNT=$(expr ${ITEMCOUNT} + 1)
    echo "${ITEMCOUNT} : ${ITEM}"
  done
  echo "Item count: ${ITEMCOUNT}"
}

ITEMLIST=$(ls -dr /usr/home/*)

CountItemsInList "${ITEMLIST}"

-Enjoy
fh : )_~

It's because ITEMCOUNT is only defined in the while loop. Try this:

#!/bin/sh

ITEMCOUNT=0

CountItemsInList()
{
  while read ITEM
  do
    ITEMCOUNT=$(expr ${ITEMCOUNT} + 1)
    echo "${ITEMCOUNT} : ${ITEM}"
  done << EOF
echo "${1}"
EOF
  echo "Item count: ${ITEMCOUNT}"
}

ITEMLIST=$(ls -dr /home/*)

CountItemsInList "${ITEMLIST}"

Maybe I'm not reading this correctly but you could get the number of items in the array this way:

echo ${#ITEMLIST[@]}

Awesome ... Beautiful ... Cool ... etc...

Of course the first thing I did was reformat it like...

  done << EOF
  echo "${1}"
  EOF

NOTICE the spaces to the left of...

  echo "${1}"
  EOF

after I put it back to YOUR EXACT formating it worked as expected.

However I'm not sure I understand it!
This is what I get: "eofITEMLISTeof" is redirected to read.
Is that correct??

As for it only being defined in the loop, is beyond me, I see it as it was defined above the function.

The only difference I see is how the read is fed...

Also can it be re-formated any other way? like maybe...

  done << EOF; echo "${1}"; EOF

I really don't want it hanging around on the left margin, makes for messy reading.

Thanks Mucho peterro

-Enjoy
fh : )_~

---------- Post updated at 06:18 PM ---------- Previous update was at 06:08 PM ----------

I need the count of elements (positional parameters), the above is the total byte count of ITEMLIST.

According to FBSD7.1R Man page the following expands to the number of positional parameters!

$#

However I have been unable to get it to output anything other than 1.
That was the very first thing I tried.

Speaking of total byte count... whats MAX for bytes in a variable as such.

Thanks for your input!

-Enjoy
fh : )_~

---------- Post updated at 07:08 PM ---------- Previous update was at 06:18 PM ----------

peterro,

I just found an issue with code:

  done << EOF
echo "${1}"
EOF

It includes the echo and double quote at the head of the first ITEM and a double quote at the tail of the last ITEM.

The following fix seems to work?

  done << EOF
${1}
EOF

Removal of echo and double quotes.

Thanks all!

-Enjoy
fh : )_~

Not sure about the quote issue, but I do understand the messy formatting. You could put a dash "-" in the start of the here document like this:

done <<-EOF

This allows for better formatting although I think the final EOF still needs to be at the beginning of the line. I suspect you're using since it is behaving like this. More info can be found in Chapter 10 of Expert Shell Scripting (Apress).

Glad it worked.