在bash脚本中打印列中的文本

在bash脚本中打印列中的文本,bash,printf,Bash,Printf,我一直在四处寻找,试图找到一个答案,甚至是一个与我类似的问题,但一直无法找到答案。也许这里有人可以帮忙 myscript.sh Execute without arguments to get a menu of options defining the environment. myscr

我一直在四处寻找,试图找到一个答案,甚至是一个与我类似的问题,但一直无法找到答案。也许这里有人可以帮忙

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
我正在尝试从bash命令打印帮助文本。我希望命令/选项在一列中,解释在第二列中,如果文本换行,则从正确的列号开始

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
我尝试了
printf
,但不太理解
列的命令

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
以下是我想要的:

myscript.sh         Execute without arguments to get a menu
                    of options defining the environment.
myscript.sh [url]   Add [url] to specify desired server
                    environment with which to run the script.
                    ex: http://the.server.domain.tld:port/
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
到目前为止,我的尝试如下:

col1="%-15s"
col2="%15s\n"
printf "$col1" "myscript.sh"
printf "$col2" "Execute without arguments to get a menu of options defining the environment."
printf "$col1" "myscript.sh [url]"
printf "$col2" "Add [url] to specify desired server environment with which to run the script."
printf "$col2" "ex: http://the.server.domain.tld:port/"
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
但它最终看起来像

myscript.sh         Execute without arguments to get a menu
of options defining the environment.
myscript.sh [url]   Add [url] to specify desired server 
environment with which to run the script.
ex: http://the.server.domain.tld:port/
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/

打印1个格式化列和以下字符串时,不需要2种单独的格式,只需在单个格式字符串中提供这两种格式:

#!/bin/bash

cols="%-20s%s\n"
printf "$cols" "myscript.sh" "Execute without arguments to get a menu of options."
printf "$cols" "myscript.sh [url]" "Add [url] to specify desired server."
printf "$cols" " " "ex: http://the.server.domain.tld:port/"
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
注意:当使用最小字段宽度值时,它是一个最小字段宽度,并且您提供的值超过该宽度,它将打印您提供的整个字符串

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
此外,如果要将第2列的续行移到第二行,仍然需要为第1列的值提供一个值(空字符串或空格)。如果查看上面的代码,输出将是:

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
输出

$ bash prncols.sh
myscript.sh         Execute without arguments to get a menu of options.
myscript.sh [url]   Add [url] to specify desired server.
                    ex: http://the.server.domain.tld:port/
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
注意:
myscript.sh[url]
17
个字符,将超过您的格式宽度
15
,这就是改用
20
的原因)

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
改用Heredoc

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
提供格式化文本(如帮助信息)的正确方法是使用heredoc,例如:

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/

cat当您打印1个格式化列和以下字符串时,不需要两种单独的格式,只需在单个格式字符串中提供这两种格式:

#!/bin/bash

cols="%-20s%s\n"
printf "$cols" "myscript.sh" "Execute without arguments to get a menu of options."
printf "$cols" "myscript.sh [url]" "Add [url] to specify desired server."
printf "$cols" " " "ex: http://the.server.domain.tld:port/"
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
注意:当使用最小字段宽度值时,它是一个最小字段宽度,并且您提供的值超过该宽度,它将打印您提供的整个字符串

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
此外,如果要将第2列的续行移到第二行,仍然需要为第1列的值提供一个值(空字符串或空格)。如果查看上面的代码,输出将是:

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
输出

$ bash prncols.sh
myscript.sh         Execute without arguments to get a menu of options.
myscript.sh [url]   Add [url] to specify desired server.
                    ex: http://the.server.domain.tld:port/
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
注意:
myscript.sh[url]
17
个字符,将超过您的格式宽度
15
,这就是改用
20
的原因)

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
改用Heredoc

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
提供格式化文本(如帮助信息)的正确方法是使用heredoc,例如:

    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/

cat如果考虑到终端窗口的宽度,您发现文本自动换行很重要,那么您可能需要使用以下帮助脚本;我把它命名为
twoocolumns.sh

#!/bin/bash
tabstop=$1
cols=$(tput cols)
paste <(echo "$2" | fold -sw$((tabstop-1))) <(echo "$3" | fold -sw$((cols-tabstop-1))) | expand -t$tabstop
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
80列终端中的输出:

myscript.sh         Execute without arguments to get a menu of options 
                    defining the environment.
myscript.sh [url]   Add [url] to specify desired server environment with which 
                    to run the script.
                    ex: http://the.server.domain.tld:port/
myscript.sh         Execute without arguments to get a 
                    menu of options defining the 
                    environment.
myscript.sh [url]   Add [url] to specify desired server 
                    environment with which to run the 
                    script.
                    ex: http://the.server.domain.tld:port/
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
60列终端中的输出:

myscript.sh         Execute without arguments to get a menu of options 
                    defining the environment.
myscript.sh [url]   Add [url] to specify desired server environment with which 
                    to run the script.
                    ex: http://the.server.domain.tld:port/
myscript.sh         Execute without arguments to get a 
                    menu of options defining the 
                    environment.
myscript.sh [url]   Add [url] to specify desired server 
                    environment with which to run the 
                    script.
                    ex: http://the.server.domain.tld:port/
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/

