Page 1 of 1
Sending commands to external terminal (Linux)
Posted: Fri Sep 25, 2020 8:28 am
by KianV
I am trying to automate certain tasks from PB, using the native OS terminal, but without success.
As an example:
Code: Select all
rp = RunProgram("xterm", "", "", #PB_Program_Open | #PB_Program_Write)
WriteProgramStringN(rp,"df -h")
CloseProgram(rp)
I expected this to open the terminal, then write the command 'df -h' to it. The terminal opens, but the string is not passed.
I have also tried passing the command as a parameter,i.e.
Code: Select all
rp = RunProgram("xterm", "df -h", "", #PB_Program_Open | #PB_Program_Write)
, but this time the terminal does not even open.
Any help would be much appreciated.
Re: Sending commands to external terminal (Linux)
Posted: Fri Sep 25, 2020 9:22 am
by Marc56us
Hi,
xterm is a GUI program (a terminal), not a shell (command line tool), you can't send command to it with
WriteProgramStringN. An equivalent to sendmessage should be used.
df (Disk Free) is not a command shell, it's a command line software (a binary like an exe in Windows). You can't use it as parameter.
Code: Select all
$ file df
df: ELF 64-bit LSB executable, ...
Use it directly or in a shell
If you want to do something, do like example in help
Code: Select all
Compiler = RunProgram("df", "-h", "", #PB_Program_Open | #PB_Program_Read)
Output$ = ""
If Compiler
While ProgramRunning(Compiler)
If AvailableProgramOutput(Compiler)
Output$ + ReadProgramString(Compiler) + Chr(13)
EndIf
Wend
Output$ + Chr(13) + Chr(13)
Output$ + "Exitcode: " + Str(ProgramExitCode(Compiler))
CloseProgram(Compiler)
EndIf
MessageRequester("Output", Output$)

Re: Sending commands to external terminal (Linux)
Posted: Fri Sep 25, 2020 4:20 pm
by KianV
Thanks Marc56us for the very fullsome reply.
What I am actually trying to accomplish is significantly more in depth than calculating disk space.(It just seemed like a nice trivial example to be getting on with.)
At least now know why it wasn't working.
I think I shall resort to writing out a shell script, then run that using RunProgram().
Many thanks.
Kian