The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Storing space delimited line in var with loop? eltinator Shell Programming and Scripting 2 08-23-2007 10:09 PM
Moving Files iAm4Free Shell Programming and Scripting 5 03-28-2007 01:13 AM
moving files ?? rocker40 UNIX for Dummies Questions & Answers 9 11-09-2003 04:36 PM
moving only files... sskb UNIX for Dummies Questions & Answers 12 11-19-2001 11:28 AM
moving space from one partition to another jason6792 UNIX for Advanced & Expert Users 4 10-08-2001 03:54 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-07-2004
Registered User
 

Join Date: Sep 2002
Location: UK
Posts: 65
Moving files with space, in for loop

Hi All
I need to put a bunch of specific files in a directory (with loads of other files), into a tar archive. The best way I thought of doing this was putting the filenames into a file, reading them line by line in a for loop, and then adding them to a tar acrhive.

However the filenames have spaces:

/the/path/to/the/file/the file name with spaces

So when I do a for x in `cat /listoffiles` etc, it doesn't work because the shell sees the spaces as separators, and hence the bits of the name as separate files.

Anyone got a resolution to this? If I list the filename at the command line, enclosed by quotes, it's OK - but when I put $x in quotes in my for loop, it doesn't work.

Thanks for any help!
__________________
Free Palestine
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-07-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,859
put $x in quotes -

Code:
 "$x"
Reply With Quote
  #3 (permalink)  
Old 05-07-2004
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,453
jim's solution will not work. The problem is "for x in `cat listoffiles`" will spilt the contents of listoffiles on whitespace. So with the example given, x will be "the" then it will be "file". And so on. Placing $x in quotes isn't enough because it's too late...the filename was split before x got a value.

My solution would be:

Code:
#! /usr/bin/ksh
exec < listoffiles
while read x ; do
       somecommand "$x"
done
Reply With Quote
  #4 (permalink)  
Old 05-10-2004
Registered User
 

Join Date: Sep 2002
Location: UK
Posts: 65
Excellent

Nice one Perderabo, it works perfectly.

That is why I have always maintained you are the UNIX king.
__________________
Free Palestine
Reply With Quote
  #5 (permalink)  
Old 05-10-2004
Registered User
 

Join Date: Apr 2004
Posts: 17
one more solution on the same line is

while read line
do
echo "$line"
done < file_name;

This will work.



Ramesh
__________________
Ramesh
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0