Sending commands to external terminal (Linux)

Just starting out? Need help? Post your questions and find answers here.
KianV
User
User
Posts: 16
Joined: Thu Dec 26, 2019 3:31 pm

Sending commands to external terminal (Linux)

Post 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.
Marc56us
Addict
Addict
Posts: 1600
Joined: Sat Feb 08, 2014 3:26 pm

Re: Sending commands to external terminal (Linux)

Post 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$)
:wink:
KianV
User
User
Posts: 16
Joined: Thu Dec 26, 2019 3:31 pm

Re: Sending commands to external terminal (Linux)

Post 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
Post Reply