[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: OT: Bash Scripting Question



Hal Vaughan wrote:
> And, while we're discussing grep -- it raises a question I might as well ask
> here.  Since grep will search for expressions, is there a similar command
> that can do a search and replace in files without the need for a Perl or
> Python script?  (Just thought I'd ask.)

    sed

    This gets back to my point of shell being capable of doing some things
that are not included in Redmond OSes but I would program in shell if my life
depended on it.  Shell programming is exceptionally messy.  Ungodly messy.
The beauty of shell is that all those nice programs can be piped into and out
of.  The problem is they use command lines to do their work and command lines
are often parsed by the shell.  As a result you get sick constructs like \\\'
just to place a single quite in a shell script (\\ for the \ and then \' for
the ').  Shell scripts, to me, are just one massive debugging headache because
one missed quote, the wrong quote, a missed escape and so on just means the
entire thing goes kablooie.

    If you can do it in a one-liner and it is something you can test over and
over to work out the kinks, great.  If it needs to be even remotely fast is
more than, say, 2-3 lines long or requires the use of more than one variable I
firmly believe that one should just open up $EDITOR and write it in Python or,
baring such sanity, Perl, Ruby or other $scripting_language of choice.  The
time saved not having to escape every last detail just so is more than made up
by being able to do something like...

files = os.listdir(dir)
for file in files
    os.unlink(file)

    ...without worrying about -rf\ \/ being the first file in the list or
other such examples.  I know it's possible to get around that in shell.
Granted.  I just prefer not having to get around it in the first place.

--
         Steve C. Lamb         | I'm your priest, I'm your shrink, I'm your
       PGP Key: 8B6E99C5       | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------

Attachment: signature.asc
Description: OpenPGP digital signature


Reply to: