Tux

...making Linux just a little more fun!

Talkback:149/lg_tips.html

[ In reference to "2-Cent Tips" in LG#149 ]

Mulyadi Santosa [mulyadi.santosa at gmail.com]


Wed, 16 Apr 2008 08:58:00 +0700

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.


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Tue, 15 Apr 2008 23:34:04 -0400

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 * https://LinuxGazette.NET *


Top    Back


Rolland Sovarszki [rollandsov at gmail.com]


Wed, 16 Apr 2008 08:49:06 +0300

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


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Wed, 16 Apr 2008 16:29:41 +0700

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.


Top    Back