D
D
Dmitry Aitkulov2016-08-11 14:08:15
bash
Dmitry Aitkulov, 2016-08-11 14:08:15

Why doesn't for loop work in bash?

Good afternoon! I need to write a script to check the length of an array in a loop, but something doesn't work (the actual script itself:

#!/bin/bash
#останавливаем службу
systemctl stop libvirt-guests
#объявляю массив и складываю в него результаты команды
unset array
array=(`virsh list | grep running | awk '{print $2}'`)
lenArr=${#array[@]}
#вот тут необходимо в бесконечном цикле проверять пока длина массива не будет равна 0
#и только тогда выполнить echo.
state=false
while [ state = true ]
        do
        if  [ "$lenArr" -eq 0 ] ; then
                echo "shutdown success"
                state=true
                break
        else
                continue
        fi
done

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Danil Biryukov-Romanov, 2016-08-11
@Scarfase1989

state=false
while [ state != true ]
        do
        if  [ "$lenArr" -eq 0 ] ; then
                echo "shutdown success"
                state=true
                break
        else
                continue
        fi
done

You set state equal to false, and then compare it with true, you have to check for equality, but you need to check for NOT equality by logic :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question