Reputation: 17920
When I run the following command:
if [ $(vboxmanage list vms | grep -c "all-in-one-1.2.7-wizard") > 0 ]; then
echo 'yes'
else
echo 'no'
fi
a 0
file is created in the current directory:
$ ll
...
-rw-rw-r-- 1 abc abc 0 Nov 19 17:33 0
Any idea why is this happening?
Upvotes: 0
Views: 49
Reputation: 290025
You are not doing an integer comparison, but redirecting the output of the command $()
into 0
, so a file with 0
name is created.
Also, the current "resolution" of the if-condition is based on the result of the execution of the condition. If it is successful, then it executes the condition.
Instead, use -gt
(g
reater t
han):
if [ $(vboxmanage list vms | grep -c "all-in-one-1.2.7-wizard") -gt 0 ]; then
^^
echo 'yes'
else
echo 'no'
fi
You can make sure this is the behaviour by doing >7
or whatever and see a file 7
(or whatever) gets created.
When would the else
condition be executed? If you couldn't redirect:
$ [ $(ls /root) > 3 ] && echo "yes" || echo "no"
ls: cannot open directory /root: Permission denied
no
See Numeric comparison for the list of all the possible integer comparisons.
Upvotes: 6