[Gllug] perl script

Tom Gilbert tom at linuxbrit.co.uk
Thu Aug 22 09:04:32 UTC 2002


* Nick Mailer (nickm at positive-internet.com) wrote:
> On Thursday 22 August 2002 2:23 am, Mike Brodbelt wrote:
> > On Thu, 2002-08-22 at 00:28, Nick Mailer wrote:
> > > On Wednesday 21 August 2002 7:42 pm, Thom May wrote:
> > > > Ber. forking perl for this is uber-overkill
> > > > #!/bin/bash
> > > > for i in *jpeg
> > > > do
> > > >         new=$(echo $i|sed -e \
> > > >         's#^\(.\)\(.\)\(.\)\(.\)\(.\)\(.*\)#\4\5\3\1\2\6#');
> > > >         mv $i $new;
> > > > done
> > > >
> > > > Sorry about the nasty sed regexp - it's doable with bash direct but
> > > > that is dark magic.
> > > > -Thom
> > >
> > > So instead of forking one process of perl, you multiply-fork echo, sed
> > > and mv. Well done, Mr Efficiency ;-)
> >
> > <pedant>
> > Echo is a shell builtin in bash.
> > </pedant>
> >
> > Mike.
> 
> <pedant++>
> 
> It is, but if /bin/echo is there (for other shells), the builtin is ignored..
> 
>  nickm at barnet:~$ strace echo hello
>  execve("/bin/echo", ["echo", "hello"], [/* 21 vars */]) = 0
> 
> </pedant++>

Uh - that's not true - the builtin will be used unless you explicitly
invoke /bin/echo.

Plus strace calls exec() on its first argument, so your example doesn't
show anything :) Try doing strace bash, then (amongst the mess) you'll
see that "echo hello" causes bash to just write("hello"), rather than
execing anything.

And if anyone wants to be pedantic, I'd like to point out that
efficiency isn't worth a damn when you're renaming a few files in a one
off operation, and that it's far more important to choose the most
appropriate and reliable tool for the task :)

I've mentioned rename.pl on the list before and it's good to have around
for situations like this, e.g:
$ rename.pl 's/(.*)foo/foo$1/' *.jpeg
or whatever.

Tom.
-- 
   .^.    .-------------------------------------------------------.
   /V\    | Tom Gilbert, London, England | http://linuxbrit.co.uk |
 /(   )\  | Open Source/UNIX consultant  | tom at linuxbrit.co.uk    |
  ^^-^^   `-------------------------------------------------------'

-- 
Gllug mailing list  -  Gllug at linux.co.uk
http://list.ftech.net/mailman/listinfo/gllug




More information about the GLLUG mailing list