Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Linux and Unix
Tag Cloud
access audio blue screen boot bsod connection crash dell desktop driver dvd email error excel excel 2003 firefox hard drive hardware hijackthis internet keyboard laptop malware monitor motherboard network networking outlook problem processor ram recovery router screen slow sound spyware tdlwsp.dll trojan upgrade vba video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Operating Systems > Linux and Unix >
Getting input in a Bash Script

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
mariolima's Avatar
Junior Member with 24 posts.
 
Join Date: Nov 2002
Location: Sao Paulo - Brazil
26-Apr-2004, 10:04 AM #1
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./
Squashman's Avatar
Distinguished Member with 14,983 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
26-Apr-2004, 10:35 AM #2
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.
Squashman's Avatar
Distinguished Member with 14,983 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
26-Apr-2004, 10:41 AM #3
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 ","
Squashman's Avatar
Distinguished Member with 14,983 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
Experience: IIAHYAYCESA,YAADA!
26-Apr-2004, 10:46 AM #4
uptime | cut -f4 -d "," | tr -d " load average: "
uptime | cut -f5 -d "," | tr -d " "
uptime | cut -f6 -d "," | tr -d " "
liviu's Avatar
Senior Member with 116 posts.
 
Join Date: Mar 2004
Location: Charleston-SC, USA
27-Apr-2004, 05:09 AM #5
Try to read Advanced Bash-Scripting Guide
http://www.tldp.org/LDP/abs/html/
codejockey's Avatar
Senior Member with 1,410 posts.
 
Join Date: Feb 2002
27-Apr-2004, 06:58 PM #6
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.
xico's Avatar
Distinguished Member with 25,406 posts.
 
Join Date: Jun 2002
Location: Venice, FL
Experience: Intermediate
28-Apr-2004, 07:54 PM #7
What should I be reading to understand the code you're talking in?
Whiteskin's Avatar
Distinguished Member with 2,051 posts.
 
Join Date: Nov 2002
Location: Alberta, Canada
Experience: Windows: Decent. Unix/Linux: Advanced +1
28-Apr-2004, 09:31 PM #8
Closed Thread Bookmark and Share

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.

Thread Tools


You Are Using:
Server ID
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.
Powered by Cermak Technologies, Inc.