Skip to main content
Spelling fixes and code-block quoting
Source Link
Toby Speight
  • 88.4k
  • 14
  • 104
  • 327

I like the script. But I have maybe some sugestionssuggestions for improvement.

RAM=$(free -m) total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}') available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

RAM=$(free -m)
total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}')
available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

Personaly iPersonalty I prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw serveralseveral scripts based on it. And in case that is a small script for desktop... why not :)

However. The main advantegeadvantage of the output from free -m is, that all information you need contains just one line. Try to hold the adventageadvantage. Multiple lines with awk usage isn't necessary here.

Try this: read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

Second thing is aritmetic,arithmetic: as @Zeta mentioned in previouseprevious answer, there ins'tisn't a reason for using expr. But instead:

WARNING=$(($total / 5)) CRITICAL=$(($total / 10))

WARNING=$(($total / 5))
CRITICAL=$(($total / 10))

warning=$(( total / 2 )) critical=$(( total / 10 ))

warning=$(( total / 2 ))
critical=$(( total / 10 ))

The rest could be okokay. But you are forcing bash that to dotest condition about warningwarning and criticalcritical, and in case thatwhen the script wasn't run by cron, you don't use it? PittyPity.

I like the script. But I have maybe some sugestions for improvement.

RAM=$(free -m) total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}') available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

Personaly i prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw serveral scripts based on it. And in case that is a small script for desktop... why not :)

However. The main advantege of the output from free -m is, that all information you need contains just one line. Try to hold the adventage. Multiple lines with awk usage isn't necessary here.

Try this: read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

Second thing is aritmetic, as @Zeta mentioned in previouse answer, there ins't a reason for using expr. But instead:

WARNING=$(($total / 5)) CRITICAL=$(($total / 10))

warning=$(( total / 2 )) critical=$(( total / 10 ))

The rest could be ok. But you forcing bash that to do condition about warning and critical and in case that the script wasn't run by cron, you don't use it? Pitty.

I like the script. But I have maybe some suggestions for improvement.

RAM=$(free -m)
total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}')
available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

Personalty I prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw several scripts based on it. And in case that is a small script for desktop... why not :)

However. The main advantage of the output from free -m is, that all information you need contains just one line. Try to hold the advantage. Multiple lines with awk usage isn't necessary here.

Try this:

read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

Second thing is arithmetic: as @Zeta mentioned in previous answer, there isn't a reason for using expr. But instead:

WARNING=$(($total / 5))
CRITICAL=$(($total / 10))
warning=$(( total / 2 ))
critical=$(( total / 10 ))

The rest could be okay. But you are forcing bash to test condition about warning and critical, and when the script wasn't run by cron, you don't use it? Pity.

edited body
Source Link

I like the script. But I have maybe some sugestions for improvement.

This part:

RAM=$(free -m) total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}') available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

Personaly i prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw serveral scripts based on it. And in case that is a small script for desktop... why not :)

However. The main advantege of the output from free -m is, that all information you need contains just one line. Try to hold the adventage. Multiple lines with awk usage isn't necessary here.

Try this: read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

Second thing is aritmetic, as mentioned @Zeta mentioned in previouse answer, there ins't a reason for using expr. But instead:

WARNING=$(($total / 5)) CRITICAL=$(($total / 10))

I would use the syntax below, due to issue which i had in bash GNU bash, 4.2.4(2)-release. (Btw. ShellCheck complains about that as well.)

warning=$(( total / 2 )) critical=$(( total / 10 ))

The rest could be ok. But you forcing bash that to do condition about warning and critical and in case that the script wasn't run by cron, you don't use it? Pitty.

I would do that this way:

#!/usr/bin/env bash

# gets available and total ram
read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

warning=$(( total / 5 ))
critical=$(( total / 10 ))

message="${available}/${total} MB free"

if (( $available < $critical )); then
  icon="error"
  expire="-t 60000"
  title="Memory critical!"
  message+=", critical at $critical MB"
elif (( $available < $warning )); then
  icon=""
  expire="-t 15000"
  title="Memory warning."
  message+=", warning at $warning MB"
fi

