How to get the output of a bash script lazarus / 66-Unix ---------------------------------------------------------------------- JohnSmith · 2017-09-24 22:55 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 · 2017-09-25 04:29 hello, you can put your command  in a shell script cpulaz.sh (dont forget execute permission on it) : Code: [Select]#!/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)}'and call it in lazarus like that : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 · 2017-09-25 11:14 Thank you, appreciate !! ---------------------------------------------------------------------- Kays · 2017-10-14 21:59 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  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i 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 · 2017-10-16 07:22 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. ---------------------------------------------------------------------- marcov · 2017-10-16 09:50 Quote from: mai on October 16, 2017, 09:22:24 amI 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.  If you don't have anything ontopic to say, maybe it is best to not comment at all. ---------------------------------------------------------------------- Thaddy · 2017-10-16 10:32 And standard UNIX tools can be perfectly written in Object Pascal.... What's your problem? ---------------------------------------------------------------------- marcov · 2017-10-16 10:47 Btw, the easiest is to just run the script interpreter with -c: Note that this script is sensitive to what shell is used, which flabbergasted me for a short while. (/bin/sh didn't work) That's why I wasn't first post Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {$mode delphi} uses process;var sin,sout : string;begin  sin:='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)}''';  runcommand('/bin/bash',['-c',sin],sout);// writeln(sin);  // only used to check quoting of inputwriteln(sout);end.  Note that all ' are doubled in the input string. This is normal way in Pascal literals to make a ', kind of like \ escape in C. At the end there is a doubled ' + another ' from closing the pascal literal. The example is basically the same as Jurassic Pork's, just using some predefined helper routines, and using direct commandline scripts (-c) instead of writing it out to file. If you just want to run, and not get the output, you can also use fpsystem()