[How To?] Run shell script and get output into another shell.

Hi guys,

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

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:

Activity=$(sudo iotop -t 0 3 | grep "load: ") &

While $Activity... do blahblah... done

But the fact is that nothing can be stored into Activity :frowning:
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... :frowning:

---------- Post updated at 09:57 AM ---------- Previous update was at 02:02 AM ----------

Ok, here is my code:

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

script.sh:

#!/bin/sh

  while read data
  do
      echo "[$(date +"%D %T")] $data" >> log_activity.txt
  done

But nothing is read :frowning:

Maybe "mkfifo" could help you in this case.

Sorry but,

:frowning:

---------- Post updated at 03:03 PM ---------- Previous update was at 01:43 PM ----------

Sorry, my mistake, a fifo seems to do not write the output on the drive. Can you confirm?

What mkfifo does is it creates a pipe into which you can send your data from

sudo iotop -t 0 -C 3 | grep "load: "

and than read from it. No data is actually stored on the disk except for the pipe itself.

See http://en.wikipedia.org/wiki/FIFO_\(computing\)

Ok great, but now the problem is the same.

I cannot play with the fifo outputs!

mkfifo my_pipe
sudo iotop -t 0 -C 3 > my_pipe

And then from another shell:

cat my_pipe

Gives the same problem... I mean I cannot store the disk activity into a single variable.

---------- Post updated at 04:44 PM ---------- Previous update was at 03:32 PM ----------

SOLVED!!!

...
iotop -t 0 -C 3 | grep "load: " --line-buffered |  while read data
do
    echo "${data}"
    #Do whatever you want with ${data} here :)
done
...

:slight_smile:

---------- Post updated at 04:45 PM ---------- Previous update was at 04:44 PM ----------

But yeah fifo was working too :wink: