簡體   English   中英

在bash腳本中使用printf格式化輸出

[英]formatting the output using printf in bash script

我正在編寫一個腳本,以在屏幕中央顯示一個菜單,供用戶使用bash腳本中的printf命令選擇選項。

我正在找到屏幕的中間列,並開始從中間列打印消息。 因此,輸出將顯示在中央。

下面是代碼片段

#!/bin/bash

INSTALLATION_HEADER=" Installater Options "

COLS=$(tput cols)

print_header ()
{
        local equal=()
        local title="$1"
        local mid=$(((${#title}+$COLS)/2))
        for (( i=0;  $i < ${#title} ; i=$(($i+1)) ))
        do
                local hypen+="-"
        done
        printf "%*s\n" $mid "$title"
        printf "%*s\n" $mid "$hypen"
        echo ""
        echo ""
}

print_install_options ()
{
        local title=${1}
        local mid=$(((${#title}+$COLS)/2))
        for (( i=0;  $i < ${#title} ; i=$(($i+1)) ))
        do
                local hypen+="-"
        done

        printf "%*s\n" $mid "$hypen"
        for i in "${install_options[@]}" ;
        do
                printf "%*s\n" $mid "$i"
        done
        printf "%*s\n" $mid "$hypen"
}

install_options=("1. Install" "2. Uninstall")
print_header "$INSTALLATION_HEADER"
print_install_options "$INSTALLATION_HEADER" 

當我執行上面的代碼時,產生的輸出是

 Installater Options
---------------------


---------------------
           1. Install
         2. Uninstall
---------------------

預期輸出應為

 Installater Options
---------------------


---------------------
1. Install
2. Uninstall
---------------------

屏幕中間沒有打印“ 1.Ïnstall”和“ 2.卸載”。 請幫助我解決它。

提前致謝。

更新的腳本:

感謝大家提出我的問題。

以下是提供所需輸出的腳本。

#!/bin/bash

INSTALLATION_HEADER=" Installater Options "

COLS=$(tput cols)

print_header ()
{
        local equal=()
        local title="$1"
        local mid=$(((${#title}+$COLS)/2))
        for (( i=0;  $i < ${#title} ; i=$(($i+1)) ))
        do
                local hypen+="-"
        done
        printf "%*s\n" $mid "$title"
        printf "%*s\n" $mid "$hypen"
        echo ""
        echo ""
}

print_install_options ()
{
        local title=${1}
        local length=$(((${#title}+$COLS)))
        local mid=$(((${#title}+$COLS)/2))
        for (( i=0;  $i < ${#title} ; i=$(($i+1)) ))
        do
                local hypen+="-"
        done

        printf "%*s\n" $mid "$hypen"
        for i in "${install_options[@]}" ;
        do
                printf "%*s%s" $((${mid}-${#title})) "" "|"
                printf "%s" "  $i  "
                printf "%*s\n" $((${#title}-${#i}-5)) "|"
        done
        printf "%*s\n" $mid "$hypen"
}

install_options=("1. Install" "2. Uninstall")
print_header "$INSTALLATION_HEADER"
print_install_options "$INSTALLATION_HEADER"

輸出:

 Installater Options
---------------------


---------------------
|  1. Install       |
|  2. Uninstall     |
---------------------

將第34行更改為

printf "%-*s%s\n" $((${mid}-${#title})) " " "$i"

結果:

                                 Installater Options 
                                ---------------------


                                ---------------------
                                1. Install
                                2. Uninstall
                                ---------------------

代替這個:

            printf "%*s\n" $mid "$i"

你需要這個:

            printf "%-*s\n" $mid "$i"

-表示左對齊。

問題是關於在屏幕中央打印,即使示例引用的是左對齊的字符串(請參閱John的答案……),所以即使字符串切線感興趣,我也將給出居中的答案OP

centre () {
    nc=`tput cols`; l=${#1}
    printf "%$(( (nc-l)/2 ))s%s\n" " " "$1"
    }

對OP的評論

在您的代碼中,引用$COLUMNS ,看一下:

% cat aaaa
echo pippo $COLUMNS
tput cols
% source aaaa
pippo 117
117
% sh aaaa
pippo
117
% 

並使用tput查找列數

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM