打印格式bash脚本:是否可以在空格处自动中断长行并仍然为新的换行符应用制表符?

问题描述 投票:0回答:1

我正在为我的bash脚本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

当我运行脚本:./generate_ssh_keys.sh -h时,它在宽阔的终端上显示良好,但是当终端收缩时,帮助消息显示如下:

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

我想克服两个问题,以使帮助功能在小窗口中更具可读性:

((1)根据单词而不是字母自动中断长行(我的意思是在space处中断,而不是在随机字母处中断]

((2)将tab应用于创建的新行。

我在狭窄终端中的预期结果是:

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

我已经尝试过使用echocolumn -there),但没有找到解决方法:

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";"

我不知道是否有可能解决(1)。我尝试打印一些帮助消息,例如awk,即使在较宽的终端中,它的一部分也显示如下:

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.

这些线似乎是手动断开的。

如果您对此有任何解决方案,请帮助我。非常感谢。

bash tabs printf line-breaks
1个回答
0
投票

我认为您正在寻找更具动态性的东西,但是我认为我使用的最佳解决方案是简单地设置文本的外观。一个非常简单的步骤是将帮助文本设置为变量,然后将其打印出来。您也可以将heredoc用于某些其他功能。这是我将您的脚本重构为:

#!/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

结果:

$ ./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
© www.soinside.com 2019 - 2024. All rights reserved.