[fpc-pascal] Why does RunCommand not produce the correct output?

Bo Berglund bo.berglund at gmail.com
Mon May 1 17:00:56 CEST 2023


I am trying to create a function to list the available serial ports on a Linux
device. But I have trouble getting commands which work on a terminal to also
work when run from my console program.

When I run this conmmand in the terminal I get the correct reply:

$ ls -l /sys/class/tty/*/device/driver | grep -v "platform/drivers/serial8250" |
awk '{print $9}' | awk -F'/' '{print "/dev/" $5}'

/dev/ttyAMA0

But when I use RunCommand() in FPC it does not work long line wrapped by
newsreader):

program listserialports;
{$mode objfpc}{$H+}

uses
  baseunix, classes, sysutils, process;

function ListSerialPorts: integer;
var
  ListCmd: TProcessString;
  OutData: string;
begin
  Result := 0;
  ListCmd := 'ls -l /sys/class/tty/*/device/driver | grep -v
"platform/drivers/serial8250" | awk ''{print $9}'' | awk -F''/'' ''{print
"/dev/" $5}''';
  Writeln('Command: ' + ListCmd); //To see what is actally sent to Linux
  RunCommand(ListCmd, OutData);
  Writeln(OutData);
end;

begin
  ListSerialPorts;
  Writeln('Done');
end.

I see the command being executed but there is an empty output.
If I copy the dislayed command and paste it in and run it I get the result as
shown above. So there should be no typo in the command line, right?
Still no output...

What can I do?
(I have created a shellscript with the same command string and it runs just fine
and prints the data on screen.
But I need the function in order to populate a control with the output inside a
GUI program once thus function works.


-- 
Bo Berglund
Developer in Sweden



More information about the fpc-pascal mailing list