?

Log in

No account? Create an account
sh is ugh - brad's life — LiveJournal [entries|archive|friends|userinfo]
Brad Fitzpatrick

[ website | bradfitz.com ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

sh is ugh [Oct. 21st, 2004|10:29 pm]
Brad Fitzpatrick
[Tags|, ]

I know people say Perl looks ugly, but man...

From OpenIPMI's bmclanconf:
hex_to_ip ()
{
    test $# -ge 1 || return;
    local HEX B1 B2 B3 B4;
    HEX=`echo $1 | tr '[:lower:]' '[:upper:]'`;
    H1=`echo $HEX | cut -c1-2`
    H2=`echo $HEX | cut -c3-4`
    H3=`echo $HEX | cut -c5-6`
    H4=`echo $HEX | cut -c7-8`
    B1=`echo 10 o 16 i $H1 p | dc`
    B2=`echo 10 o 16 i $H2 p | dc`
    B3=`echo 10 o 16 i $H3 p | dc`
    B4=`echo 10 o 16 i $H4 p | dc`
    echo "$B1.$B2.$B3.$B4"
}

Not sure if I should be impressed or disgusted. Kinda my reaction when I see anything done in a shell.

I used to complain about TCL, but shell scripts piping things into new programs left and right... is that anywhere near efficient? Or does it come down to just not mattering? I suspect.

IPMI is frickin' awesome, though.
LinkReply

Comments:
[User Picture]From: quelrod
2004-10-21 10:49 pm (UTC)
Well the concept of 1 program 1 task is beautiful in theory. It can certainly be combined with pipes to create some powerful stuff. Of course something like perl certainly makes scripts less painful. Shell scripting seems to be a dying art. Personally it comes down to using the right tool for the job. I also think the more languages (yes I'll call shell scripting a language for our purposes) you are at least moderately familiar with...the more options you have and your knowledge of one language will certainly help your knowledge of another. Though, it sure isn't pretty.
(Reply) (Thread)
[User Picture]From: karl
2004-10-22 07:48 am (UTC)

Tcl

What's the complaint about Tcl? It kicks ass. A Tcl equivalent of that shell script will be thousands of times faster, easier to write and (unlike PERL) read.

You even suspect for the horrible script that the inefficiency (18 forks, 18 execs, 18 pipe setups and like 100 file descriptor closes) doesn't matter.

So if it might not matter anyway, why slap something that has way less of what you're complaining about?
(Reply) (Thread)
[User Picture]From: brad
2004-10-22 09:52 am (UTC)

Re: Tcl

I got real beef with TCL, especially its readability... I love its tiny grammar.

Early versions of TCL passed strings around, always. I was (poorly) making the comparison to TCL's string passing as an analogy to the shell's use of exec and pipes.
(Reply) (Parent) (Thread)
[User Picture]From: karl
2004-10-22 10:52 am (UTC)

Re: Tcl

Thanks for the clarification, Brad. Yeah, Tcl was a lot more gross when everything was strings. For the last few years, with its internal use of objects that cache native interpretations of things such as integers, reals, lists, etc, (and the same objects are used to cache the code emitted by the on-the-fly compiler next to its source), Tcl does minimal string copying, list reparsing, etc. In some cases, programs became thousands of times faster migrating from Tcl 7 to 8.


As for readability, Tcl is among the better IMO (and surely almost everyone would agree it's way more readable than PERL or, for example, shell scripts). In any case, I think a big part of readability is having good names and comments. Consider the code below, which is part of Rivet's session management object. You don't even have to know Tcl to have some sense of what's going on...

    #
    # consider_garbage_collection - perform a garbage collection gcProbability
    #   percent of the time.  For example, if gcProbability is 1, about 1 in
    #   every 100 times this routine is called, garbage collection will be
    #   performed.
    #
    method consider_garbage_collection {} {
        if {rand() <= $gcProbability / 100.0} {
            do_garbage_collection
        }
    }

    #
    # set_session_cookie - set a session cookie to the specified value --
    #  other cookie attributes are controlled by variables defined in the
    #  object
    #
    method set_session_cookie {value} {
        cookie set $cookieName $value \
            -path $cookiePath \
            -minutes $cookieLifetime \
            -secure $cookieSecure
    }

   
(Reply) (Parent) (Thread)
[User Picture]From: brad
2004-10-22 11:37 am (UTC)

Re: Tcl

I meant to type: "I got no real beef".

I agree that TCL is /very/ readable.
(Reply) (Parent) (Thread)