小编Iac*_*hus的帖子

Bash 脚本将财富/文本从标准输入/管道居中

我在 python3 中使用一个小脚本在控制台中显示居中的命运,你能建议我如何在纯 bash 中做到这一点吗?

文件:center.python3

#!/usr/bin/env python3

import sys, os

linelist = list(sys.stdin)

# gets the biggest line
biggest_line_size = 0
for line in linelist:
    line_lenght = len(line.expandtabs())
    if line_lenght > biggest_line_size:
        biggest_line_size = line_lenght

columns = int(os.popen('tput cols', 'r').read())
offset = biggest_line_size / 2
perfect_center = columns / 2
padsize =  int(perfect_center - offset)
spacing = ' ' * padsize # space char

text = str()
for line in linelist:
    text += (spacing + line)

divider = spacing …
Run Code Online (Sandbox Code Playgroud)

bash

5
推荐指数
2
解决办法
1233
查看次数

标签 统计

bash ×1