[prev in list] [next in list] [prev in thread] [next in thread] 

List:       hpux-admin
Subject:    [HPADM] SUMMARY: Filename Containing Spaces
From:       "Godbout, Marc" <mgodbout () zygo ! com>
Date:       2002-02-27 21:04:40
[Download RAW message or body]

THANKS TO ALL RESPONDENTS:
Gary Butler, John Curtis, Arthur Dent, Edward, Stig Eriksson, Bill Hassell,
Eef Hartman, Ralf Hildebrandt, Steve Illgen, Thierry Itty, Stéphane
Lachapelle, Sachin Malesha, Olivier Masse, Cyrille Maucci, Hubert Partl,
Ryan, Christopher Schroen, James Sohr, Stan Stewart, Lutz Summerfeld, Bill
Thompson, Bob Vance, Ram Vasudev, Justin Willoughby.


ORIGINAL QUESTION:
------------------------------------------------------------
Contents of script:
#!/sbin/sh
   for i in `cat /tmp/list`
   do
      print $i
   done

Contents of input file /tmp/list:
My File.txt
 
The above script produces the following output because the filename
contained in the input file includes a space:
 
My not found
File.txt not found
 
I attempted to force the filename to be treated as a single file by wrapping
it in quotes ("My File.txt"), but the output did not change.


SUMMARY:
------------------------------------------------------------
The three most popular solutions were:
1. Use an internal field separator (IFS).
2. Use a read instead of for construct.
3. Wrap quotes around the cat command and $i.

I used an IFS because that was the first solution I received and tested.
There may well be valid reasons why one of the other solutions would be
better. The modified script now reads:

#!/sbin/sh
IFS=""
   for i in `cat /tmp/list`
   do
      print $i
   done
unset IFS


RESPONSES:
------------------------------------------------------------
Arthur Dent, Edward, Stig Eriksson, Thierry Itty, Steve Illgen, Stéphane
Lachapelle, and Justin Willoughby all suggested variations of:

while read i; do
  print "$i"
done < /tmp/list

Thierry Itty offered the following additional advice: 

The for statement expands the input string as blank delimited words thus the
"i" variable once gets "My" then "File.txt". What you have to do is to use a
read statement instead of a for. You don't want to process each token of the
list because of the spaces in your filename.
------------------------------------------------------------
Eef Hartman:
I think (haven't tested it) that
   for i in "`cat /tmp/list`"
will maybe work. It does with "ls" instead of the "cat". The problem is that
"cat" in a for will put all the lines AFTER one other, separated by spaces,
so the difference between "two words on one line" and "two lines" isn't seen
anymore by "for". If this doesn't work, you will have to edit the file and
put quotes AROUND the line(s) with spaces!

But a lot of other commands will not accept any filenames with spaces, so
normally you should stay away of filenames like these(for instance "find"
will find them but "xargs" will not execute ANY commands on them).

Example of a command we had to "fix":
for dir in /tmp /var/tmp
do
   cd $dir
      find . $modeflag ! -type s -print |\
      sed -e 's/^/"/' -e 's/$/"/'     |\
      xargs -i /bin/rm -rf {}
done
(to clean out /tmp and /var/tmp, modeflag is a -mtime <somevalue> option).

What the sed does is adding "" around the found filenames, it works for most
files (but not, of course, those already containing a " IN the filename).
------------------------------------------------------------
Bill Hassell:
#!/sbin/sh
   for MYFILE in "$(cat /tmp/list)"   # use $( ), grave accent `` is
deprecated
     do
     print "$MYFILE"
   done

By enclosing the list in quotes, MYVAR will be a single line string.
------------------------------------------------------------
Stan Stewart suggested adding double-quotes as shown:
#!/sbin/sh
   for i in "`cat list`"
   do
      print "$i"
   done
------------------------------------------------------------
Lutz Summerfeld noted my original script contained two problems:
The internal field separator IFS value of the shell and that the new-lines
in the file are converted to blanks if you use `cat /tmp/list`. Try this:

#!/sbin/ksh
IFS="\012"   # set IFS to new-line removing blank and tab

while read file
do
   print $file
done < /tmp/list
------------------------------------------------------------


--
             ---> Please post QUESTIONS and SUMMARIES only!! <---
        To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
       Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
 
 Archives:  ftp.dutchworks.nl/pub/digests/hpux-admin       (FTP, browse only)
            http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic