如何在解析内容以显示反馈的同时隐藏脚本的输出?
How do I hide my script's output while also parsing content to display feedback?
我有以下 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 }')
...
end_du=$(du -h | grep "$filesystem" | awk '{print }')
echo "Volume has been expanded from $start_du to $end_du"
扩展音量的中间步骤只需要将 stdout and/or stderr 转移到 /dev/null 以抑制输出,例如pvresize /dev/sda3 >/dev/null 2>&1
转移两者(离开 2>&1
仍然会报告错误)。
我有以下 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 }')
...
end_du=$(du -h | grep "$filesystem" | awk '{print }')
echo "Volume has been expanded from $start_du to $end_du"
扩展音量的中间步骤只需要将 stdout and/or stderr 转移到 /dev/null 以抑制输出,例如pvresize /dev/sda3 >/dev/null 2>&1
转移两者(离开 2>&1
仍然会报告错误)。