简体   繁体   English

在bash脚本中使用printf格式化输出

[英]formatting the output using printf in bash script

I am writing a script to display a menu at the center of the screen for the users to select the options using printf command in bash script. 我正在编写一个脚本,以在屏幕中央显示一个菜单,供用户使用bash脚本中的printf命令选择选项。

I am finding the middle column of the screen, and start printing the messages from the middle column. 我正在找到屏幕的中间列,并开始从中间列打印消息。 Hence, the output will be displayed at the center. 因此,输出将显示在中央。

Below is the code snippet 下面是代码片段

#!/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" 

When I execute the above code, the output produced is 当我执行上面的代码时,产生的输出是

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


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

The expected output should be 预期输出应为

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


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

"1. Ïnstall" and "2. Uninstall" are not printing at the middle of the screen. 屏幕中间没有打印“ 1.Ïnstall”和“ 2.卸载”。 Please help me in resolving it. 请帮助我解决它。

Thanks in Advance. 提前致谢。

Updated Script: 更新的脚本:

Thanks to all for anwering my question. 感谢大家提出我的问题。

Below is the script which gives the required output. 以下是提供所需输出的脚本。

#!/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"

Output: 输出:

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


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

Change line 34 to 将第34行更改为

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

Result: 结果:

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


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

Instead of this: 代替这个:

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

You need this: 你需要这个:

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

The - means left-align. -表示左对齐。

The question is about printing in the centre of a screen, even if the examples refer to a left aligned string (see John's answer for that...) so I will give the answer for centering a string even if it is of tangential interest for the OP 问题是关于在屏幕中央打印,即使示例引用的是左对齐的字符串(请参阅John的答案……),所以即使字符串切线感兴趣,我也将给出居中的答案OP

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

Comment for the OP 对OP的评论

In your code you reference $COLUMNS , look at this: 在您的代码中,引用$COLUMNS ,看一下:

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

and use tput to find the number of columns 并使用tput查找列数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM