2016-02-26 35 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/