If statement in Bash creates weird 0 file -
when run following command:
if [ $(vboxmanage list vms | grep -c "all-in-one-1.2.7-wizard") > 0 ]; echo 'yes' else echo 'no' fi
a 0
file created in current directory:
$ ll ... -rw-rw-r-- 1 abc abc 0 nov 19 17:33 0
any idea why happening?
you not doing integer comparison, redirecting output of command $()
0
, file 0
name created.
also, current "resolution" of if-condition based on result of execution of condition. if successful, executes condition.
instead, use -gt
(g
reater t
han):
if [ $(vboxmanage list vms | grep -c "all-in-one-1.2.7-wizard") -gt 0 ]; ^^ echo 'yes' else echo 'no' fi
you can make sure behaviour doing >7
or whatever , see file 7
(or whatever) gets created.
when else
condition executed? if couldn't redirect:
$ [ $(ls /root) > 3 ] && echo "yes" || echo "no" ls: cannot open directory /root: permission denied no
see numeric comparison list of possible integer comparisons.
Comments
Post a Comment