您的位置:首页 > 运维架构 > Shell

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

2017-09-06 19:29 561 查看

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

题目如下

有一个文本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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