0

I am writing a help function for my bash script generate_ssh_keys.sh:

#!/usr/bin/env bash
################################################################################
# Help                                                                         #
################################################################################

Help()
{
    # Display help
    echo "This script aims to generate id_rsa key and copy this key to remote servers."
    echo
    echo "Syntax: generate_ssh_keys.sh [-i|-e|-H]"
    echo "optional arguments:"
    format="%-10s\t%s\n"

    printf "$format" "-i, --id" "Path to ssh rsa key file. If this flag is not specified, this script will use ~/.ssh/id_rsa as the key."
    printf "$format" "-e, --email" "Your email to generate ssh-keygen."
    printf "$format" "-H, --host" "Hostname. If this flag is not specified, the script copy the key to all the hosts in your ~/.ssh/config"
}
# GET OPTS
while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
        -h|--help ) # display Help
            Help
            exit;;
        * ) # temporarily ignore other flags to test Help function
            echo "Illegal option"
            echo
            Help
            exit 3
            ;;
    esac
done

When I run my script: ./generate_ssh_keys.sh -h, it was well displayed with a wide terminal but when the terminal shrank, the help message display like this:

This script aims to generate id_rsa key and copy this key to remote se
rvers.

Syntax: generate_ssh_keys.sh [-i|-e|-H]
optional arguments:
-i, --id    Path to ssh rsa key file. If this flag is not specifie
d, this script will use ~/.ssh/id_rsa as the key.
-e, --email Your email to generate ssh-keygen.
-H, --host  Hostname. If this flag is not specified, the script co
py the key to all the hosts in your ~/.ssh/config

There are two problems I would like to overcome to make the help function more readable in small window:

(1) Auto break long line based on words, not letters (I mean break at space, not at a random letter)

(2) Apply tab for the new line created.

My expected result in narrow terminal is:

This script aims to generate id_rsa key and copy this key to remote
servers.

Syntax: generate_ssh_keys.sh [-i|-e|-H]
optional arguments:
-i, --id    Path to ssh rsa key file. If this flag is not
            specified, this script will use ~/.ssh/id_rsa as the
            key.
-e, --email Your email to generate ssh-keygen.
-H, --host  Hostname. If this flag is not specified, the script
            copy the key to all the hosts in your ~/.ssh/config

I have tried with echo and column -t (here) but I didn't find out the solution:

echo -e "-i, --id\tPath to ssh rsa key file. If this flag is not specified, this script will use ~/.ssh/id_rsa as the key."
# pipe to column -t
echo "-i, --id;Path to ssh rsa key file. If this flag is not specified, this script will use ~/.ssh/id_rsa as the key." | column -t -s";"

I don't know if it is possible to solve (1) or not. I tried printing some help messages such as of awk, a part of it displays like this even in a wide terminal:

To report bugs, see node `Bugs' in `gawk.info'
which is section `Reporting Problems and Bugs' in the
printed version.  This same information may be found at
https://www.gnu.org/software/gawk/manual/html_node/Bugs.html.
PLEASE do NOT try to report bugs by posting in comp.lang.awk,
or by using a web forum such as Stack Overflow.

The lines seem to be manually broken.

Please help me if you know any solutions to this. Thank you very much.

Hoa Nguyen
  • 470
  • 6
  • 15

1 Answers1

1

I think you're looking for something a little more dynamic, but I think the best solution I've used is to simply format the text how you want it to look. A really easy step is to just set the help text to a variable and then print that. You can use a heredoc for some additional functionality as well. Here's what I refactored your script to:

#!/usr/bin/env bash
################################################################################
# Help                                                                         #
################################################################################
Help()
{
    show_help="
    This script aims to generate id_rsa key and copy this key 
    to remote servers.

    Syntax: generate_ssh_keys.sh [-i|-e|-H]
            optional arguments:

    -i, --id        Path to ssh rsa key file. If this 
                flag is not specified, this script will 
            use ~/.ssh/id_rsa as the key.
    -e, --email     Your email to generate ssh-keygen.
    -H, --host      Hostname. If this flag is not specified, 
                the script copy the key to all the hosts 
            in your ~/.ssh/config
"
    echo "$show_help" 

}
# GET OPTS
while [[ $# -gt 0 ]]; do
    key="$1"
    case $key in
        -h|--help ) # display Help
            Help
            exit;;
        * ) # temporarily ignore other flags to test Help function
            echo "Illegal option"
            echo
            Help
            exit 3
            ;;
    esac
done

And the result:

$ ./help.sh -h

    This script aims to generate id_rsa key and copy this key 
    to remote servers.

    Syntax: generate_ssh_keys.sh [-i|-e|-H]
            optional arguments:

    -i, --id        Path to ssh rsa key file. If this 
                    flag is not specified, this script will 
                    use ~/.ssh/id_rsa as the key.
    -e, --email     Your email to generate ssh-keygen.
    -H, --host      Hostname. If this flag is not specified, 
                    the script copy the key to all the hosts 
                    in your ~/.ssh/config
iamwpj
  • 180
  • 7
  • Thank you so much, @iamwpj , your solution is better. It seems to be impossible to auto brake line at space. – Hoa Nguyen May 14 '20 at 11:13