There are a couple of errors.
- you echo do_exit before you set it to 1
- you have
fi & sleep 5;
which makes the if statement a child process, thus you can't use the value in the parent process -gt
is for numeric comparisment, not strings.
Below should work for you.
#!/bin/bash
do_exit=0
while [ "$do_exit" == 0 ] ; do
free=`free -m | grep Mem | awk '{print $4}'`
if [[[ "$free"$free -gt 0 ];]]; then
do_exit=1
echo ${$do_exit}
fi
sleep 5;
done
You can break
while loops as well. Check this:
#!/bin/bash
while true; do
free=$(free -m | grep Mem | awk '{print $4}')
if [[ $free -gt 0 ]]; then
break;
fi
sleep 5;
done