• Bug#922423: initscripts: formatting of scripts

    From Mark Hindley@21:1/5 to Dmitry Bogatov on Mon Mar 17 12:30:01 2025
    On Fri, Feb 15, 2019 at 05:27:26PM +0000, Dmitry Bogatov wrote:
    Package: initscripts
    Severity: wishlist

    initscripts currently quite inconsistent in programming style and
    formatting, using mix of tabs and spaces, test and [ ] and so on.

    I agree we should do this for forky cycle. Can we agree the style we want?

    As a starter, I suggest (debian/src/README.style)

    Shell script style
    ==================

    Scripts should follow the style produced by shfmt(1) utilising the -ci and -s options. That is

    - tab indented

    - case indented

    - ';' rather than a newline between conditional test and 'then'

    - no space before redirection operators

    In addition:-

    - prefer '[ ]' to test(1) or builtin


    Comments? Changes?

    Mark

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mark Hindley@21:1/5 to All on Mon Mar 17 13:10:01 2025
    I am going to suggest a few more.

    - avoid obsolete x prefixes such as [ x"$VARIABLE" = xvalue ]

    - avoid [ .. -a .. ] and [ .. -o .. ] in POSIX scripts (undefined)

    Mark

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lorenzo@21:1/5 to Mark Hindley on Mon Mar 17 19:20:01 2025
    Hi!

    On Mon, 17 Mar 2025 11:23:22 +0000
    Mark Hindley <mark@hindley.org.uk> wrote:

    On Fri, Feb 15, 2019 at 05:27:26PM +0000, Dmitry Bogatov wrote:
    Package: initscripts
    Severity: wishlist

    initscripts currently quite inconsistent in programming style and formatting, using mix of tabs and spaces, test and [ ] and so on.

    I agree we should do this for forky cycle. Can we agree the style we
    want?

    As a starter, I suggest (debian/src/README.style)

    partially off topic but I think it will help if we recommend to use
    the /lib/init/init-d-script since by using it you decrease the amount
    of code that can display inconsistent style.

    Lorenzo

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Martin Steigerwald@21:1/5 to All on Mon Mar 17 20:40:01 2025
    Hi.

    Lorenzo - 17.03.25, 19:12 CET/CEST:
    I agree we should do this for forky cycle. Can we agree the style we
    want?

    As a starter, I suggest (debian/src/README.style)

    partially off topic but I think it will help if we recommend to use
    the /lib/init/init-d-script since by using it you decrease the amount
    of code that can display inconsistent style.

    Agreed. It can make for quite elegant and minimal scripts:

    https://salsa.debian.org/debian/fio/-/blob/master/debian/fio.init?ref_type=heads

    Best,
    --
    Martin

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Thorsten Glaser@21:1/5 to Mark Hindley on Mon Mar 17 21:40:01 2025
    On Mon, 17 Mar 2025, Mark Hindley wrote:

    Scripts should follow the style produced by shfmt(1)

    FWIW, never heard of that tool.

    - case indented

    Don’t indent case, it’s useless and just adds clutter.
    You cannot have something other than the pattern inside, so:

    case $foo in
    (bar)
    echo bla
    ;;
    (*)
    echo nope
    ;;
    esac

    (If all scriptlets are extremely short, merging on less
    lines is possible.)

    - prefer '[ ]' to test(1) or builtin

    I personally, as the maintainer of a shell and a plethora of
    scripts, recommend people to prefer test(1) over [ … ] instead
    because the latter “looks like” special syntax but [(1) isn’t
    (unlike [[ … ]], which they may also know), and that leads to
    insecure code, whereas test(1) looks more like a normal program
    whose arguments need proper quoting. I’m aware that this is a
    minority opinion, though.

    http://www.mirbsd.org/man9/style has bits applying to shell
    scripts as well.

    bye,
    //mirabilos
    --
    „Cool, /usr/share/doc/mksh/examples/uhr.gz ist ja ein Grund,
    mksh auf jedem System zu installieren.“
    -- XTaran auf der OpenRheinRuhr, ganz begeistert
    (EN: “[…]uhr.gz is a reason to install mksh on every system.”)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mark Hindley@21:1/5 to Lorenzo on Tue Mar 18 08:50:01 2025
    On Mon, Mar 17, 2025 at 07:12:27PM +0100, Lorenzo wrote:
    partially off topic but I think it will help if we recommend to use
    the /lib/init/init-d-script since by using it you decrease the amount
    of code that can display inconsistent style.

    Yes. the example LSB script has been init-d-script based for a while.

    This question is really about the scripts packaged in bin:initscripts. I am not taking on rewriting them all for init-d-script!

    Mark

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Martin Steigerwald@21:1/5 to All on Tue Mar 18 09:40:01 2025
    Mark Hindley - 18.03.25, 08:41 CET/CEST:
    On Mon, Mar 17, 2025 at 07:12:27PM +0100, Lorenzo wrote:
    partially off topic but I think it will help if we recommend to use
    the /lib/init/init-d-script since by using it you decrease the amount
    of code that can display inconsistent style.

    Yes. the example LSB script has been init-d-script based for a while.

    This question is really about the scripts packaged in bin:initscripts. I
    am not taking on rewriting them all for init-d-script!

    Sure!

    I am aware the bug report is about adapting existing scripts, but for now
    your question was on feedback about README.style.

    I'd not expect you or anyone else to convert all init scripts. Especially
    as it is not that they would not be working as is. During forky cycle I
    think anyone who likes to contribute on this can do so by providing merge requests.

    However I think for any new scripts and in case someone likes to
    contribute adapted scripts a mention of init-d-script in README.style
    would be good.

    And it can still be an incremental approach but for forky cycle just
    adapting syntax first. And then maybe as people are willing to contribute convert at least simpler ones to init-d-script.

    However… also there might be scripts where converting to init-d-script
    does not really make sense cause one would need to override the default implementation of every action in init-d-script. So maybe mention that init-d-script might not benefit very complex init scripts that much if at
    all. But for simpler init scripts an adaption to init-d-script might be
    less work than rewriting certain shell syntax and reformatting.

    --
    Martin

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mark Hindley@21:1/5 to Martin Steigerwald on Tue Mar 18 10:20:01 2025
    On Tue, Mar 18, 2025 at 09:32:25AM +0100, Martin Steigerwald wrote:
    However I think for any new scripts and in case someone likes to
    contribute adapted scripts a mention of init-d-script in README.style
    would be good.

    Yes, I agree. I have added

    - consider using init-d-script(5)

    Thanks

    Mark

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Marc Chantreux@21:1/5 to Thorsten Glaser on Wed Mar 19 16:00:01 2025
    hello,

    On Mon, Mar 17, 2025 at 09:31:22PM +0100, Thorsten Glaser wrote:
    Scripts should follow the style produced by shfmt(1)
    FWIW, never heard of that tool.

    shell is one of my main programming langages since the last millenium
    and I confirm: shfmt is unkown by the communities I belong to.

    Don’t indent case, it’s useless and just adds clutter.

    *yes* thanks for that

    Also I would like to go one step further: as in some shells, the case
    separator can be ;; or ;&, "hiding" it in the cod indentation is
    something I wanted to avoid.

    my coding style for many years is:

    * case separators prefixes the line
    * use the opening parenthesis

    my vim snippet for this is

    case "${1}"
    in (-h | --help | help ) "exactly! HELP!!!!! we need a scrum master here!"
    ;; (……)
    ;; (*) "that's no moon, it's a space station … -- Obiwan. Aldeeran"
    esac

    a real life exemple can be seen here:

    https://git.unistra.fr/mc/dot/-/blob/main/bin/tsveverything?ref_type=heads#L44


    --
    Marc Chantreux

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)