我有以下bash脚本,用于扩展输出大量信息的卷。
我想隐藏脚本命令中的所有输出,只在脚本开始时捕获卷的大小,然后在脚本结束时捕获卷的大小,并在更改后输出一行。
脚本:
#!/bin/bash
du -h
printf "Fix\nFix\n" |parted ---pretend-input-tty /dev/sda print
parted /dev/sda resizepart 3 100%
pvresize /dev/sda3
lvextend -l +100%FREE /dev/SystemVG/root
xfs_growfs /dev/SystemVG/root
du -h
开始时du -h命令输出以下内容:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/SystemVG-root 12G 6.4G 5.4G 55% /
devtmpfs 858M 0 858M 0% /dev
tmpfs 870M 0 870M 0% /dev/shm
tmpfs 870M 9.4M 860M 2% /run
tmpfs 870M 0 870M 0% /sys/fs/cgroup
/dev/sda2 1.9G 49M 1.8G 3% /boot
/dev/sda1 71M 12M 60M 16% /boot/efi
tmpfs 174M 0 174M 0% /run/user/0
最后的du -h命令输出以下内容:
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/SystemVG-root 21G 6.4G 14G 32% /
devtmpfs 858M 0 858M 0% /dev
tmpfs 870M 0 870M 0% /dev/shm
tmpfs 870M 9.4M 860M 2% /run
tmpfs 870M 0 870M 0% /sys/fs/cgroup
/dev/sda2 1.9G 49M 1.8G 3% /boot
/dev/sda1 71M 12M 60M 16% /boot/efi
tmpfs 174M 0 174M 0% /run/user/0
我试图通过输出实现以下内容:
[root@system]# ./expandvolume.sh
Volume has been expanded from 12G to 21G.
[root@system]#
如果可以提供文件系统名称,则可以像这样处理du
输出:
filesystem='SystemVG'
start_du=$(du -h | grep "$filesystem" | awk '{print $2}')
...
end_du=$(du -h | grep "$filesystem" | awk '{print $2}')
echo "Volume has been expanded from $start_du to $end_du"
扩展音量的中间步骤只需要将stdout和/或stderr转移到/ dev / null即可抑制输出,例如pvresize /dev/sda3 >/dev/null 2>&1
转移了两者(放弃2>&1
仍会报告错误)。