this post was submitted on 11 Aug 2024
28 points (96.7% liked)
Linux
5362 readers
49 users here now
A community for everything relating to the linux operating system
Also check out !linux_memes@programming.dev
Original icon base courtesy of lewing@isc.tamu.edu and The GIMP
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
can anyone help me figure out, why the following shell script does not work:
I am running it in a location with a lots of folders containing spaces (think of it like this:
I get errors of the following form:
but when I manually enter
cd 'baa foo'
it works fine. Why could that be? (the echo retuns something like "foo baa #" .) It really confuses me that the cd with the exact same string works when I enter it manually. I have allready tried leaving out the quotes in the cd command and escaping the spaces usingdir=$(printf %q "${dir}");
before the cd but that did not work either.tbh I am new to shell scripts so maybe there is something obvious I overlooked.
You're probably over-complicating things. Have you heard about the
find -print0 | xargs -0
idiom? all that variable interpolation (dir=${dir:2}
) and quoting"'""${dir}""'"
is better to be dealt by the built-in shell tools. Or you could write a script for the whole body of that while loop, and then callfind . -exec ./action.sh {} \;
. Same script could be used with the previously mentioned idiom too, you'd need to usebash -c ./action.sh
though. One advantage of "find | xargs" is that you can run these concurrently, paralellizing the action to all your dirs, in groups, of say 4 of them... and so on... it's cool and simple.