if [[ -t 0 ]]; then
  echo "$title $message"
else
  notify-send "--icon=$icon" "$title" "$message"
fi

Regards :)

I like the script. But I have maybe some sugestions for improvement.

This part:

RAM=$(free -m) total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}') available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

Personaly i prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw serveral scripts based on it. And in case that is a small script for desktop... why not :)

However. The main advantege of the output from free -m is, that all information you need contains just one line. Try to hold the adventage. Multiple lines with awk usage isn't necessary here.

Try this: read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

Second thing is aritmetic, as mentioned @Zeta in previouse answer, there ins't a reason for using expr. But instead:

WARNING=$(($total / 5)) CRITICAL=$(($total / 10))

I would use the syntax below, due to issue which i had in bash GNU bash, 4.2.4(2)-release. (Btw. ShellCheck complains about that as well.)

warning=$(( total / 2 )) critical=$(( total / 10 ))

The rest could be ok. But you forcing bash that to do condition about warning and critical and in case that the script wasn't run by cron, you don't use it? Pitty.

I would do that this way:

#!/usr/bin/env bash

# gets available and total ram
read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

warning=$(( total / 5 ))
critical=$(( total / 10 ))

message="${available}/${total} MB free"

if (( $available < $critical )); then
  icon="error"
  expire="-t 60000"
  title="Memory critical!"
  message+=", critical at $critical MB"
elif (( $available < $warning )); then
  icon=""
  expire="-t 15000"
  title="Memory warning."
  message+=", warning at $warning MB"
fi

if [[ -t 0 ]]; then
  echo "$title $message"
else
  notify-send "--icon=$icon" "$title" "$message"
fi

Regards :)

I like the script. But I have maybe some sugestions for improvement.

This part:

RAM=$(free -m) total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}') available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

Personaly i prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw serveral scripts based on it. And in case that is a small script for desktop... why not :)

However. The main advantege of the output from free -m is, that all information you need contains just one line. Try to hold the adventage. Multiple lines with awk usage isn't necessary here.

Try this: read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

Second thing is aritmetic, as @Zeta mentioned in previouse answer, there ins't a reason for using expr. But instead:

WARNING=$(($total / 5)) CRITICAL=$(($total / 10))

I would use the syntax below, due to issue which i had in bash GNU bash, 4.2.4(2)-release. (Btw. ShellCheck complains about that as well.)

warning=$(( total / 2 )) critical=$(( total / 10 ))

The rest could be ok. But you forcing bash that to do condition about warning and critical and in case that the script wasn't run by cron, you don't use it? Pitty.

I would do that this way:

#!/usr/bin/env bash

# gets available and total ram
read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

warning=$(( total / 5 ))
critical=$(( total / 10 ))

message="${available}/${total} MB free"

if (( $available < $critical )); then
  icon="error"
  title="Memory critical!"
  message+=", critical at $critical MB"
elif (( $available < $warning )); then
  icon=""
  title="Memory warning."
  message+=", warning at $warning MB"
fi

if [[ -t 0 ]]; then
  echo "$title $message"
else
  notify-send "--icon=$icon" "$title" "$message"
fi

Regards :)

deleted 2 characters in body
Source Link

I like the script. But I have maybe some sugestions for improvement.

This part:

RAM=$(free -m) total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}') available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

Personaly i prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw serveral scripts based on it. And in case that is a small script for desktop... why not :)

However. The main advantege of the output from free -m is, that all information you need contains just one line. Try to hold the adventage. Multiple lines with awk usage isn't necessary here.

Try this: read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

Second thing is aritmetic, as mentioned @Zeta in previouse answer, there ins't a reason for using expr. But instead:

WARNING=$(($total / 5)) CRITICAL=$(($total / 10))

I would use the syntax below, due to issue which i had in bash GNU bash, 4.2.4(2)-release. (Btw. ShellCheck complains about that as well.)

warning=$(( total / 2 )) critical=$(( total / 10 ))

The rest could be ok. But you forcing bash that to do condition about warning and critical and in case that the script wasn't run by cron, you don't use it? Pitty.

I would do that this way:

