Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2025-01

Question about tprocess from fpc

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Terry A. Haimann

I recently tried to execute a sed command from Lazarus, but didn't receive any output. But when I imbedded the sed command in a script and then executed the script from tprocess, it worked fine. Why is this? I have run across similar issues from other Linux commands when trying to execute them from a Free Pascal program. The work around I have found is to hide them in a script. Not pretty, but it works.

This was run on a i7-8700 and I am running Linux Mint 20.2 and am using Mate 1.24

Source Code

Function CaptureArtWork(b, e: LongInt): TStringList;
Var
   ArtProc:                TProcess;
   ArtSL:                  TStringList;
   i:                      LongInt;
Begin
     ArtProc  := TProcess.Create(nil);
     ArtSL := TStringList.Create;
     ArtProc.Executable := 'sed';
     ArtProc.Parameters.Add('-n');
     ArtProc.Parameters.Add('"' + IntToStr(b) + ',' + IntToStr(e) + 'p"');
     ArtProc.Parameters.Add('MyFileName');
     ArtProc.Options := ArtProc.Options + [poWaitOnExit, poUsePipes];
     ArtProc.Execute;
     ArtSL.LoadFromStream(ArtProc.Output);
     ArtProc.Free;
     CaptureArtWork := ArtSL;
End;
Terry A. Haimann

Reply by email

Marco van de Voort
to Terry A. Haimann

Op 24-1-2025 om 15:10 schreef Terry A. Haimann via fpc-pascal:

I recently tried to execute a sed command from Lazarus, but didn't receive any output. But when I imbedded the sed command in a script and then executed the script from tprocess, it worked fine. Why is this? I have run across similar issues from other Linux commands when trying to execute them from a Free Pascal program. The work around I have found is to hide them in a script. Not pretty, but it works.

This looks dodgy, remove the "" quotes.

ArtProc.Parameters.Add('"' + IntToStr(b) + ',' + IntToStr(e) + 'p"');

Quoting is used by the shell to separate parameters, but with Parameters.Add we already have done the separation, so quoting is not needed nor supported.

Reply by email

n7800
to Terry A. Haimann: I recently tried to execute a sed command from Lazarus, but didn't receive any output. …

You are reading the output incorrectly, this should be done in a loop:  
https://wiki.lazarus.freepascal.org/Executing_External_Programs#Reading_large_output  
To avoid doing this manually, use the "RunCommand":  
https://wiki.lazarus.freepascal.org/Executing_External_Programs#(Process.)RunCommand  
In general, study this article. There are a lot of details there, and options that you can choose, depending on what you need.  

Show quoted text 32 lines
Пятница, 24 января 2025, 19:18 +05:00 от Terry A. Haimann via fpc-pascal :   I recently tried to execute a sed command from Lazarus, but didn't receive any output. But when I imbedded the sed command in a script and then executed the script from tprocess, it worked fine. Why is this? I have run across similar issues from other Linux commands when trying to execute them from a Free Pascal program. The work around I have found is to hide them in a script. Not pretty, but it works. This was run on a i7-8700 and I am running Linux Mint 20.2 and am using Mate 1.24 Source Code Function CaptureArtWork(b, e: LongInt): TStringList; Var    ArtProc: TProcess;    ArtSL: TStringList;    i: LongInt; Begin      ArtProc := TProcess.Create(nil);      ArtSL := TStringList.Create;      ArtProc.Executable := 'sed';      ArtProc.Parameters.Add('-n');      ArtProc.Parameters.Add('"' + IntToStr(b) + ',' + IntToStr(e) + 'p"');      ArtProc.Parameters.Add('MyFileName');      ArtProc.Options := ArtProc.Options + [poWaitOnExit, poUsePipes];      ArtProc.Execute;      ArtSL.LoadFromStream(ArtProc.Output);      ArtProc.Free;      CaptureArtWork := ArtSL; End; -- Terry A. Haimann < > _______________________________________________ fpc-pascal maillist - https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

 

Reply by email