Forum > Unix
How to get the output of a bash script
JohnSmith:
Hi
I would like to get the output into Lazarus
cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'
Thanks
Jurassic Pork:
hello,
you can put your command in a shell script cpulaz.sh (dont forget execute permission on it) :
--- Code: ---#!/bin/bash
cat <(grep 'cpu ' /proc/stat) <(sleep 1 && grep 'cpu ' /proc/stat) | awk -v RS="" '{print ($13-$2+$15-$4)*100/($13-$2+$15-$4+$16-$5)}'
--- End code ---
and call it in lazarus like that :
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---implementationuses process;{$R *.lfm}{ TForm1 }procedure TForm1.Button8Click(Sender: TObject); var hprocess: TProcess; OutputLines: TStringList; OutputError: TstringList; begin OutputLines:=TStringList.Create; OutputError:=TStringList.Create; hProcess := TProcess.Create(nil); hProcess.Executable := '/bin/bash'; hprocess.Parameters.Add('cpulaz.sh'); hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes]; hProcess.Execute; OutputLines.LoadFromStream(hprocess.Output); OutputError.Add('Error :'); OutputError.LoadFromStream(hProcess.Stderr); if OutputError.Text <> '' then showMessage(OutputError.Text) else ShowMessage(OutputLines.Text); hProcess.Free; OutputLines.Free; Outputerror.Free;end;
Friendly, J.P
JohnSmith:
Thank you, appreciate !!
Kays:
I hate such “design”, though. I think its more readable, if you just stay in Pascal,
however as usual you have to deal with other issues (EDivByZero/RTE200 to name one):
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program pcr(input, output, stderr); uses // for fpSleep() baseUnix; const kStatPath = '/proc/stat'; type // according to /usr/share/doc/linux-doc/filesystems/proc.txt.gz cpuStat = record user: longword; nice: longword; system: longword; idle: longword; iowait: longword; irq: longword; softirq: longword; steal: longword; guest: longword; guest_nice: longword; end; // reads CPU statistics from /proc/statfunction retrieveTotalCpuStat(): cpuStat;var beginningOfLine: string[4]; stat: text;begin assign(stat, kStatPath); {$iochecks off} reset(stat); {$iochecks on} if IOResult() = 0 then begin while not eof(stat) do begin read(stat, beginningOfLine); // this function only cares about the total CPU statistics if beginningOfLine = 'cpu ' then begin // can't directly read(stat, retrieveTotalCpuStat) here with retrieveTotalCpuStat do begin read(stat, user); read(stat, nice); read(stat, system); read(stat, idle); read(stat, iowait); read(stat, irq); read(stat, softirq); read(stat, steal); read(stat, guest); read(stat, guest_nice); end; // simpler than adding an additional "finished"-flag if not seekEof(stat) then begin writeLn(stderr, 'error: EOF not found'); close(stat); halt(1); end; end else begin // just advance file pointer readLn(stat); end; end; close(stat); end // else IOResult() reported an error else begin retrieveTotalCpuStat := default(cpuStat); end;end; type relativeTime = (earlier, later); var cpuStats: array[relativeTime] of cpuStat; sysUserDelta, sysUserIdleDelta: longword; begin // gather data cpuStats[earlier] := retrieveTotalCpuStat(); if fpSleep(1) <> 0 then begin writeLn(stderr, 'error: can''t sleep'); halt(1); end; cpuStats[later] := retrieveTotalCpuStat(); // calculations sysUserDelta := cpuStats[later].user - cpuStats[earlier].user + cpuStats[later].system - cpuStats[earlier].system; sysUserIdleDelta := sysUserDelta + cpuStats[later].idle - cpuStats[earlier].idle; if sysUserIdleDelta = 0 then begin writeLn(stderr, 'error: attempted division by zero'); halt(2); end; // output writeLn((sysUserDelta * 100 / sysUserIdleDelta):24:8); exitCode := 0;end.
mai:
I don't hate that design at all.
if Pascal was any more helpful - which it isn't - than standard UNIX tools, you might have a point.
but given that Laz & fpp crash on every third click, the topmost solution comes across as a pretty stable idea. O:-)
Navigation
[0] Message Index
[#] Next page