Recent

Author Topic: [SOLVED]Getting console output as string  (Read 5920 times)

bobix

  • Jr. Member
  • **
  • Posts: 71
    • http://rechnik-bg.com
[SOLVED]Getting console output as string
« on: April 21, 2017, 03:25:41 pm »
When i type in console
Code: Pascal  [Select][+][-]
  1. echo `ls`
I'm getting list of files in current directory. But using Runcommand to get similar output, I am getting `ls` as string. How can I make echo to return list of files? Or is there any other way i can get, for example, output of this console command:
xprop -id $(xprop -root _NET_ACTIVE_WINDOW | cut -d ' ' -f 5) WM_NAME

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,process;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Button1: TButton;
  16.     procedure Button1Click(Sender: TObject);
  17.   private
  18.     { private declarations }
  19.   public
  20.     { public declarations }
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.Button1Click(Sender: TObject);
  33. var
  34.   ss:string;
  35. begin
  36.   ss:='';
  37.   runcommand('echo',['`ls`'],ss);
  38.   showmessage(ss);
  39. end;
  40.  
  41. end.  
« Last Edit: April 21, 2017, 03:53:35 pm by bobix »
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Getting console output as string
« Reply #1 on: April 21, 2017, 03:34:04 pm »
runcommand/tprocess is not a shell. So you can not expect it to act as such.

You do expect that with your code as it uses a chained command and backticks.

Instead invoke bash and add parameters accordingly. That is a shell that should be capable of handling such things.

More on executing external command can be found here.

PS: if all you want is a directory listing why not use:
Code: [Select]
runcommand('ls',[],ss);
« Last Edit: April 21, 2017, 03:52:05 pm by molly »

bobix

  • Jr. Member
  • **
  • Posts: 71
    • http://rechnik-bg.com
Re: Getting console output as string
« Reply #2 on: April 21, 2017, 03:52:57 pm »
I've tryed many things. Finaly got it working :)
Thank you!
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   AProcess: TProcess;
  4.   AStringList: TStringList;
  5. begin
  6.   AProcess := TProcess.Create(nil);
  7.   AProcess.Executable := '/bin/sh';
  8.   AProcess.Parameters.Add('-c');
  9.   AProcess.Parameters.Add('echo `ls`');
  10.   AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];
  11.   AProcess.Execute;
  12.   AStringList := TStringList.Create;
  13.   AStringList.LoadFromStream(AProcess.Output);
  14.   showmessage(astringlist.Text) ;
  15.   AStringList.Free;
  16.   AProcess.Free;
  17. end;  
"  AProcess.Parameters.Add('-c');" was the missing part in my tests :)
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: [SOLVED]Getting console output as string
« Reply #3 on: April 21, 2017, 04:01:01 pm »
Good that you have it sorted out  :)

For such specific cases it is always a good idea to refer to a manual that explains a few things.

bobix

  • Jr. Member
  • **
  • Posts: 71
    • http://rechnik-bg.com
Re: [SOLVED]Getting console output as string
« Reply #4 on: April 21, 2017, 04:05:31 pm »
So true  ;)
Lazarus 1.8.4 r57972 FPC 3.0.4 i386-win32-win32/win64

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Getting console output as string
« Reply #5 on: April 23, 2017, 05:55:08 am »
I've tryed many things. Finaly got it working :)
Thank you!
...
"  AProcess.Parameters.Add('-c');" was the missing part in my tests :)
This code is prone to buffer deadlock, better use runcommand or its brothers.

 

TinyPortal © 2005-2018