...making Linux just a little more fun!
[ In reference to "2-Cent Tips" in LG#149 ]
Mulyadi Santosa [mulyadi.santosa at gmail.com]
Hi Rolland...
On Wed, Apr 16, 2008 at 2:46 AM, Rolland Sovarszki <rollandsov@gmail.com> wrote:
> Hi there > > I am new to Linux, but with a big desire to become better. I have recently > read your article "2-cent tips: convert the collection of your mp3 files > into ogg" > in Linux Gazzete. I have found it very interesting, and I wanted to try it > out for myself. Everything went fine, except the script for replacing the " > " (blanks) with "_". > After a little digging, and study of sed command, I have found out that the > problem came from this line of code: > > do mv -v "$a" $(echo $a | sed s/\/\_/g); > > So I have replaced it with: > > do mv -v "$a" $(echo $a | sed s/" "/\_/g);
Thanks a ton! If you check the discussion thread in TAG following my post, there are various criticism for this tip, whether it's improvement or correction. Therefore, I highly appreciate your feedback and CC your e-mail to TAG, so everybody can get the benefit.
regards,
Mulyadi.
Ben Okopnik [ben at linuxgazette.net]
On Wed, Apr 16, 2008 at 08:58:00AM +0700, Mulyadi Santosa wrote:
> Hi Rolland... > > On Wed, Apr 16, 2008 at 2:46 AM, Rolland Sovarszki <rollandsov@gmail.com> wrote: > > Hi there > > > > I am new to Linux, but with a big desire to become better. I have recently > > read your article "2-cent tips: convert the collection of your mp3 files > > into ogg" > > in Linux Gazzete. I have found it very interesting, and I wanted to try it > > out for myself. Everything went fine, except the script for replacing the " > > " (blanks) with "_". > > After a little digging, and study of sed command, I have found out that the > > problem came from this line of code: > > > > do mv -v "$a" $(echo $a | sed s/\/\_/g); > > > > So I have replaced it with: > > > > do mv -v "$a" $(echo $a | sed s/" "/\_/g);
There's a problem with this version as well - pretty much the same one as Mulyadi's original script had, but it's disguised a little better.
The problem is that the substitution operation (s///) is part of "sed" syntax, but - since it's not protected by quotes - gets to be interpreted by the shell. Mulyadi's original version (which he'd obviously mistyped) was
sed s/\ /\_/g
Take a careful look at it. Why is there an escape ('\') in front of the space? Because without it, the shell (not sed) will split it up into two tokens and present 'sed' with the following (extra spaces added for clarity):
sed s/ /_/g
That is, 'sed' as a Unix command, 's/' as the operation. and '/_/g' as the filename to be processed. Obviously wrong. Escaping the space - whether with a backslash or with a pair of quotes - makes it a literal character, so the shell leaves it alone. Doing it this way, however, leaves you having to escape every single questionable character in the 'sed' commandline.
The simple solution is - *don't do that*. Instead, always protect any operations that you need to send to a program with a pair of single quotes:
sed 's/ /_/g' filename grep '^foobar$' filename nawk '{print $NF}' filename perl -walne'print $F[3]' filename
This will avoid the need for all that escaping.
-- * Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *
Rolland Sovarszki [rollandsov at gmail.com]
Hi both
Thanks a lot for your answers. Mulyadi thanks for awakening my curriosity in trying out new things,and Ben, thanks for the small bash lesson )
Best regards
Mulyadi Santosa [mulyadi.santosa at gmail.com]
Hi
On Wed, Apr 16, 2008 at 10:34 AM, Ben Okopnik <ben@linuxgazette.net> wrote:
> There's a problem with this version as well - pretty much the same one > as Mulyadi's original script had, but it's disguised a little better.
As always, I greatly respect Ben's clear answer.... somebody who volunteers some of his free time to support others for free deserve two thumbs up IMHO.
regards,
Mulyadi.