LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2007, 05:17 AM   #1
garr0n
LQ Newbie
 
Registered: Mar 2007
Distribution: Slackware 12.0
Posts: 20

Rep: Reputation: 0
Read list of files as arguments in a script


This is a theoretical problem at this point for me, but nonetheless it's something that's been bothering me for awhile.

Say I have a list of files in a text file, one file per line. Some files have characters that need escaping (spaces, etc.). I want to pass each file as an argument to a program. How?

Code:
% cat filelist
./dir 1/file 1
./dir 2/file 2

% cat `cat filelist`
cat: ./dir: No such file or directory
cat: 1/file: No such file or directory
cat: 1: No such file or directory
cat: ./dir: No such file or directory
cat: 2/file: No such file or directory
cat: 2: No such file or directory

% cat "`cat filelist`"
cat: ./dir 1/file 1
./dir 2/file 2: No such file or directory
 
Old 10-17-2007, 05:52 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Code:
bash 3.2.25(1)$ mkdir 'dir 1' 'dir 2'
bash 3.2.25(1)$ echo FileOne>'./dir 1/file 1'
bash 3.2.25(1)$ echo FileTwo>'./dir 2/file 2'
bash 3.2.25(1)$ echo './dir 1/file 1
> ./dir 2/file 2'>filelist
bash 3.2.25(1)$ cat `cat filelist`
cat: ./dir: No such file or directory
cat: 1/file: No such file or directory
cat: 1: No such file or directory
cat: ./dir: No such file or directory
cat: 2/file: No such file or directory
cat: 2: No such file or directory
bash 3.2.25(1)$ (IFS=$'\n';cat `cat filelist`)
FileOne
FileTwo
 
Old 10-17-2007, 06:55 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
If you set the IFS to newline only in your script, you'll then be able to use the cat cmd to read in each line.
You'll still have to be careful to escape spaces during further processing.
 
Old 10-17-2007, 08:10 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
try...
Code:
cat file_list | 
while read file ;do
   command "$file"
done
 
Old 10-17-2007, 08:18 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Mmmm - both Chris and billy responded to a thread like this, and neither suggested perl as the solution.

I'm disappointed ...
 
Old 10-17-2007, 12:58 PM   #6
garr0n
LQ Newbie
 
Registered: Mar 2007
Distribution: Slackware 12.0
Posts: 20

Original Poster
Rep: Reputation: 0
radoulov, chrism1: Great, thanks! Guess I need to read the bash manual a bit more.

bigearsbilly: That actually calls command once per argument, no? For 'cat' it has the same result, obviously. Still, it's always good to learn another trick that might help sometime down the road.

syg00: Yeah, "Learn perl" would be a great solution, if a little much in the short term.
 
Old 10-18-2007, 02:07 AM   #7
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
hey, why not use perl?


(are we so predictable?)
 
Old 10-18-2007, 02:23 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by bigearsbilly View Post
try...
Code:
cat file_list | 
while read file ;do
   command "$file"
done
the cat can be skinned.
Code:
while read file ;do
   command "$file"
done < file_list
 
Old 10-18-2007, 02:28 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by garr0n View Post
bigearsbilly: That actually calls command once per argument, no? For 'cat' it has the same result, obviously. Still, it's always good to learn another trick that might help sometime down the road.
"cat"'s purpose is to concatenate files (although almost everyone i see use it to display files). use tools like awk (or even the while loop) if you want to process files.

Quote:
syg00: Yeah, "Learn perl" would be a great solution, if a little much in the short term.
before there's Perl, there's awk.
 
Old 10-25-2007, 08:18 PM   #10
garr0n
LQ Newbie
 
Registered: Mar 2007
Distribution: Slackware 12.0
Posts: 20

Original Poster
Rep: Reputation: 0
I know cat is meant to concatinate files, it was just the simplest thing I could think of for an example.

Can this be done with awk? I've used awk a little but it was mostly just copying examples and replacing a few strings... I think awk is the next thing on my list of Unix tools to explore properly.
 
Old 10-25-2007, 10:19 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by garr0n View Post
Can this be done with awk?
of course. depending on what your program that takes in argument is
Code:
awk '{
  cmd="program \047"$0"\047" #single quote the file path with spaces
  system(cmd) #or cmd|getline
  close(cmd)
}' file
Quote:
I've used awk a little but it was mostly just copying examples and replacing a few strings...
its more than that. it loops over files so almost always, i don't have to use while/for loops. its grep/sed/cat/etc combined, so almost always, i only use awk for my job. Of course, this is just what i do. No need to disagree with me on this.

Quote:
I think awk is the next thing on my list of Unix tools to explore properly.
it should your next big thing
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need bash script to list files, drop extension and dump to file talwar_ Programming 10 06-03-2011 09:18 AM
how to create a simple script for installing a list of files zbenjudah Debian 28 04-16-2007 03:03 PM
List of arguments too long, need to delete 59,000 files stefaandk Linux - General 4 07-12-2006 02:14 AM
passing a list of arguments to a command hdagelic Linux - General 2 05-09-2005 09:30 AM
Script to Run the Same Commands on a List of Files Chryzmo Programming 1 10-03-2004 08:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration