Forum > Linux

[SOLVED]Getting console output as string

(1/2) > >>

bobix:
When i type in console

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,process; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    procedure Button1Click(Sender: TObject);  private    { private declarations }  public    { public declarations }  end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.Button1Click(Sender: TObject);var  ss:string;begin  ss:='';  runcommand('echo',['`ls`'],ss);  showmessage(ss);end; end.  

molly:
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: ---runcommand('ls',[],ss);

--- End code ---

bobix:
I've tryed many things. Finaly got it working :)
Thank you!

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button1Click(Sender: TObject);var  AProcess: TProcess;  AStringList: TStringList;begin  AProcess := TProcess.Create(nil);  AProcess.Executable := '/bin/sh';  AProcess.Parameters.Add('-c');  AProcess.Parameters.Add('echo `ls`');  AProcess.Options := AProcess.Options + [poWaitOnExit, poUsePipes];  AProcess.Execute;  AStringList := TStringList.Create;  AStringList.LoadFromStream(AProcess.Output);  showmessage(astringlist.Text) ;  AStringList.Free;  AProcess.Free;end;  "  AProcess.Parameters.Add('-c');" was the missing part in my tests :)

molly:
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:
So true  ;)

Navigation

[0] Message Index

[#] Next page

Go to full version