Skip to content
Advertisement

Bash script that will read a number from file and run command if number equals or is higher than X value

I would like to have a script that reads a number from a file, and if this number equals or higher a certain value, it will run another command, if not, script will just die, it would be something like this:

[root@firewall ~]# cat result.txt 
50
[root@firewall ~]# 
[root@firewall ~]# ./run.sh
result.txt is higher or equals 50. Running /sbin/reboot
[root@firewall ~]# 

Thanks for any help.

Advertisement

Answer

#!/bin/bash

THRESHOLD=50
VALUE=$(cat result.txt)

# -eq -> equals
# -gt -> greater than
# -ge -> greater than or equal (you are looking for this one)

if [ $VALUE -ge $THRESHOLD ]
then
  # Your action
fi
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement