Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am trying to create a script that will check if uptime is greater than or equal a set number of hour(s), here is what I've got so far from googling:
Code:
#!/bin/sh
# Test Script to see if uptime is less than 1 hours
UPT="uptime | awk -F'( |,)' '{print $2}'"
HOURS="1"
if [ "$UPT" -lt "$HOURS" ]
then
echo "uptime is less then $HOURS hours(s)"
exit
else
echo "uptime is greater than $HOURS hour(s)"
fi
I think the problem is with the $UPT variable, I don't think it is being set properly, also the output has a colon in it (for example if uptime is 6hours and 36 minutes the result is "6:36") so it can't be compared (greater than or equal to) against the $HOURS variable because it has non-numeric characters in it. How can I extract the number of hours from uptime? Also what will happen when this script is run will less than 1 hour of uptime?
Your UPT variable isn't going to contain what you want. It will literally contain the string "uptime | awk -F'( |,)' '{print $2}'". If you want UPT to contain the output of that command line, either use backticks, or $() like so:
UPT=`uptime | awk -F'( |,)' '{print $2}'`,
or UPT=$(uptime | awk -F'( |,)' '{print $2}')
Also, to get just the hours, why not simply use awk again with the separator as ":", e.g.
Your UPT variable isn't going to contain what you want. It will literally contain the string "uptime | awk -F'( |,)' '{print $2}'". If you want UPT to contain the output of that command line, either use backticks, or $() like so:
UPT=`uptime | awk -F'( |,)' '{print $2}'`,
or UPT=$(uptime | awk -F'( |,)' '{print $2}')
Thanks
Now to just figure out how to extract just the hours ? and handle situations where hours are less then 1?
Quote:
Originally Posted by Nylex
Also, to get just the hours, why not simply use awk again with the separator as ":", e.g.
here is my output using a cut 'n' paste from your post
18:42:48 up 15 days, 3:33, 4 users, load average: 0.13, 0.33, 0.27
3
Please note, that I have "up 15 days," emphasis on the comma. This makes the fields different. If your uptime is > 1 day the script might work as is, if so then consideration must be given for the case uptime < 1 day.
Looks like a job for sed - " some stuff, whitespace, a digit (or 2), colon, 2 digits, a comma, more stuff."
Seems well enough formed to be simply parsable regardless of the intervening "days" or extraneous whitespace.
Looks like a job for sed - " some stuff, whitespace, a digit (or 2), colon, 2 digits, a comma, more stuff."
Seems well enough formed to be simply parsable regardless of the intervening "days" or extraneous whitespace.
uptime | sed -e 's/.* \(.* days,\)\? \(.*:..,\) .*/\1 \2/' -e's/,//g' -e 's/ days/d/' -e 's/ up //'
Now how can I get it to read the time properly so I can run my script to check if uptime is less than x number of hour(s)?
Code:
#!/bin/sh
# Test Script to see if uptime is less than 1 hours
UPT="uptime | sed -e 's/.* \(.* days,\)\? \(.*:..,\) .*/\1 \2/' -e's/,//g' -e 's/ days/d/' -e 's/ up //'"
HOURS="1"
if [ "$UPT" -lt "$HOURS" ]
then
echo "uptime is less then $HOURS hours(s)"
exit
else
echo "uptime is greater than $HOURS hour(s)"
fi
Doesn't work because $UPT is formatted with a colon (i.e. if the uptime is 1hr and 36minutes = "1:36")
Any idea's?
sed -nr 's/.*\s+([[:digit:]]{1,2}):[[:digit:]]{2},.*/\1/p'
Alright so this is what I've got:
Code:
#!/bin/sh
# Test Script to see if uptime is less than $HOURS hour(s)
UPT=$uptime | sed -nr 's/.*\s+([[:digit:]]{1,2}):[[:digit:]]{2},.*/\1/p'
HOURS=2
if [$UPT -lt $HOURS]
then
echo "uptime is less then $HOURS hours(s)"
exit
else
echo "uptime is greater than $HOURS hour(s)"
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.