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

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

题目如下

有一个文本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
}

# 传入当前窗口列值
# 参数1
width=$1
file=./a.txt

while read line
do
        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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值