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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

redirecting stdout to variable 1

Status
Not open for further replies.

galorin

MIS
Nov 22, 2007
154
GB
I am in the process of writing a script to download server logs, use a perl script to parse it for information, do a bunch of processing, and create a report. As a part of this report, I need to do a simple line count of a csv file, and ideally store this in a variable withing the script.

I don't understand redirection, and I don't even know if what I am trying to do is possible with Bash. Anyhow, here is the redirect portion

Code:
exec 6>&1 #redirect stdout
exec > $COUNT

for file in cop.csv; do
 grep EWP-COP $file | wc -l
done

exec 1>&6 6>&-

 
Nevermind, I can't do it, so I'm redirecting to a text file.

Now I'm having some confusion getting the information back out of the text file and into an array. I'm trying to figure out the array workings in Bash currently.
 
For future reference, a construct like:
COUNT=`grep EWP-COP cop.csv | wc -l`

would do all that redirecting and 'for loop' in one command. (Note those 'back-ticks' are not 'apostrophes')
Use the 'for loop' construct for performing the same thing on a list of items.

Arrays. The shells I have used (sh, ksh, bash) can only cope with one-dimensional arrays. See the man pages for details (eg upper limit for the index).

To read a text file, read it line by line in a 'while read' 'do' loop.

Please feel free to post further questions.


I hope that helps.

Mike
 
Fantastic! No more redirects needed and nomore file I/O.. for the moment. As I still don't understand arrays, could you please explain why this following code doesn't work?

Code:
i=0
cat count | while read line
 do
 LINES[${i}]=$line
 echo ${LINES[${i}]}
 i=$(($i + 1 ))
done

echo ${LINES[0]}
echo ${LINES[2]}
 
Is 'count' the name of a file (in the current working directory)?

In bash LINES is an environmental variable. In the shells I have used, all environmental variables are all uppercase. Therefore I would recommend not using all uppercase variable names. If you really want to use all uppercase variable names, check that the variable doesn't already exist with:
echo ${<variable>} # eg: echo ${LINES}
or
set # to see all of them

This looks like a 'scoping' issue. For some reason sh & bash in some loop constructs create their own 'local' variables, which get deleted on completion of the loop. Your script works fine in Korn Shell, assuming 'count' is a file.


I hope that helps.

Mike
 
Code:
#!/bin/ksh

i=0
while read line
 do
 myLINES[${i}]=$line
 echo ${myLINES[${i}]}
 i=$(($i + 1 ))
done < count

echo ${myLINES[0]}
echo ${myLINES[2]}

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Ahhh that makes sense now. didn't know lines was a reserved word. thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top