The major bug in your script is cut
should look like:
uptime_days=`uptime | cut -d " " -f 4`
Note the 5
became a 4
. This is because the cut was picking out the word days
, not the number before it.
The way to catch errors like this is to add an echo
into your code like:
echo uptime_days=$uptime_days
So when the output contains
uptime_days=days,
it is obvious some error in parsing has occurred.
There are also a couple of other things that could be improved to avoid issues later:
shebang
The top line of the script should start with #!
for it to be read by the kernel. Your code is being interpreted by the shell anyway, but if you run the command in a different way it might not so making the top line look like
#!/bin/bash
is a good idea.
double brackets
Using double brackets for conditionals in the shell is less error prone. You can also move the then
onto the same line with the if
like so:
if [[ $uptime_days -ge $max ]]; then
echo shutdown!
fi