Counting items in variables, How come this works, However


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Counting items in variables, How come this works, However
# 1  
Old 08-07-2009
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
Code:
echo "Item count: ${ITEMCOUNT}"

prints 0 (Zero)!

And is there a solution.

Code:
#!/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 : )_~
# 2  
Old 08-07-2009
It's because ITEMCOUNT is only defined in the while loop. Try this:

Code:
#!/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}"

# 3  
Old 08-07-2009
Maybe I'm not reading this correctly but you could get the number of items in the array this way:

Code:
echo ${#ITEMLIST[@]}

# 4  
Old 08-07-2009
Quote:
Originally Posted by peterro
It's because ITEMCOUNT is only defined in the while loop. Try this:

Code:
#!/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}"

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

Of course the first thing I did was reformat it like...
Code:
  done << EOF
  echo "${1}"
  EOF

NOTICE the spaces to the left of...
Code:
  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...
Code:
  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 ----------

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

Code:
echo ${#ITEMLIST[@]}

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!
Code:
$#

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:
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?
Code:
  done << EOF
${1}
EOF

Removal of echo and double quotes.

Thanks all!

-Enjoy
fh : )_~
# 5  
Old 08-08-2009
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:

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] Counting The Number of Lines Between Values with Multiple Variables

Hey everyone, I have a bunch of lines with values in field 4 that I am interested in. If these values are between 1 and 3 I want it to count all these values to all be counted together and then have the computer print out LOW and the number of lines with those values in between 1 and 3,... (2 Replies)
Discussion started by: VagabondGold
2 Replies

2. Shell Programming and Scripting

awk, help me - counting items and listing them

This is my first ever post... please help! :o I have two columns....here is part of the file... 12, 46798 6692, 46799 5710, ... (3 Replies)
Discussion started by: pelhabuan
3 Replies

3. Shell Programming and Scripting

Moving items to a new line

Hi, Let's I have the following strings (md5): 07177edf8261d28c6a003e583fcbe38c 0717c0037b3a20fc0f0998e673f228d5 0717d611a5d24374628b98e17fd00977,0717d611a5d24374628b98e17fd00977 07189a18afdae558bb5aadfe602e4a91 0719e97d481c239667f38a3e166bed74 071af3225fe50a1fdbb42c43aac313cc... (4 Replies)
Discussion started by: talfiq
4 Replies

4. Shell Programming and Scripting

Counting the number of readable, writable, and executable items in a directory

Hello, I'm writing a script in sh in which the first command line argument is a directory. from that, i'm suppose to count the number of readable, writable, and executable items in the directory. I know using $1 represents the directory, and ls would display all the items in the directory, and that... (4 Replies)
Discussion started by: kratos22
4 Replies

5. UNIX for Dummies Questions & Answers

queue items...

Hi everyone. I have a lot of programs i want to run on some data files, they need to be done sequentially. Often the output from one program is the input for the next. e.g $ progA data1 > data1.A $ progB data1.A > data1.AB $ progC data1.AB > data1.ABC repeat on data2, 3, 4, 5, 6 etc ... (4 Replies)
Discussion started by: Jay-pea
4 Replies

6. Shell Programming and Scripting

Help needed regarding first 3 items in the list

Hi, I've a list in the following format: Empdept filedetails buildingNo Area AAA 444 2 juy AAA 544 2 kui AAA 567 4 poi AAA 734 5 oiu AAA 444 ... (2 Replies)
Discussion started by: skpvalvekar
2 Replies

7. UNIX for Dummies Questions & Answers

Searching multiple items

Hi, I'm a complete newbie so bear with me. I have a directory (and sub-dirs) full of .doc, .xls files. What I'm trying to do is do a single search within the files (i.e. within each .doc etc) for occurrences of multiple items e.g. apples, pears, grapes, bananas. Basically I'd provide a... (4 Replies)
Discussion started by: kainfs
4 Replies

8. UNIX for Dummies Questions & Answers

Number of items in a directory

This should be so simple.. I have folder with about 27 subfolders in it, each folder has a number of fonts in it.. how do I get the total number of fonts for all subfolders? (2 Replies)
Discussion started by: glev2005
2 Replies

9. Shell Programming and Scripting

awk between items including items

OS=HP-UX ksh The following works, except I want to include the <start> and <end> in the output. awk -F '<start>' 'BEGIN{RS="<end>"; OFS="\n"; ORS=""} {print $2} somefile.log' The following work in bash but not in ksh sed -n '/^<start>/,/^<end>/{/LABEL$/!p}' somefile.log (4 Replies)
Discussion started by: Ikon
4 Replies

10. Shell Programming and Scripting

Comparing items in 2 files

Hi, I have 2 files with contents in them and I need to compare each item in each file. File1: item4 item5 File2: item2 item3 item5 item6 The items names can be of different lengths. If the items in the File1 are not in File2, delete the missing item in File1. The resulting... (12 Replies)
Discussion started by: ReV
12 Replies
Login or Register to Ask a Question