Find the answer to your Linux question:
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 ...
  1. #1
    Just 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

    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
    I am using bash. I had inserted echo statement for tim but it comes out to be empty instead of 0.001.
    sed -e "s/^ //g" removes blank spaces I think.

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    IFS 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 bash
    cheers, drl
    Welcome - 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 )

  3. #3
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by Popup123 View Post
    sed -e "s/^ //g" removes blank spaces I think.
    No, it removes a single space from the beginning of a line. The 'g' is redundant since there is only one beginning of line (matched by the ^). To remove all leading whitespace use
    Code:
    sed 's/^ *//'

  4. #4
    Just 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.

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by Popup123 View Post
    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.
    The read command reads 2 variables which are seperated by a space or tab in the found file.
    Here's where the file is opened that's found in the for loop with the ls command:

    Code:
    done<$file
    Regards

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...