On 08:38 12 Feb 2002, Wojtek Pilorz <[EMAIL PROTECTED]> wrote: | On Tue, 12 Feb 2002, Cameron Simpson wrote: | > On 23:39 11 Feb 2002, Reuben D Budiardja <[EMAIL PROTECTED]> wrote: | > | I need a bit of help with bash here. I try to create a script that would | > | change the extension of files in directory. I have files like | > | xxx.pc that I want a change to xxx.jpg, where xxx is numbers, in a directory. | > | How can I do that using bash? | > | > for f in *.pc | > do newname=`basename "$f" .bc`.jpg | > mv -i "$f" "$newname" | > done | It seems that you try to work correctly for file names containing spaces,
Yes. | etc; if so, the results of basename should be protected | from word split (I am also correcting mistyped ".bc") No. The bit after the = takes place _after_ word split. So, for example, these are _perfectly_ safe: a="1 2 3" b="this;that" c=`ls` abc=$a$b$c | for f in *.pc; do newname="`basename "$f" .pc`".jpg; This extra "`...`" is unnecessary, and makes it harder to use \ inside the `...`. | > Of course that works in any Bourne shell, not just bash. Which is as it should be. | | If we want bash-only solution we can use a bit faster (no subprocessed | created): | | for f in *.pc; do newname="${f%.pc}".jpg; mv -i "$f" "$newname"; done True, and will fail badly on a system without bash. (And again, you don't need those extra "" around ${f%.pc}.) Unless speed is actually a problem, portability is generally to be preferred. Cheers, -- Cameron Simpson, DoD#743 [EMAIL PROTECTED] http://www.zip.com.au/~cs/ You can't always get what you want. - The Rolling Stones _______________________________________________ Redhat-list mailing list [EMAIL PROTECTED] https://listman.redhat.com/mailman/listinfo/redhat-list