2016-02-26 98 views
1

我一直在四处搜寻,试图找到一个类似于我的答案,甚至是一个问题,并且一直无法这样做。也许这里有人可以帮忙。在bash脚本的列中打印文本

我试图从我的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/ 
+1

如果你想要与第一格式的2列,只使用'COL2 = “%S \ n” 个'或用于连续行col2你仍然必须打印col1。例如在你的最后一个printf之前,添加'printf'$ col1“”“'实际上,你应该将你的格式合并成一个'col =”% - 15s%s“',然后将这两个值提供给一个printf调用。 –

回答

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/ 
+0

感谢所有的答案。我选择这个作为最好的答案,因为它简洁明了,完全符合我的要求。代替使用第二个脚本文件,我将代码放入函数中。 – Machtyn

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被用来代替)。

使用定界符相反

的正确方法提供格式化的文本,如帮助信息是使用定界符,如:

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 标记之间的格式。 (上面的示例将帮助信息缩进4个空格)。

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/