Page 1 of 1

[SOLVED] reading from a file

Posted: Fri Jun 11, 2004 10:51 am
by hward
Hi I can read my file ok with this code

Code: Select all

<?php
    if (!($fp = fopen("/proc/mdstat", "r")))  
        exit("Unable to open the input file.");
    while (!feof($fp))  
    &#123;  
        $c = fgetc($fp);  
        print $c;  
    &#125;
    fclose($fp);
?>
Personalities : [raid1] read_ahead 1024 sectors md0 : active raid1 hde1[1] hdg1[0] 39078016 blocks [2/2] [UU] [======>..............] resync = 34.9% (13670876/39078016) finish=41.0min speed=10324K/sec unused devices:

All I want out of it is
resync = 34.9%
finish=41.0min

Can I pull out just that part of the file?

Posted: Fri Jun 11, 2004 11:07 am
by Weirdan

Code: Select all

$str = file_get_contents("/proc/mdstat");
preg_match("/.*resync = ([0-9.]*)%.*finish=([0-9.]*)min/m", $str, $matches);
echo "resync = $matches[1]%\nfinish=$matches[2]min\n";
something like that.

Posted: Fri Jun 11, 2004 11:32 am
by hward
that works fine but when it is finished it shows

resync=% finish= min

how can you make it show

resync=100% finish=complete

when it is finished and theres nothing to show

Posted: Fri Jun 11, 2004 11:38 am
by Weirdan
it means you need to tweak the regexp I gave to you to match your sting.

Posted: Fri Jun 11, 2004 12:18 pm
by hward
I guess I don't understand what you mean

you mean like if else stuff?

Posted: Fri Jun 11, 2004 3:51 pm
by Weirdan
does it work while the process you monitor still running? something like
resync = 34.9%
finish=41.0min

?

Posted: Fri Jun 11, 2004 4:04 pm
by hward
I got it working with and if else

thanks