如果考虑到终端窗口的宽度,您发现让文本自动换行很重要,那么您可能需要使用以下帮助器脚本;我把它命名为
twoocolumns.sh

#!/bin/bash
tabstop=$1
cols=$(tput cols)
paste <(echo "$2" | fold -sw$((tabstop-1))) <(echo "$3" | fold -sw$((cols-tabstop-1))) | expand -t$tabstop
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
80列终端中的输出:

myscript.sh         Execute without arguments to get a menu of options 
                    defining the environment.
myscript.sh [url]   Add [url] to specify desired server environment with which 
                    to run the script.
                    ex: http://the.server.domain.tld:port/
myscript.sh         Execute without arguments to get a 
                    menu of options defining the 
                    environment.
myscript.sh [url]   Add [url] to specify desired server 
                    environment with which to run the 
                    script.
                    ex: http://the.server.domain.tld:port/
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/
60列终端中的输出:

myscript.sh         Execute without arguments to get a menu of options 
                    defining the environment.
myscript.sh [url]   Add [url] to specify desired server environment with which 
                    to run the script.
                    ex: http://the.server.domain.tld:port/
myscript.sh         Execute without arguments to get a 
                    menu of options defining the 
                    environment.
myscript.sh [url]   Add [url] to specify desired server 
                    environment with which to run the 
                    script.
                    ex: http://the.server.domain.tld:port/
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/

您可以创建一个函数,该函数使用空格使所有内容都适合。
包含testdata的函数:

function myprint {
   firstline=0    
   while read -r line; do
      if [[ "${firstline}" -eq 0 ]]; then
         header="$1"                     
         firstline=1                     
      else                               
         header=" "                      
      fi                                 
      printf "%-25s%s\n" "${header}" "${line}" 
   done <<< "$(fold -w30 -s <<< $2)"          
}                                              

h1="myscript.sh"
t1="Execute without arguments to get a menu of options defining the environment.  myscript.sh [url]   Add [url] to specify desired server environment with which to run the script.  ex: http://the.server.domain.tld:port/"  
h2="myscript.sh [url]"                                                                                         
t2="Add [url] to specify desired server environment with which to run the script.  ex: http://the.server.domain.tld:port/"                                                                                                    
myprint "$h1" "$t1"                                                                                            
myprint "$h2" "$t2"   
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/

您可以创建一个函数,该函数使用空格使所有内容都适合。
包含testdata的函数:

function myprint {
   firstline=0    
   while read -r line; do
      if [[ "${firstline}" -eq 0 ]]; then
         header="$1"                     
         firstline=1                     
      else                               
         header=" "                      
      fi                                 
      printf "%-25s%s\n" "${header}" "${line}" 
   done <<< "$(fold -w30 -s <<< $2)"          
}                                              

h1="myscript.sh"
t1="Execute without arguments to get a menu of options defining the environment.  myscript.sh [url]   Add [url] to specify desired server environment with which to run the script.  ex: http://the.server.domain.tld:port/"  
h2="myscript.sh [url]"                                                                                         
t2="Add [url] to specify desired server environment with which to run the script.  ex: http://the.server.domain.tld:port/"                                                                                                    
myprint "$h1" "$t1"                                                                                            
myprint "$h2" "$t2"   
    myscript.sh              Execute without arguments to
                             get a menu of options
                             defining the environment.
                             myscript.sh [url] Add [url]
                             to specify desired server
                             environment with which to run
                             the script. ex:
                             http://the.server.domain.tld:p
                             ort/
    myscript.sh [url]        Add [url] to specify desired
                             server environment with which
                             to run the script. ex:
                             http://the.server.domain.tld:p
                             ort/

如果您想要第一个格式化的两列,只需使用
col2=“%s\n”
或对于col2中的续行,您仍然必须打印col1。e、 g.在上次printf之前,添加
printf“$col1”“”
事实上,您可能应该将您的格式组合成一个
col=“%-15s%s”
,然后将这两个值提供给一个printf调用。如果您想要两个第一个格式化的列,只需使用
col2=“%s\n”
或对于col2中的续行,仍然必须打印col1。e、 g.在最后一次printf之前,添加
printf“$col1”“”
事实上,您可能应该将您的格式合并到一个
col=“%-15s%s”
中,然后为一个printf调用提供这两个值。谢谢大家的回答。我选择这个作为最好的答案,因为它很简洁,并且完全符合我的要求。我没有使用第二个脚本文件,而是将代码放入函数中。谢谢大家的回答。我选择这个作为最好的答案,因为它很简洁,并且完全符合我的要求。我没有使用第二个脚本文件,而是将代码放入函数中。这是一个非常简洁的函数,谢谢。不幸的是,它不适用于颜色,因为fold将颜色代码作为额外字符读取。。。我会在这里发布一个答案,如果我找到一个扩展的解决方案,颜色工作。我理解这个问题,但不知道一个简单的解决办法。当你对颜色感兴趣时,你可能会喜欢答案,但这在这里没有帮助。这是一个非常简洁的函数,谢谢。不幸的是,它不适用于颜色,因为fold将颜色代码作为额外字符读取。。。我会在这里发布一个答案,如果我找到一个扩展的解决方案,颜色工作。我理解这个问题,但不知道一个简单的解决办法。当你对颜色感兴趣时,你可能会喜欢答案,但这在这里没有帮助。