Issue
I have the following bash script that expands a volume which outputs a lot of information.
I would like to hide all output from the commands of the script, capture only the size of the volume at the start of the script, then at the end of the script, and output a single line with the change.
The script:
#!/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
The du -h command at the start outputs the following:
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
The du -h command at the end outputs the following:
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
What I'm trying to achieve as output is the following:
[root@system]# ./expandvolume.sh
Volume has been expanded from 12G to 21G.
[root@system]#
Solution
If you're OK with providing the filesystem name, you could deal the du
output like this:
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"
The intermediate steps to expand the volume just need stdout and/or stderr to be diverted to /dev/null to suppress output, e.g. pvresize /dev/sda3 >/dev/null 2>&1
diverts both (leaving off 2>&1
will still report errors).
Answered By - Lorccan Answer Checked By - David Goodson (WPSolving Volunteer)