Register
It is currently Mon Dec 22, 2014 12:03 pm

Using arguments from file in the way "$@"


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Wed Nov 12, 2008 4:11 am   

Joined: Wed Nov 12, 2008 3:45 am
Posts: 3
I have a script that reads pathnames (one in each line and may contain space) from a file and feed all the pathnames to an external program such as `find'. How can I make it (the 2nd `find') work as if it was fed with "$@"? Thanks!

Code:
#!/bin/bash
file=test.in
for d in "$@"; do
  echo $d
done > $file

find "$@" -type f   # this works perfectly

list=`cat $file`
find $list -type f    # this fails when a path contains space


Run the above script with two arguments: dir1 dir2\ with\ space


Top
 Profile  
 PostPosted: Wed Nov 12, 2008 6:48 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 229
Location: London - UK
I haven't examined this in detail but generally these issues can be solved by quoting everything correctly, for example;

Code:
list=`cat $file`
find $list -type f


would be better written as...

Code:
list=$(cat "${file}")
find "${list}" -type f


which should eliminate issues with spaces


Top
 Profile  
 PostPosted: Wed Nov 12, 2008 7:11 am   

Joined: Wed Nov 12, 2008 3:45 am
Posts: 3
To make things clear, I just want the following script to do `find dir1 dir2\ with\ space -type f` after reading the file "foo".

Code:
foo
---
dir1
dir2\ with\ space

script
------
#!/bin/bash

list=`cat foo`
find $list -type f    # treated as 4 paths: find 'dir1' 'dir2\\' 'with\\' 'space'


Top
 Profile  
 PostPosted: Wed Nov 12, 2008 11:17 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Try this!

Code:
while read list; do find "$list" -type f; done < foo


Hope it helps!
-J


Top
 Profile YIM  
 PostPosted: Wed Nov 12, 2008 7:34 pm   

Joined: Wed Nov 12, 2008 3:45 am
Posts: 3
Thanks. actually, I've been doing this. But what I need is to pipe its output for further processing without saving to a temporary file.

find $list -type f | process

Thanks in advance for any idea.


Top
 Profile  
 PostPosted: Thu Nov 13, 2008 11:58 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Right on, that's not a problem. My example doesn't put anything into a temporary file. It's taking input from "foo" which I borrowed from your example. I'm just generating $list with 'read' instead of 'cat'. You should be able to pipe the output to another script just fine, but you still might run into problems with the spaces. you may be able to get around that by using the exec switch to find:

Code:
while read list; do
  find "$list" -type f -exec process {} \;
done < foo


Haven't tested that, but it should give you a general idea. Does that help?

-J


Top
 Profile YIM  
 PostPosted: Mon Nov 17, 2008 8:58 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
I'd do a tricky one ;)

Insert script to filename:
Code:
#!/bin/bash
file=/tmp/paths.tmp
for i in $@; do
   echo $i >> $file
done


For the find!
Code:
#!/bin/bash
file=/tmp/paths.tmp
IFS="\n"
for i in $(cat $file); do
   find $i $@
done


the trick is the IFS="\n", IFS states what the delimiting character is. In this case a newline is the delimiter for for-loops. Normally (and i think most of you encountered this before) a whitespace character will delimit the for-loop, so a path like /home/fredrik eriksson/ would itterate /home/fredrik and eriksson/.

Hope this made any sense :)

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Mon Nov 17, 2008 5:20 pm   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Makes sense to me! I try to avoid messing with IFS, but it would certainly work in this situation!


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP