change for-loop delimiter character?
Hello,
I'm not sure if the subject is 100% correct, but it was the best I could come up with. I wanted to make a simple for loop to apply many files to one command: Code:
for i in `ls *eps`; do epstopdf $i; done And yes, I know I could just replace the space with underscore and; voilą! I want to know if this can be done though :) |
try:
Code:
for i in `ls *eps`; do epstopdf "$i"; done Code:
for i in *eps; do epstopdf "$i"; done |
Great! The last one worked perfectly. I don't understand why though :) How does '*eps' differ from 'ls -c1 *eps'?
And the quotes in the 'do' part of the loop doesn't seem to make any difference since the filenames are split up in the first part of the loop. Is there no way to explicitly tell it to ignore a space? |
I don't really know to be honest, but I suspect it's got something to do with shell expansion of *. That is to say, the shell (bash?) handles the *eps part, and for each iteration of the loop, it moves to the next file, whereas if you use `ls -c1 *eps`, all the eps files in your directory get passed to the for loop as a long line of files since the ls is expanding the *, rather than bash. When it's been expanded by ls, epstopdf can't handle that sort of input (a long list of many files).
If you're interested, there's someone on these forums called matthewg42 who explained this really well in another thread somewhere. I'll have a look to see if I can find it. |
Did you mean this:
Quote:
|
This is what the bash manpage has to say on *:
Code:
Special Parameters Code:
IFS The Internal Field Separator that is used for word splitting |
I seem to have a similar problem with the following which searches for all the files not accessed for a year:
for i in `find ./ -name "*" -atime +365`; do ls -lh ${i}; done I get what I expect until I find files with spaces in their names. NOTE: I don't actually want to ls the resulting finds, this is just for demonstration of the problem and minimising the code here. I attempted using sed to replace ' ' with '\ ' and awk to place double quotes around the names. After reading this thread it seems I need to set the "IFS" with end of line \n ? IFS='\n' seems not to work so a little more googling brought me to: IFS=$'\n' eg IFS=$'\n';for i in `find /home/keith/ -name "*" -atime +365`; do ls -lh ${i}; done |
All times are GMT -5. The time now is 01:53 PM. |