From 3c1d81d1fe1b68b2ed282e746f7573d953ad5da5 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sat, 2 Apr 2022 14:00:47 +0300 Subject: .local/bin: bash best practices Additionally, virsh -q gets rid of that stupid empty line virsh for some reason prints out every time. --- %HOME%/.local/bin/vagrant-update-all-boxes.sh | 9 +++---- %HOME%/.local/bin/virsh-cleanup.sh | 39 ++++++++------------------- 2 files changed, 14 insertions(+), 34 deletions(-) (limited to '%HOME%/.local') diff --git a/%HOME%/.local/bin/vagrant-update-all-boxes.sh b/%HOME%/.local/bin/vagrant-update-all-boxes.sh index 6d7e5a8..6f4b61c 100755 --- a/%HOME%/.local/bin/vagrant-update-all-boxes.sh +++ b/%HOME%/.local/bin/vagrant-update-all-boxes.sh @@ -8,7 +8,7 @@ # This script updates all Vagrant boxes. set -o errexit -o nounset -o pipefail -shopt -s inherit_errexit +shopt -s inherit_errexit lastpipe dump() { local msg @@ -45,13 +45,10 @@ update_box_from_line() { } update_all_boxes() { - local output - output="$( vagrant box outdated --global | grep -F outdated )" - local line - while IFS= read -r line; do + vagrant box outdated --global | grep -F outdated | while IFS= read -r line; do update_box_from_line "$line" - done <<< "$output" + done } clean_old_boxes() { diff --git a/%HOME%/.local/bin/virsh-cleanup.sh b/%HOME%/.local/bin/virsh-cleanup.sh index d85560f..efdfa5e 100755 --- a/%HOME%/.local/bin/virsh-cleanup.sh +++ b/%HOME%/.local/bin/virsh-cleanup.sh @@ -8,7 +8,7 @@ # This script destroys all libvirt resources. set -o errexit -o nounset -o pipefail -shopt -s inherit_errexit +shopt -s inherit_errexit lastpipe CONNECT="${CONNECT:=qemu:///system}" POOL="${POOL:=default}" @@ -24,7 +24,7 @@ dump() { } run_virsh() { - virsh -c "$CONNECT" "$@" + virsh -q -c "$CONNECT" "$@" } list_domains() { @@ -32,24 +32,18 @@ list_domains() { } list_networks() { - local output - output="$( run_virsh net-list --all --name )" - local name - while IFS= read -r name; do + run_virsh net-list --all --name | while IFS= read -r name; do [ "$name" = default ] && continue echo "$name" - done <<< "$output" + done } list_volumes() { - local output - output="$( run_virsh vol-list "$POOL" | tail -n +3 | tr -s ' ' )" - local name - while IFS=' ' read -r name _; do + run_virsh vol-list "$POOL" | tr -s ' ' | while IFS=' ' read -r name _; do echo "$name" - done <<< "$output" + done } remove_domain() { @@ -79,22 +73,11 @@ remove_volume() { } main() { - local output item - - output="$( list_domains )" - if [ -n "$output" ]; then - while IFS= read -r item; do remove_domain "$item"; done <<< "$output" - fi - - output="$( list_volumes )" - if [ -n "$output" ]; then - while IFS= read -r item; do remove_volume "$item"; done <<< "$output" - fi - - output="$( list_networks )" - if [ -n "$output" ]; then - while IFS= read -r item; do remove_network "$item"; done <<< "$output" - fi + local item + + list_domains | while IFS= read -r item; do remove_domain "$item"; done + list_volumes | while IFS= read -r item; do remove_volume "$item"; done + list_networks | while IFS= read -r item; do remove_network "$item"; done } main -- cgit v1.2.3