Results 1 to 9 of 9

Thread: How To: Run shell script and get output into shell variable

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Question How To: Run shell script and get output into shell variable

    Hi guys,

    I have a simple question, I want to store the output of the following command:

    sudo iotop -t 0 -C 3 | grep "load: "
    As you can see it is running all the time, and we get a new line every 3sec.

    I just want to store these new lines into a single variable, so I can use it into a script.

    To clear the screen, and do not print lines after the others when iotop is running just delete "- C".

    I DO NOT WANT TO STORE THE OUTPUT ON THE DRIVE!!!
    I DO NOT WANT TO KILL IOTOP, IT MUST BE RUNNING ALL THE TIME IN BACKGROUND!!!

    The idea is to do not write on the drive! Just store the output into a single variable called $Activity.

    For example:

    Code:
    Activity=$(sudo iotop -t 0 3 | grep "load: ") &
    
    While $Activity... do blahblah... done
    But the fact is that nothing can be stored into Activity
    So I have tried to know if something can be done to read the last output of a specified PID. But I need help on that point too :-/ I don't know how to do it.

    Please help...

  2. #2
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,515
    Thanks
    17
    Thanked 814 Times in 511 Posts
    Rep Power
    10

    Default

    My iotop command gives me following error when I run:
    Code:
    iotop: error: no such option: -t
    Are you sure -t and -C is valid option (i don't see them in man page)? I'm using v 0.2-2 under Ubuntu 8.10. Are you using DTrace base iotop version shell script (under Solaris / OpenSolaris UNIX)?
    Last edited by nixcraft; 16th July 2009 at 09:15 AM.
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Sorry I'm using it under MacOS.

    But the idea is there, you can see what I'm saying without any argument too

    It's just a command that clear the screen every 3sec reporting the hard drive activity.

    And the prob is that I want to store all these new fresh information into a variable that can be used in parallel into another script.

  4. #4
    Junior Member
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Ok, here is my code:

    Code:
    sudo iotop -t 0 -C 3 | grep "load: " | script.sh
    script.sh:

    Code:
    #!/bin/sh
    
      while read data
      do
          echo "[$(date +"%D %T")] $data" >> log_activity.txt
      done
    But nothing is read

  5. #5
    Never say die nixcraft's Avatar
    Join Date
    Jan 2005
    Location
    BIOS
    Posts
    4,515
    Thanks
    17
    Thanked 814 Times in 511 Posts
    Rep Power
    10

    Default

    try
    Code:
    #!/bin/bash
    exec 3>&1 
    iotop -t 0 -C 3 | grep "load: "
     while read line 
    do
        echo "[$(date +"%D %T")] $line"
    done
    exec 3>&-
    Outputs:
    Code:
    2009 Jul 16 16:00:17,  load: 0.25,  disk_r:      0 KB,  disk_w:      4 KB
    2009 Jul 16 16:00:20,  load: 0.23,  disk_r:      0 KB,  disk_w:      0 KB
    2009 Jul 16 16:00:23,  load: 0.23,  disk_r:      0 KB,  disk_w:      0 KB
    ^C^C
    let me know...
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]


  6. #6
    Junior Member
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    Thanks, but that's not working too :'(

    iotop is executed, and displays the disk activity info on the screen, but the while function is not loaded :/
    Last edited by Thireus; 16th July 2009 at 05:12 PM.

  7. #7
    Senior Member monk's Avatar
    Join Date
    Jan 2005
    Location
    Tibet
    Posts
    643
    Thanks
    5
    Thanked 43 Times in 38 Posts
    Rep Power
    15

    Default

    From the grep(1) man page:
    --line-buffered Turns on line buffering. However, this can be a performance penalty.
    So solution is to pass --line-buffered to your grep command:
    Code:
    ...
    iotop -t 0 -C 3 | grep "load: " --line-buffered |  while read data
    do
        echo "[$(date +"%D %T")] ${data}"
    done
    ...
    Run using sudo
    Code:
    sudo ./script.sh
    First sudo -s and as a root:
    Code:
    sudo -s
    ./script.sh
    May the force with you!

  8. The Following 2 Users Say Thank You to monk For This Useful Post:

    chimu (17th July 2009), Thireus (16th July 2009)

  9. #8
    Junior Member
    Join Date
    Jul 2009
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Default

    YAhouuuu!!!!

    GREAT! I LOVE YOU! <3

    Thanks a lot

    And thanks to nixcraft too

  10. #9
    Is that all you got? rockdalinux's Avatar
    Join Date
    May 2005
    Location
    Planet Vegeta
    Posts
    987
    Thanks
    27
    Thanked 71 Times in 62 Posts
    Rep Power
    20

    Talking

    Without grep:
    Code:
    iotop -t 0 -C 3 | while IFS= read -r data
    do 
        case $data in 
              *load* ) echo "[$(date +"%D %T")] ${data}";; 
        esac 
    done
    Rocky Jr.
    What's wrong? I hope I am not making you uncomfortable...

    Never send a boy to do a mans job.

  11. The Following User Says Thank You to rockdalinux For This Useful Post:

    chimu (17th July 2009)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 12th July 2009, 02:05 AM
  2. [Solved] Shell store or return output of a command in a variable
    By chimu in forum Shell scripting
    Replies: 4
    Last Post: 5th July 2009, 07:47 PM
  3. set shell variable uses which file in RHEL5.1??
    By knvpavan in forum Shell scripting
    Replies: 2
    Last Post: 19th May 2009, 05:39 AM
  4. shell script output to log file in /var/log/
    By asim.mcp in forum Shell scripting
    Replies: 6
    Last Post: 13th May 2009, 04:04 PM
  5. How to pass shell variable to awk program
    By in forum Shell scripting
    Replies: 4
    Last Post: 23rd June 2006, 05:11 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •