Link to home
Start Free TrialLog in
Avatar of Shamsul Kamal
Shamsul Kamal

asked on

How to get a variable from command line using perl ?

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.
Avatar of FishMonger
FishMonger
Flag of United States of America image

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
Avatar of Shamsul Kamal
Shamsul Kamal

ASKER

Can you provide complete sample if possible based on the my requirement ?
Does this do what you want?

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

it should return about this:
1.53
0.61
0.63
ASKER CERTIFIED SOLUTION
Avatar of vikas_madhusudana
vikas_madhusudana
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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]'
root@svr9 [~]# uptime | grep -o '[0-9]\+\.[0-9]'
0.8
0.8
0.7
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]\+*'
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 [/]#

ya that is just a warning that fine you can just do a echo $var to check whether it has the variable 1.53
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
it is a warning saying that you are using a escape sequence in your awk code
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks all...