每次我在终端中使用 watch 命令时,输出都会从屏幕开始显示,这看起来有点尴尬。我希望让 watch 在屏幕中心显示输出。
$ watch -n 1 du -hs "*.part"
52K Prince of Tennis Episode 100 English Subbed Online - Chia-Anime.mp4.part
64M Prince of Tennis Episode 92 English Subbed Online - Chia-Anime.mp4.part
53M Prince of Tennis Episode 93 English Subbed Online - Chia-Anime.mp4.part
23M Prince of Tennis Episode 94 English Subbed Online - Chia-Anime.mp4.part
13M Prince of Tennis Episode 95 English Subbed Online - Chia-Anime.mp4.part
24K Prince of Tennis Episode 96 English Subbed Online - Chia-Anime.mp4.part
12K Prince of Tennis Episode 97 English Subbed Online - Chia-Anime.mp4.part
40K Prince of Tennis Episode 98 English Subbed Online - Chia-Anime.mp4.part
36K Prince of Tennis Episode 99 English Subbed Online - Chia-Anime.mp4.part
我希望这个输出从屏幕中心开始出现。
答案1
您可以组合watch
和printf
来产生所需的输出。
以下是示例脚本:
#!/bin/bash
columns="$(tput cols)"
du -hs *.part | while read i; do
printf "%*s\n" $(( (${#i} + columns) / 2)) "$i"
done
使用如下名称保存它script.sh
并运行它:
watch bash script.sh
输出为:
4.0K myfile
4.0K anotherfile
54M coreutils-8.25
28K coreutils_8.25-2ubuntu2.debian.tar.xz
4.0K coreutils_8.25-2ubuntu2.dsc
5.5M coreutils_8.25.orig.tar.xz
这是另一个脚本:
#!/bin/bash
for i in *.part
do
title=`du -hs "$i"`
echo -e "\t\t\t\t$title\n"
done
运行:
watch bash myscript.sh
它将显示如下结果:
4.0K myfile
4.0K anotherfile
54M coreutils-8.25
28K coreutils_8.25-2ubuntu2.debian.tar.xz
4.0K coreutils_8.25-2ubuntu2.dsc
5.5M coreutils_8.25.orig.tar.xz
使用\t
尽可能多的次数来获得您想要的结果。