[fpc-devel] Free Pascal / Free Vision: Executing shell commands and showing output

Bart bartjunk64 at gmail.com
Tue Jan 27 18:14:02 CET 2026


On Tue, Jan 27, 2026 at 9:44 AM Aruna Hewapathirane via fpc-devel
<fpc-devel at lists.freepascal.org> wrote:


> The attached screenshot shows the issue I’m running into with the displayed output. I’m clearly missing something fundamental here, and I’d appreciate any pointers in the right direction.

What happens if you output the Result to a file?

I use similar code (allbeit in a Lazarus program), I use
LoadFromStream(Proc.Output) instead of your CopyFrom(), not sure if
that matters.
===
type
  TBlockDeviceInfoStrings = type TStringList;
...
procedure GetBlockDeviceInfoStrings(BlockDeviceInfoStrings:
TBlockDeviceInfoStrings);
var
  Proc: TProcess;
  i: Integer;
begin
  BlockDeviceInfoStrings.Clear;
  Proc := TProcess.Create(nil);
  try
  Proc.Executable := 'lsblk';   //ToDo: get this from path  ??
  Proc.Parameters.Add('--pair'); //output in Key-Value pairs
  //Proc.Parameters.Add('-e7'); //exclude RAM drives
  Proc.Parameters.Add('-a');    //list all blockdevices
  Proc.Parameters.Add('-b');    //size in bytes
  Proc.Parameters.Add('-o');    //output
  Proc.Parameters.Add('NAME,MOUNTPOINT,LABEL,FSTYPE,TYPE,PARTTYPE,UUID,MODEL,TRAN,RM,SIZE,FSSIZE,FSUSED,FSUSE%,FSAVAIL');
 //output columns
  Proc.Options := Proc.Options + [poWaitOnExit, poUsePipes];
  Proc.Execute;
  //writeln('lsblk: Proc.ExitCode=',Proc.ExitCode,',
Proc.ExitStatus=',Proc.ExitStatus);
  if not (Proc.ExitCode = 0) and (Proc.ExitStatus = 0) then
  begin
    writeln(stderr,'lsblk: ExitCode=',Proc.ExitCode,',
ExitStatus=',Proc.ExitStatus);
    Exit;
  end;
  BlockDeviceInfoStrings.LoadFromStream(Proc.Output);
  for i := BlockDeviceInfoStrings.Count - 1 downto 0 do
    if (Trim(BlockDeviceInfoStrings[i])='') then
      BlockDeviceInfoStrings.Delete(i);
  finally
    Proc.Free;
  end;
end;
===



-- 
Bart


More information about the fpc-devel mailing list