ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
However this method fails when the filenames contain spaces. I tried using sed to insert \ before the spaces and surrounding the filenames in quotes, but it seems the for-loop ignores all of this and splits the arguments at the spaces no matter what. Does anyone know how to only split the input e.g. at newlines?
And yes, I know I could just replace the space with underscore and; voilą! I want to know if this can be done though
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.
...the shell expands the list fist and passes the list to ls. ls never sees the meta-character, *. I only mention it because for a lot of DOS veterans like myself, this was the other way round. It's a moment of epiphany for a lot of people to realise how it's working. Was for me at any rate
This came as a surprise to me, I must admit... But now I see what you where talking about a lot better
Special Parameters
The shell treats several parameters specially. These parameters may
only be referenced; assignment to them is not allowed.
* Expands to the positional parameters, starting from one. When
the expansion occurs within double quotes, it expands to a sin-
gle word with the value of each parameter separated by the first
character of the IFS special variable. That is, "$*" is equiva-
lent to "$1c$2c...", where c is the first character of the value
of the IFS variable. If IFS is unset, the parameters are sepa-
rated by spaces. If IFS is null, the parameters are joined
without intervening separators.
and on IFS
Code:
IFS The Internal Field Separator that is used for word splitting
after expansion and to split lines into words with the read
builtin command. The default value is '<space><tab><new-
line>'.
If you're interested in understanding this more than me (!), have a look for globbing and regular expressions. I think that these are all the different ways of expanding various variables
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.