From: Michal Privoznik <[email protected]>
In Bash, the following code does not do what you think it does:
func() {
local var=$(false)
echo $?
}
Here, '0' is echoed even though false is designed to exit with a
non-zero code. This is because in fact the last executed command
is 'local' not 'false' and thus '$?' contains zero as in "yeah,
the variable is successfully declared" [1]. In our libvirt-guest
shell script, there are a few places like this. Fix them.
1: bash_builtins(1)
Signed-off-by: Michal Privoznik <[email protected]>
---
tools/libvirt-guests.sh.in | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tools/libvirt-guests.sh.in b/tools/libvirt-guests.sh.in
index f2db1282ad..e05bfdba61 100644
--- a/tools/libvirt-guests.sh.in
+++ b/tools/libvirt-guests.sh.in
@@ -106,8 +106,9 @@ test_connect()
list_guests() {
local uri="$1"
local persistent="$2"
- local list="$(run_virsh_c "$uri" list --uuid $persistent)"
+ local list
+ list="$(run_virsh_c "$uri" list --uuid $persistent)"
if [ $? -ne 0 ]; then
RETVAL=1
return 1
@@ -482,7 +483,8 @@ stop() {
eval_gettext "Running guests on \$uri URI: "
- local list="$(list_guests "$uri")"
+ local list
+ list="$(list_guests "$uri")"
if [ $? -eq 0 ]; then
local empty=true
for uuid in $list; do
@@ -498,7 +500,8 @@ stop() {
fi
if "$persistent_only"; then
- local transient="$(list_guests "$uri" "--transient")"
+ local transient
+ transient="$(list_guests "$uri" "--transient")"
if [ $? -eq 0 ]; then
local empty="true"
local uuid=
--
2.52.0