批处理实现文本内容居中显示 -shell

来源:互联网 发布:淘宝详情页html代码 编辑:程序博客网 时间:2024/05/15 19:38

批处理实现文本内容居中显示

题目如下

有一个文本a.txt

[Promise don’t come easy]
I should have known all along.
There was something wrong.
I just never read between the lines.
Then I woke up one day and found you on your way.
Leaving nothing but my heart behind.
What can I do to make it up to you.
Promises don’t come easy.
But tell me if there’s a way to bring you back home to stay.
Well I’d promises anything to you.
I’ve been walkin’ around with my head hanging down.
Wondrin’ what I’m gonna do.
‘Cause when you walked out that door.

要求,CMD原始窗口中,不调节窗口大小,居中显示文本内容。输出如下:

                      [Promise don't come easy]                    I should have known all along.                      There was something wrong.                 I just never read between the lines.          Then I woke up one day and found you on your way.                 Leaving nothing but my heart behind.                 What can I do to make it up to you.                      Promises don't come easy.     But tell me if there's a way to bring you back home to stay.                  Well I'd promises anything to you.         I've been walkin' around with my head hanging down.                     Wondrin' what I'm gonna do.                'Cause when you walked out that door.

编写代码

#! /bin/bash# 输出具体数量空格后,输出内容# 参数1:空格数量# 参数2:内容function printll() {        i=1        while [ ${i} -lt $1 ]        do                let i++                printf " "        done        echo $2}# 传入当前窗口列值# 参数1width=$1file=./a.txtwhile read linedo        len=${#line}        let w=(${width}-${len})/2        # 这里一定要加上引号,参数有空格会当做多个参数传入        printll ${w} "${line}"done < ${file}

执行命令

./test.sh ${COLUMNS}

ps:窗口列值:${COLUMNS},宽值:${LINES}

运行结果

这里写图片描述

参考文档

样式输出:http://blog.csdn.net/fdipzone/article/details/9993961

原创粉丝点击