1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Getting input in a Bash Script

Discussion in 'Linux and Unix' started by mariolima, Apr 26, 2004.

Thread Status:
Not open for further replies.
Advertisement
  1. mariolima

    mariolima Thread Starter

    Joined:
    Nov 4, 2002
    Messages:
    24
    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./
     
  2. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    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.
     
  3. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    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 ","
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    uptime | cut -f4 -d "," | tr -d " load average: "
    uptime | cut -f5 -d "," | tr -d " "
    uptime | cut -f6 -d "," | tr -d " "
     
  5. liviu

    liviu

    Joined:
    Mar 23, 2004
    Messages:
    116
  6. codejockey

    codejockey

    Joined:
    Feb 11, 2002
    Messages:
    1,405
    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.
     
  7. xico

    xico

    Joined:
    Jun 29, 2002
    Messages:
    29,787
    What should I be reading to understand the code you're talking in?
     
  8. Whiteskin

    Whiteskin

    Joined:
    Nov 15, 2002
    Messages:
    1,964
  9. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/223974

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice