Current-Users archive

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

Re: Output alignment in build.sh



    Date:        Tue, 24 Aug 2010 02:46:55 -0400
    From:        christos%zoulas.com@localhost (Christos Zoulas)
    Message-ID:  <20100824064655.911EA56426%rebar.astron.com@localhost>


  | +statusmsg2()
  | +{
  | +   msg="${1}"
  | +   while [ ${#msg} -lt 21 ] ; do
  | +           msg="${msg} "
  | +   done
  | +   statusmsg "${msg}${2}"
  | +}

Ignoring the question of what the function should be called, this looks
like a C programmer writing shell code, how about instead ...

whatevername()
{
        local msg

        msg="${1}"
        case "${msg}" in
        ????????????????*)      ;;
        ??????????*)            msg="${msg}      ";;
        ?????*)                 msg="${msg}           ";;
        *)                      msg="${msg}                ";;
        esac
        case "${msg}" in
        ?????????????????????*) ;;
        ????????????????????)   msg="${msg} ";;
        ???????????????????)    msg="${msg}  ";;
        ??????????????????)     msg="${msg}   ";;
        ?????????????????)      msg="${msg}    ";;
        ????????????????)       msg="${msg}     ";;
        esac
        statusmsg "${msg}${2}"
}

The first case statement makes sure (if I counted ?'s and spaces
correctly) that the msg is at least 16 characters long, the second
handles each of the possibilities, and adds the appropriate number
of spaces, so they're all the same length (except if msg was too long
to start with of course.)

You could use "case "${#msg}" and 16|17|18|19|[2-9]*) (etc) as the patterns,
but the way I wrote it works on even the oldest Bourne style shell
(${#var} is a later addition) and to me anyway, is just more obvious
(some shells might not like the "local" declaration, but that can just be
removed if needed).

kre



Home | Main Index | Thread Index | Old Index