I question on a bash script and filename spaces...
I have over 50 files names with spaces in them. I'm trying to use a for loop as such:
for filename in *.php However, when I print the $filename in the script, the script prints out parts of the filename because of the spaces in the file name. For example, say the file name was Star Quest.php When the script executes one $filename would be Star and another Quest.php. How could I account for this? Something like $'filename' I dunno. |
Try using the `find` command?
Code:
for f in "$(find /folder/of/the/files -type f -name "*.php")"; do Sasha |
I have over 50 html files that I have renamed to php files. I want to insert this code into all the php files.
Code:
<br><br> Code:
for f in "$(find . -type f -name "*.php")";do |
It would possibly help if you "quote" the "$f" variable (the filename), no?
Sasha PS - Since you seem to have posted this problem, or a very closely related one, over there: http://www.linuxquestions.org/questi...sed...-782516/ I am going to have that thread merged with this one. Thanks |
I tried the "$f" solution but I get '$f' not a valid identifier error.
|
You'd have to show us the entire process you run in the console. to figure out WHERE the problem with $f is occurring. But I do notice one thing up there in Post#3, and that is you are using a slash "/" as the sed special character (the field separator, so to speak) but the LHS (the regex) also contains a slash "/" which is probably causing sed to screw up. You want to use a different field separator for sed, and/or escape the slashes in the LHS.
Anyhow, please show us what you see exactly in the console, when you run the commands. Sasha |
You need to use the file command and use quotes:
Code:
find . -name "*.php" | while read i |
@ devnull10,
what file command? Do you mean "find" command? And, shouldn't that be a "$i" rather than a "$1", or is it just my font.... |
Code:
for f in "$(find . -type f -name "*.php")" ; do Code:
while read f |
@ colucix -- cool, remains to be seen if this will work for the OP :) -- good call about the quotes; maybe we're not supposed to know why it works, if it does ;)
One thing I'm leery of, is the "< <(..." construct. That doesn't always work, but on that, I do not know why. |
Quote:
|
try to run:
Code:
echo <(:) |
@ konsolebox: great! Your example made me think of the reason why process substitution works in the code above. Indeed process substitution uses file descriptors to send its output to another process, so that the command
Code:
$ echo <(:) In the while loop discussed above, the process substitution feeds a file descriptor and its content is then passed to the loop through the input redirection. We can do the same with a real file and the result is the same (the loop over filenames with blank spaces works as expected) for example Code:
$ cat testfile Instead the for loop doesn't work since it takes the values from a list of arguments and every blank space in the list is interpreted as a field separator. In theory the for loop would work if we could do this: Code:
for f in "Another Quest.php" "Star Quest.php" In summary it is the read statement that does the trick, not the process substitution. I can book a room in clinic, now! ;) |
Quote:
so, you use the find Code:
find . -name "*.php" | while read i |
All times are GMT -5. The time now is 07:28 PM. |