Results 1 to 6 of 6
Can somebody explain how $IFS is used?
I have to modify following piece of code acccording to my needs.
But I am unable to understand it. I have to read ...
- 08-16-2007 #1Just Joined!
- Join Date
- Aug 2007
- Posts
- 2
problem in shell script
Can somebody explain how $IFS is used?
I have to modify following piece of code acccording to my needs.
But I am unable to understand it. I have to read a log file written as:-
0.001
0.002
I am using bash. I had inserted echo statement for tim but it comes out to be empty instead of 0.001.Code:tot=0.0 max=0 npts=0 totpts=0 for file in `ls -1 logs/*.log`; do oldifs=$IFS IFS=: while read str tim; do tim=`echo $tim | sed -e "s/^ //g"` tot=`echo "$tot + $tim" | bc -l` npts=`expr $npts + 1` totpts=`expr $totpts + 100` break done<$file IFS=$oldifs done
sed -e "s/^ //g" removes blank spaces I think.
- 08-16-2007 #2Linux Engineer
- Join Date
- Apr 2006
- Location
- Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
- Posts
- 1,117
Hi.
cheers, drlIFS The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read
builtin command. The default value is ``<space><tab><new-
line>''.
-- man bashWelcome - get the most out of the forum by reading forum basics and guidelines: click here.
90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
We look forward to helping you with the challenge of the other 10%.
( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )
- 08-16-2007 #3Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
- 08-17-2007 #4Just Joined!
- Join Date
- Aug 2007
- Posts
- 2
Hi,
Thanks. How does this while read str works? I think in this code file has not been opened because ls -1 .. in for loop will only give file name.
- 08-17-2007 #5Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
- 08-17-2007 #6
It might help us solve your problem if we knew what you were trying to do. Can you explain what you want this script to end up doing? Based on what I understand of your problem, for instance, you have no need to modify $IFS at all...
DISTRO=Arch
Registered Linux User #388732


Reply With Quote
