 | Junior Member with 24 posts. | | Join Date: Nov 2002 Location: Sao Paulo - Brazil | | Getting input in a Bash Script Hi:
Imagine I need to issue, in a Bash Script, a command like "uptime". The result of this command is a line with many words and numbers, like:
10:40:18 up 48 days, 11:54, 1 user, load average: 0.12, 0.15, 0.16
I am particularly interested in getting those 3 different Load Average Times.
Question: how to code it in the script so that I can issue the uptime command, pass it on via pipe to another command putting each of these times in a different variable?
Thanks a lot.
Mario./ | | Distinguished Member with 14,983 posts. | | Join Date: Apr 2003 Location: 1265 Lombardi Ave Experience: IIAHYAYCESA,YAADA! | | Quote: |
Originally Posted by mariolima Hi:
Imagine I need to issue, in a Bash Script, a command like "uptime". The result of this command is a line with many words and numbers, like:
10:40:18 up 48 days, 11:54, 1 user, load average: 0.12, 0.15, 0.16
I am particularly interested in getting those 3 different Load Average Times.
Question: how to code it in the script so that I can issue the uptime command, pass it on via pipe to another command putting each of these times in a different variable?
Thanks a lot.
Mario./ | Hmm, this could be difficult. I was thinking of using the cut command but that would not work because the output lenght is probably going to change. I will look a little more into this. CodeJockey will probably have an answer for this. | | Distinguished Member with 14,983 posts. | | Join Date: Apr 2003 Location: 1265 Lombardi Ave Experience: IIAHYAYCESA,YAADA! | | Oh, I forgot you can use a delimeter in the cut command. But you are still going to have to tweak that output as well.
uptime | cut -f4 -d "," | | Distinguished Member with 14,983 posts. | | Join Date: Apr 2003 Location: 1265 Lombardi Ave Experience: IIAHYAYCESA,YAADA! | | uptime | cut -f4 -d "," | tr -d " load average: "
uptime | cut -f5 -d "," | tr -d " "
uptime | cut -f6 -d "," | tr -d " " | | Senior Member with 116 posts. | | Join Date: Mar 2004 Location: Charleston-SC, USA | | | | | Senior Member with 1,410 posts. | | | | Quote: |
CodeJockey will probably have an answer for this.
| Well, I might if I'd ever get off my digital derriere ... (  ) ...
Actually, LwdSquashman has got things pretty well covered -- but of course, there are other variations. As earlier posts mentioned the punctuation in the output from uptime is a problem (also true of the w and top commands, which also report this information). You might use something like this:
OUTPUT="`uptime | tr -d ','`"
set $OUTPUT
ARGCOUNT=`expr $# - 3`
shift $ARGCOUNT
echo one minute loadavg is $1
echo five minute loadavg is $2
echo fifteen minute loadavg is $3
which assumes that the data of interest will always be the last 3 fields in the output from the uptime command.
Hope this helps.
__________________ The slowest component still sits at the keyboard. | | Distinguished Member with 25,406 posts. | | Join Date: Jun 2002 Location: Venice, FL Experience: Intermediate | | What should I be reading to understand the code you're talking in? | | Distinguished Member with 2,051 posts. | | Join Date: Nov 2002 Location: Alberta, Canada Experience: Windows: Decent. Unix/Linux: Advanced +1 | | | |  THIS THREAD HAS EXPIRED.
Are you having the same problem?
We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.
|
Smart Search
| Find your solution! | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | |  WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
| You Are Using: |
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 05:59 AM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd. | |
|