Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2003
    Location
    East Coast of South Africa
    Posts
    10

    Question Unanswered: Shell scripting when verable contains a space

    Scenario: I have a file with each line containg filenames. i.e. '{dirname}/Oct Reports'
    Problem: The shell reads each line as two verables due to the space.
    Script:
    #!/bin/sh
    for i in `cat file`
    do
    echo $i
    done
    Output:
    '{dirname}/Oct
    Reports'
    Tried enclosing filename in single quotes, double quotes, backslash before space, two backslash before space.
    please advise if you know where I am going wrong..

    Surfon
    Last edited by EZEE!; 10-09-03 at 09:19.

  2. #2
    Join Date
    Oct 2003
    Location
    East Coast of South Africa
    Posts
    10

    Lightbulb

    Answered my own question by searching this site further and applying a similar task to this one.
    Code that worked for me eventually:

    cat outfile | while read l
    do
    echi $l
    done

    Any other ideas welcome though.

  3. #3
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    You can overcome the space issue by setting IFS to a value other than whitespace.

    e.g.

    IFS=#; for i in $(cat outfile); do...

    Also, in your example, you needn't use 'cat'.

    while read l
    do
    echi $l
    done < outfile

    HTH

  4. #4
    Join Date
    Oct 2003
    Location
    Germany
    Posts
    138

    @ all in this posting

    Thanks for the solution, I have had the same problem last week.
    But I don´t find the right words for asking here.

    Greetings from Germany
    Peter F.

  5. #5
    Join Date
    Oct 2003
    Location
    East Coast of South Africa
    Posts
    10
    Damian Ibbotson, thanks, that is why you are a guru.
    For us wannabies..Can you expand a litlle on what IFS=# is declaring...

    Muchos Grazias

  6. #6
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Originally posted by EZEE!
    Damian Ibbotson, thanks, that is why you are a guru.
    For us wannabies..Can you expand a litlle on what IFS=# is declaring...

    Muchos Grazias
    IFS (Internal Field Seperator) is an environment variable which is normally a space, a tab and a newline. The shell uses individual characters found in the IFS variable to separate fields (surprisingly enough!). IFS=# is simply setting the value of the variable to a # character.

    You can see this in action if you try the following:

    IFS=#; set Hello World; echo "$*"

    If you only wanted to alter IFS for the cat command, you would do...

    for i in $(IFS=# cat outfile); do

    PS - I'm really not a 'guru'. I think you can interpret that as meaning 'prolific poster' and not much else!
    Last edited by Damian Ibbotson; 10-11-03 at 06:40.

  7. #7
    Join Date
    Oct 2003
    Location
    East Coast of South Africa
    Posts
    10

    Further problems - feedback

    Hi, I thought I let you know of further hassles I had.
    The echo works fine but if I try to tar copy the file to another volume I have endless problems. and more errors when deleting origional. Here is what didnt work... I tried a combo of them.
    #for i in $(IFS=# cat $out)
    #IFS=#; for i in $(cat $out)
    #IFS=#; for i in $( -name "*.xls" -printf "\"%p\" ");
    #for i in $(IFS=# find /usr4/data -mtime +2457 -name "*.xls" -printf "%p ");
    #(cd / && tar cf - $(IFS=# find /usr4/data -mtime +2457 -name "*.xls" -printf "\"%p\" "))|(cd $todir; tar xfBp -)
    #do
    # echo $i
    #get dirname
    # copy data with tar
    #(tar cf - "$i")| (cd $todir; tar xfBp -)
    #(IFS=# && tar cf - "$i")| (cd $todir; tar xfBp -)
    #remove origional
    #IFS=#; rm "$i"
    #done

    BUT THIS IS WHAT WORKED IN THE END....

    #!/bin/bash
    todir='/usr4/history/working'
    # list of files used with `find /usr4/data -mtime +2457 `
    out= "outdile1997"
    while read bla
    do
    echo $bla
    (tar cf - "$bla")| (cd $todir; tar xfBp -)
    #IFS=# ;rm $bla
    done < $out

    It Simply amases me how verious loops work in shells and I take my hat off to anyone who truly understands them. for, foreach, while, case ....and,and,and...

    I must say a thankyou to Damian Ibbotson. Cheers

    Stephan

Posting Permissions

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