Skip to main content
edited body
Source Link
Bayou
  • 3.4k
  • 1
  • 9
  • 25

There are a couple of errors.

  1. you echo do_exit before you set it to 1
  2. you have fi & sleep 5; which makes the if statement a child process, thus you can't use the value in the parent process
  3. -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

There are a couple of errors.

  1. you echo do_exit before you set it to 1
  2. you have fi & sleep 5; which makes the if statement a child process, thus you can't use the value in the parent process
  3. -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" -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

There are a couple of errors.

  1. you echo do_exit before you set it to 1
  2. you have fi & sleep 5; which makes the if statement a child process, thus you can't use the value in the parent process
  3. -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 -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
Source Link
Bayou
  • 3.4k
  • 1
  • 9
  • 25

There are a couple of errors.

  1. you echo do_exit before you set it to 1
  2. you have fi & sleep 5; which makes the if statement a child process, thus you can't use the value in the parent process
  3. -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" -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