LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 02-16-2009, 03:52 PM   #1
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Rep: Reputation: 15
Extracting "Hours" from uptime in a BASH script?


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?
 
Old 02-16-2009, 04:02 PM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
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.

UPT=`uptime | awk -F'( |,)' '{print $2}' | awk -F ":" '{print $1}'`
 
Old 02-16-2009, 04:08 PM   #3
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Nylex View Post
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 View Post
Also, to get just the hours, why not simply use awk again with the separator as ":", e.g.

UPT=`uptime | awk -F'( |,)' '{print $2}' | awk -F ":" '{print $1}'`
Because that will output the current hour, not the uptime hour... for example:
Code:
$ uptime
 17:07:14 up  6:29,  3 users,  load average: 1.06, 1.12, 1.11
There for:
Code:
$ uptime | awk -F'( |,)' '{print $2}' | awk -F ":" '{print $1}'
17
Not "6"

Last edited by BassKozz; 02-16-2009 at 04:19 PM.
 
Old 02-16-2009, 04:12 PM   #4
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
That's strange. It works fine for me:

nick@darkstar:~$ uptime; UPT=`uptime | awk -F'( |,)' '{print $2}' | awk -F ":" '{print $1}'`; echo $UPT
22:34:06 up 4:16, 4 users, load average: 0.07, 0.30, 0.60
4
 
Old 02-16-2009, 04:20 PM   #5
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Nylex View Post
That's strange. It works fine for me:

nick@darkstar:~$ uptime; UPT=`uptime | awk -F'( |,)' '{print $2}' | awk -F ":" '{print $1}'`; echo $UPT
22:34:06 up 4:16, 4 users, load average: 0.07, 0.30, 0.60
4
Doesn't work for me:
Code:
$ uptime; UPT=`uptime | awk -F'( |,)' '{print $2}' | awk -F ":" '{print $1}'`; echo $UPT
 17:19:15 up  6:42,  3 users,  load average: 1.21, 1.09, 1.08
17
weird
 
Old 02-16-2009, 09:05 PM   #6
richwmn
LQ Newbie
 
Registered: Dec 2005
Location: Athens, GA
Distribution: Slackware (various),Ubuntu 9.04
Posts: 27

Rep: Reputation: 17
Quote:
Originally Posted by BassKozz View Post
Doesn't work for me:
Code:
$ uptime; UPT=`uptime | awk -F'( |,)' '{print $2}' | awk -F ":" '{print $1}'`; echo $UPT
 17:19:15 up  6:42,  3 users,  load average: 1.21, 1.09, 1.08
17
weird
BassKozz -- you have a space [ ( |,) ] in yours where nylex does not[ (|,) ]. It makes a difference. I get time with the space and uptime without.

Rich

Last edited by richwmn; 02-16-2009 at 09:09 PM.
 
Old 02-17-2009, 12:35 AM   #7
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Ah, I didn't notice that. I thought I just copied the line from BassKozz's post.
 
Old 02-17-2009, 04:16 PM   #8
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by richwmn View Post
BassKozz -- you have a space [ ( |,) ] in yours where nylex does not[ (|,) ]. It makes a difference. I get time with the space and uptime without.

Rich
Now I am getting the number of users when I don't use the space:
Code:
$ uptime; UPT=`uptime | awk -F'( |,)' '{print $2}' | awk -F ":" '{print $1}'`; echo $UPT
 17:14:06 up  4:58,  2 users,  load average: 0.96, 0.93, 0.81
17
$ uptime; UPT=`uptime | awk -F'(|,)' '{print $2}' | awk -F ":" '{print $1}'`; echo $UPT
 17:14:14 up  4:58,  2 users,  load average: 0.81, 0.90, 0.80
2 users

Last edited by BassKozz; 02-17-2009 at 07:39 PM.
 
Old 02-17-2009, 05:48 PM   #9
richwmn
LQ Newbie
 
Registered: Dec 2005
Location: Athens, GA
Distribution: Slackware (various),Ubuntu 9.04
Posts: 27

Rep: Reputation: 17
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.

Rich
 
Old 02-17-2009, 06:26 PM   #10
richwmn
LQ Newbie
 
Registered: Dec 2005
Location: Athens, GA
Distribution: Slackware (various),Ubuntu 9.04
Posts: 27

Rep: Reputation: 17
Further info --
running both on Slackware 12.1 gives the result I reported, time with the space and uptime without.

running both on Ubuntu 10.8 gives time with the space and an error without

methinks it needs more research

A little bit more -
Slackware 12.1 contains GNU Awk 3.1.5, Ubuntu has another version MAWK, for which I can't determine a version.

Rich

Last edited by richwmn; 02-17-2009 at 07:08 PM.
 
Old 02-17-2009, 07:11 PM   #11
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
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.
 
Old 02-17-2009, 07:43 PM   #12
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
awk -F "up |," '{print $2}'
Or with sed
sed 's/.*up \([^,]*\),.*/\1/'
And then convert in minutes and check.

Last edited by nx5000; 02-17-2009 at 07:44 PM.
 
Old 02-17-2009, 07:47 PM   #13
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by syg00 View Post
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.
Top results on google "sed uptime":
Code:
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?

Last edited by BassKozz; 02-17-2009 at 07:51 PM.
 
Old 02-17-2009, 07:51 PM   #14
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,128

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Ugh - try this instead
Code:
sed -nr 's/.*\s+([[:digit:]]{1,2}):[[:digit:]]{2},.*/\1/p'
 
Old 02-17-2009, 08:02 PM   #15
BassKozz
Member
 
Registered: Dec 2008
Distribution: Ubuntu 8.10
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by syg00 View Post
Ugh - try this instead
Code:
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
Output:
Code:
[: 17: missing ]
?
 
  


Reply

Tags
awk, bash, grep, script, scripting, uptime



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 01:03 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
Simple bash script "unexpected end of line error" snowman81 Programming 11 11-11-2007 09:31 AM
helping me in a bash script that perform a "select" menus Task adam_blackice Programming 5 09-15-2007 01:09 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:41 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration