在bash脚本的列中打印文本

时间:2016-02-26 21:15:44

标签: bash printf

我一直在寻找答案,甚至是一个类似于我的答案,甚至一个问题,而且一直无法这样做。也许这里有人可以提供帮助。

我正在尝试从我的bash命令中打印帮助文本。我希望命令/选项在一列中,并在第二列中解释如果文本换行,则从正确的列号开始。

我已尝试过printf,我不太了解column命令。

这就是我想要的:

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/

到目前为止,我的尝试看起来像是:

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:port/

3 个答案:

答案 0 :(得分:2)

如果您发现自动文本自动换行很重要,考虑到终端窗口的宽度,那么您可能需要使用以下帮助程序脚本;我把它命名为twocolumns.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

这样称呼:

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

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/

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/

答案 1 :(得分:1)

当您打印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/"

注意:使用最小字段宽度值时,它是最小字段宽度,并且您提供的值超过该宽度,它将打印您提供的整个字符串。

此外,如果要将column2的延续移到第二行,则仍需要为第1列值提供值(空字符串或空格)。如果你查看上面的代码,输出将是:

<强>输出

$ 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 [url]17个字符,并且会超出15的格式宽度,这就是使用20代替的原因)。

使用Heredoc取代

提供格式化文本(例如 help )信息的正确方法是使用 heredoc ,例如:

cat << EOF

    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/

EOF

更容易维护,您可以完全控制heredoc sigils 之间的格式。 (上面的示例将帮助信息缩进4个空格)。

答案 2 :(得分:1)

你可以制作一个使用空格来使一切适合的功能 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/