#!/usr/bin/env bash

# gets available and total ram
read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

warning=$(( total / 5 ))
critical=$(( total / 10 ))

message="${available}/${total} MB free"

if (( $available < $critical )); then
  icon="error"
  expire="-t 60000"
  title="Memory critical!"
  message="${available}/${total} MB freemessage+=", critical at $critical MB"
elif (( $available < $warning )); then
  icon="-i warning"icon=""
  expire="-t 15000"
  title="Memory warning. ${available}/${total} MB free, "
  message="${available}/${total} MB freemessage+=", criticalwarning at $critical$warning MB"
fi

if [[ -t 0 ]]; then
  echo "$title $message"
else
  notify-send "--icon=$icon" "$title" "$message"
fi

Regards :)

I like the script. But I have maybe some sugestions for improvement.

This part:

RAM=$(free -m) total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}') available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

Personaly i prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw serveral scripts based on it. And in case that is a small script for desktop... why not :)

However. The main advantege of the output from free -m is, that all information you need contains just one line. Try to hold the adventage. Multiple lines with awk usage isn't necessary here.

Try this: read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

Second thing is aritmetic, as mentioned @Zeta in previouse answer, there ins't a reason for using expr. But instead:

WARNING=$(($total / 5)) CRITICAL=$(($total / 10))

I would use the syntax below, due to issue which i had in bash GNU bash, 4.2.4(2)-release. (Btw. ShellCheck complains about that as well.)

warning=$(( total / 2 )) critical=$(( total / 10 ))

The rest could be ok. But you forcing bash that to do condition about warning and critical and in case that the script wasn't run by cron, you don't use it? Pitty.

I would do that this way:

#!/usr/bin/env bash

# gets available and total ram
read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

warning=$(( total / 5 ))
critical=$(( total / 10 ))

if (( $available < $critical )); then
  icon="error"
  expire="-t 60000"
  title="Memory critical!"
  message="${available}/${total} MB free, critical at $critical MB"
elif (( $available < $warning )); then
  icon="-i warning"
  expire="-t 15000"
  title="Memory warning. ${available}/${total} MB free, "
  message="${available}/${total} MB free, critical at $critical MB"
fi

if [[ -t 0 ]]; then
  echo "$title $message"
else
  notify-send "--icon=$icon" "$title" "$message"
fi

Regards :)

I like the script. But I have maybe some sugestions for improvement.

This part:

RAM=$(free -m) total=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $2}') available=$(echo "$RAM"|awk '/^[mM]em\.?:/{print $7}')

Personaly i prefer to dig these values from /proc/meminfo to avoid running unnecessary free utility, but I saw serveral scripts based on it. And in case that is a small script for desktop... why not :)

However. The main advantege of the output from free -m is, that all information you need contains just one line. Try to hold the adventage. Multiple lines with awk usage isn't necessary here.

Try this: read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

Second thing is aritmetic, as mentioned @Zeta in previouse answer, there ins't a reason for using expr. But instead:

WARNING=$(($total / 5)) CRITICAL=$(($total / 10))

I would use the syntax below, due to issue which i had in bash GNU bash, 4.2.4(2)-release. (Btw. ShellCheck complains about that as well.)

warning=$(( total / 2 )) critical=$(( total / 10 ))

The rest could be ok. But you forcing bash that to do condition about warning and critical and in case that the script wasn't run by cron, you don't use it? Pitty.

I would do that this way:

#!/usr/bin/env bash

# gets available and total ram
read -r _ total _ _ _ _ available <<< $(free -m | grep -i mem)

warning=$(( total / 5 ))
critical=$(( total / 10 ))

message="${available}/${total} MB free"

if (( $available < $critical )); then
  icon="error"
  expire="-t 60000"
  title="Memory critical!"
  message+=", critical at $critical MB"
elif (( $available < $warning )); then
  icon=""
  expire="-t 15000"
  title="Memory warning."
  message+=", warning at $warning MB"
fi

if [[ -t 0 ]]; then
  echo "$title $message"
else
  notify-send "--icon=$icon" "$title" "$message"
fi

Regards :)

Source Link
Loading