Announcement

Collapse
No announcement yet.

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

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • [Solved] 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
    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, 09:15 AM.
    All [Solved] threads are closed by mods / admin to avoid spam issues. See Howto mark a thread as [Solved]

    Comment


    • #3
      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.

      Comment


      • #4
        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

        Comment


        • #5
          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]

          Comment


          • #6
            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, 05:12 PM.

            Comment


            • #7
              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!

              Comment


              • #8
                YAhouuuu!!!!

                GREAT! I LOVE YOU! <3

                Thanks a lot

                And thanks to nixcraft too

                Comment


                • #9
                  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.

                  Comment

                  Working...
                  X