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

tsiegel at softcon.com tsiegel at softcon.com
Mon May 1 20:25:28 CEST 2023


As mentioned in another post, you'll need to run a shell, (whether it be 
bash or another system provided shell), in order to process the 
commands.  If you run the commands directly, there is no shell 
interpretation, and the | which passes the output of one command to the 
input of the next one won't function as expected,

I don't know what environment processes commands if you run the commands 
without a shell environment, it could be FPC, or it could be something 
buried in the linux environment, I've never bothered to look up the 
answer, but it certainly won't have the redirection capability of the 
linux shells provided by the system, and actually, can behave 
differently, depending on which shell is used.

You could just add the line:

#!/bin/bash

to the first line of your script, then just call the script from your 
program.

Alternatively, you could call the bash command, and pass the command to 
it that way, either one should get you the output you desire.




On 5/1/2023 11:00 AM, Bo Berglund via fpc-pascal wrote:
> 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.
>
>


More information about the fpc-pascal mailing list