?
Solved

How to get a variable from command line using perl ?

Posted on 2009-12-27
16
Medium Priority
?
321 Views
Last Modified: 2013-12-26
Hi,

I would like to request an assistant.

May i know how to get the "1.53" number (variable) after the "load average :" when issuing the following command ?

eg:

root@svr17 [~]# uptime
 02:28:11 up 38 days,  9:42,  2 users,  load average: 1.53, 0.61, 0.63

I actually wanted to accomplish this :

if the "load" is more than "10"
{
do this
}
else {
do nothing
}

The "load" is actually the "1.53" variable get from the "uptime" command.

Appreciates if anybody can helps.

Thank you.
0
Comment
Question by:smksa
  • 5
  • 4
  • 3
  • +3
16 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 26127931
Unix::Uptime - Determine the current uptime, in seconds, and load averages, across different *NIX architectures
http://search.cpan.org/~pioto/Unix-Uptime-0.36/lib/Unix/Uptime.pm
0
 
LVL 2

Author Comment

by:smksa
ID: 26127939
Can you provide complete sample if possible based on the my requirement ?
0
 
LVL 11

Expert Comment

by:nordtorp
ID: 26127957
Does this do what you want?

uptime | grep -o '[0-9]\+\.[0-9]\+*'

it should return about this:
1.53
0.61
0.63
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Accepted Solution

by:
vikas_madhusudana earned 800 total points
ID: 26127964
can you try this

var=`uptime | awk '{gsub("\,","");print $8}'`
0
 
LVL 2

Author Comment

by:smksa
ID: 26127994
Hi,

nordtorp, I actually wanted just "1.53" .

Vikas , you command produce error :

root@svr9 [~]# uptime | awk '{gsub("\,","");print $8}'
awk: warning: escape sequence `\,' treated as plain `,'
load
0
 
LVL 11

Expert Comment

by:nordtorp
ID: 26128000
I don't have a linux box here so I cannot test for you, but what does this return?
uptime | grep -o '[0-9]\+\.[0-9]'
0
 
LVL 2

Author Comment

by:smksa
ID: 26128007
root@svr9 [~]# uptime | grep -o '[0-9]\+\.[0-9]'
0.8
0.8
0.7
0
 
LVL 11

Expert Comment

by:nordtorp
ID: 26128028
I am sorry for just testing now, but what about this then:

uptime | grep -o -q '[0-9]\+\.[0-9]\+*'
or uptime | grep -q '[0-9]\+\.[0-9]\+*'
0
 
LVL 2

Author Comment

by:smksa
ID: 26128036
All is empty result :

root@svr17 [/]# uptime | grep -o -q '[0-9]\+\.[0-9]\+*'
root@svr17 [/]# uptime | grep -q '[0-9]\+\.[0-9]\+*'
root@svr17 [/]#

0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26128050
ya that is just a warning that fine you can just do a echo $var to check whether it has the variable 1.53
0
 
LVL 11

Assisted Solution

by:nordtorp
nordtorp earned 400 total points
ID: 26128055
temp=$(cat /proc/loadavg) && echo ${temp%% *}

Line got from here:
http://www.linuxselfhelp.com/HOWTO/Bash-Prompt-HOWTO/x728.html
0
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 26128056
it is a warning saying that you are using a escape sequence in your awk code
0
 
LVL 28

Assisted Solution

by:FishMonger
FishMonger earned 400 total points
ID: 26128275
Depending on what your real goal is, beyond what you've indicated, here is the basic Perl solution.

#!/usr/bin/perl

use Unix::Uptime;

my ($load) = Unix::Uptime->load;

if ($load > 10) {
    # do something
}
else {
    # do something else
}

Open in new window

0
 
LVL 85

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 26128704
if( (split' ',`uptime`)[-3] >10 ){
    # do something
}else{
    # do something else
}
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 200 total points
ID: 26129602
yet another way...
my $load = `uptime` =~ /load\s+average:\s+([\d\.]+)/;

Open in new window

0
 
LVL 2

Author Closing Comment

by:smksa
ID: 31670227
Thanks all...
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question