[SLL] multi-threading

Paul A. Franz, P.E. paul at eucleides.com
Fri Oct 3 00:30:04 PDT 2008


On Mon, September 29, 2008 8:33 am, Robert Woodcock wrote:
> On Mon, Sep 29, 2008 at 03:42:53AM -0700, Paul A. Franz, P.E. wrote:
>> If you see something in the script that you think is poor practice, don't hesitate
>> to
>> point it out.
>
> For reversing an IP address, try this:
>
> sed 's/\([^.]*\)\.\([^.]*\)\.\([^.]*\)\.\([^.]*\)/\4.\3.\2.\1/'

My method, BTW,

    rev_ip="`echo $host_ip | cut -d. -f4`.`echo $host_ip | cut -d. -f3`\
       .`echo $host_ip | cut -d. -f2`.`echo $host_ip | cut -d. -f1`."

and rewritten using your suggested style (I like it!),

    rev_ip="$(echo $host_ip | cut -d. -f4).$(echo $host_ip | cut -d. -f3)\
        .$(echo $host_ip | cut -d. -f2).$(echo $host_ip | cut -d. -f1)."

Seems to me to be at least as readable and timing tests don't show a significant if
any appreciable difference.

> It'll have to spawn a few dozen less processes each time. :)

That would be another project, to write a script that will compare the execution time,
total number of processes and maximum memory required for two equivalent functional
tasks like these.

> Also, for padding a variable when printing it, try the shell printf command:
>
> printf "%15s" $foo

That is a super suggestion. I was under the mistaken impression this C like printing
construct wasn't available in bash. Really appreciate that tip.

> Also, in my scripts I've switched to using $(foo) instead of `foo` - it's
> clearer and it's nestable.

Another good one and eliminates a lot of the use of xargs especially when nesting.

Maybe you don't use xargs either favoring expr?

> let is a bashism, BTW. Make sure you put #!/bin/bash at the start of your
> script. If you want it to be portable you can use "n=$(expr $n + 1)".

You referred to my use of:
     let "n+=1"

This shell script is already not too portable because it works with Sendmail log files
where Sendmail, /etc/mail/access, and /etc/mail/sendmail.mc are all so customized and
the shell script looks for the results of the log messages based on the
customizations.

These arithmetic assignments in bash and C and C descendants always have bothered me
as a long time Fortran programmer where

     n=n+1  <<== much cleaner than either 'n=$(expr $n + 1)' or 'let "n+=1"'

Is all that it takes. This business of having to put a $ sign in front of a variable
when it is on the right side of = and not doing so on the left side seems totally
unnecessary to me as is the choice of passing data in an argument list by value or by
reference. Compiler should totally handle that. So all you do is specify the variable
name. Go ahead, straighten me out on this!

-- 
Paul A. Franz, P.E.
PAF Consulting Engineers
Office 425.440.9505
Cell 425.241.1618


More information about the linux-list mailing list