0
\$\begingroup\$

Say, I have my POSIX script introduction stored in variable:

script_intro=\
'------------------------------------------------------------
--           Safe system files editing as root            --
--           Code language: POSIX shell script            --
--         Copyright: 2020-2023 Vlastimil Burian          --
--             Email: [email protected]             --
--              GitHub: https://git.io/Jvnzq              --
--                 License: The Unlicense                 --
--                  Version 5.0 (stable)                  --
--                 Released: 2023-Feb-13                  --
------------------------------------------------------------'

This was actually made by boxes (boxes -d ada-box -a c -s 60).


And now say we have a function to ask if the user wants to display that intro and I want that done by default (y*/Enter), where the interesting part is the Enter key which was mostly attributed to this answer.

sudoedit_help ()
{
    printf '%s' 'sudoedit-enhanced: Do you wish to print intro? [Y/Enter] / ? '
    IFS= read -r print_intro
    case "$print_intro" in
        ([Yy]*|'') printf '%s\n' "$script_intro"
    esac
    
    cat << EOF
Main useful info like usage...
Usage example: suvim /path/to/file1 /path/to/file2
EOF
}
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Problem #1

sudoedit_help or generally sudoedit_whatever functions will populate the shell and be messing with the standard sudoedit command in shell command completion for the user.

There is a simple solution to it, the best I came up with is prefixing the functions with an underscore like this:

_sudoedit_help ()
{
...
}

Problem #2

Before asking any additional question(s), help should be displayed. Like this:

_sudoedit_help ()
{
    cat <<EOF
sudoedit-enhanced: Usage
Example: suvi /path/to/file1 /path/to/file2
EOF

...
}

Problem #3 (cosmetic)

Sentence sudoedit-enhanced: Do you wish to print intro? [Y/Enter] / ? can be better phrased like this:

sudoedit-enhanced: Do you wish to print intro? [y/Enter/?]

Problem #4

The function name could be named better, e.g. to contain a verb. Like this:

_sudoedit_show_help ()
{
...
}

Problem #5

sudoedit-enhanced: Usage could really be better phrased. Like this: Usage of sudoedit-enhanced shell extension.

And since we are showing one single example of use, then vim shall be replaced by the basic vi to be more generic.


Refactored code

_sudoedit_show_help ()
{
    cat <<EOF
Usage of sudoedit-enhanced shell extension:
Example: suvi /path/to/file1 /path/to/file2
EOF

    printf '\n%s' 'sudoedit-enhanced: Do you wish to print intro? [y/Enter/?] '
    IFS= read -r print_intro
    case "$print_intro" in
        ([Yy]*|'') printf '%s\n' "$script_intro"
    esac
}
\$\endgroup\$

This site is temporarily in read-only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .