Recent

Author Topic: CommandLine is deprecated - shell operators |  (Read 19419 times)

lugdanum

  • New Member
  • *
  • Posts: 14
CommandLine is deprecated - shell operators |
« on: February 22, 2014, 07:16:57 pm »
Hello

I try to retrieve the name of the processor under Linux, but the command does not work:
Code: [Select]
Process1.parameters.add ('cat / proc / cpuinfo | grep model | cut -c14-')
I think the problem comes from the | symbol, moreover I saw that CommandLine is deprecated.

This example works even more valuable:
Code: [Select]
Process1.CommandLine: = 'cat / etc / hostname';
   Process1.Execute;


Can you please tell me how to replace these examples correctly?

I saw that he had to use something like:
Code: [Select]
Tprocess.Executable: =
Tprocess.Parameters.Add ('');
Tprocess.Parameters.Add ('')

But I do not understand it and I do not.

I tried this, but it does not work
Code: [Select]
   Process1.Executable:='sh';
   Process1.parameters.add('-c');
   Process1.parameters.add('cat /proc/cpuinfo  | grep model  | cut -c14-');
   Process1.Execute; 

Thank you very much in advance.
« Last Edit: February 23, 2014, 06:41:46 pm by lugdanum »

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: CommandLine is deprecated - shell operators |
« Reply #1 on: February 22, 2014, 07:46:00 pm »
Try this:
Code: [Select]
Process1.Executable := '/bin/bash';
Process1.Parameters.Add('-c');
Process1.Parameters.Add('cat /proc/cpuinfo | grep model | cut-c14-');

lugdanum

  • New Member
  • *
  • Posts: 14
Re: CommandLine is deprecated - shell operators |
« Reply #2 on: February 22, 2014, 09:26:34 pm »
Thank you for responding so quickly, but it does not work, nothing appears.
it's still weird


Thank you again.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: CommandLine is deprecated - shell operators |
« Reply #3 on: February 23, 2014, 03:03:52 am »
Try this:
Code: [Select]
Process1.Executable := '/bin/bash';
Process1.Parameters.Add('-c');
Process1.Parameters.Add('cat /proc/cpuinfo | grep model | cut-c14-');
Parameters.Add is supposed to be called per option. See: http://www.freepascal.org/docs-html/fcl/process/tprocess.parameters.html

lugdanum

  • New Member
  • *
  • Posts: 14
Re: CommandLine is deprecated - shell operators |
« Reply #4 on: February 23, 2014, 11:17:34 am »
thank you very much, but I'm really bad, I tried several things like this:

Code: [Select]
Process1.Executable := '/bin/bash';
Process1.Parameters.Add('-c');
Process1.Parameters.Add('cat');
Process1.Parameters.Add('/proc/cpuinfo');
Process1.Parameters.Add('grep model');
Process1.Parameters.Add('cut -c14-');

nothing to do but I do not. I started and I have a hard ...
I do not see too how. More sorry for my english but I go through Google translation.

thank you
« Last Edit: February 23, 2014, 06:41:25 pm by lugdanum »

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: CommandLine is deprecated - shell operators |
« Reply #5 on: February 23, 2014, 12:21:51 pm »
Complete code:
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);


  procedure ASleep(ASleepTime : LongInt);
  Var
    ATick : QWord;
  begin
    ATick := GetTickCount64 + ASleepTime;
    While GetTickCount64 < ATick Do Begin
      ThreadSwitch;
      Application.ProcessMessages;
    end;
  end;

Const
  BUFFER_SIZE = 1024;
Var
  AStr, BStr : String;
  ARead, ATimeCount : LongInt;
  AStrList : TStringList;
  ABuf : PChar;
  AOut : DWord;
begin
  AStrList := TStringList.Create;
  AStr := '';
  BStr := '';
  ARead := 0;
  ATimeCount := 0;

  GetMem(ABuf, BUFFER_SIZE);

  Process1.Options := [poUsePipes,poStderrToOutPut];
  Process1.Executable := '/bin/bash';
  Process1.Parameters.Add('-i');
  Process1.Execute;

  BStr := 'cat /proc/cpuinfo | grep model | cut -c14-' + LineEnding;
  Process1.Input.Write(BStr[1], Length(BStr));

  ASleep(300);

  BStr := '';

  Repeat
    ASleep(10);

    AOut := Process1.Output.NumBytesAvailable;
    If AOut <> 0 Then Begin
      ATimeCount := 0;
      ARead := Process1.Output.Read(ABuf^, BUFFER_SIZE);
        If ARead <> 0 Then Begin
          SetLength(BStr, ARead);
          //BStr := AStr + String(APChar^);
          Move(ABuf^, BStr[1], ARead);
          AStr := AStr + BStr;
          BStr := '';
        end;
    end
    Else
      Inc(ATimeCount);

  Until ATimeCount >= 50;

  // Exit from Bash
  BStr := 'exit' + LineEnding;
  Process1.Input.Write(BStr[1], Length(BStr));

  While Process1.Running Do
    ASleep(300);

  AStrList.Text := AStr;

  Memo1.Lines.Assign(AStrList);

  FreeAndNil(AStrList);
  FreeMem(ABuf, BUFFER_SIZE);
end;

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: CommandLine is deprecated - shell operators |
« Reply #6 on: February 23, 2014, 12:39:50 pm »
... and the wiki page that explains about this:
http://wiki.lazarus.freepascal.org/Executing_External_Programs
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

lugdanum

  • New Member
  • *
  • Posts: 14
Re: CommandLine is deprecated - shell operators |
« Reply #7 on: February 23, 2014, 12:56:43 pm »
Thank you from the beginning I saw this doc: http://wiki.lazarus.freepascal.org/Executing_External_Programs

But I'm too bad, I can not do it, even with the example de Cyrax.
In addition I think it should really just code for that ...

Although it is no longer valid I have this running, unwanted:
Code: [Select]
Process1.CommandLine:= 'sh -c "lspci | grep ''VGA''"';
Process1.Execute;

I think I'll see the side of Gambas because to retrieve a response from the console I think it is very complicated now cover. But it is also certainly I'm too bad!
again thank you for taking time to answer me anyway.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: CommandLine is deprecated - shell operators |
« Reply #8 on: February 23, 2014, 12:58:47 pm »
I'm sure Cyrax' example should work just fine. However, we cannot help you if you do not tell us exactly what's wrong (if necessary, in French, with an English translation below).

Anyway, if you're switching to Gambas, hope it works for you ;)

Bonne chance,
BigChimp
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

lugdanum

  • New Member
  • *
  • Posts: 14
Re: CommandLine is deprecated - shell operators |
« Reply #9 on: February 23, 2014, 03:14:32 pm »
Really thank you for the welcome, the Lazarus community is top notch.  :)
I just tested on Gambas, I get what I want but it looks to me less goods and Pascal lazarus ... A matter of taste I guess.


In fact to explain myself, I try to get the result of a command in a TMemo, when I do:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin   
 Process1.CommandLine:= 'sh -c "cat /proc/cpuinfo | grep model | cut -c14-"';
 Process1.Execute;
 Memo1.Lines.LoadFromStream(Process1.Output);
 Memo1.Lines.EndUpdate;
 Process1.free
end;
It works as I want, even if it tells me
Quote
Warning: Symbol "CommandLine" is deprecated


I really do not see how. Maybe I attack too complicated to start Delphi I used correctly but there are more than 15 years ago, and here I am a little lost.

Thank you anyway!


////////////////////////////////////////////////////////////
Vraiment merci pour l'accueil, la communauté Lazarus est vraiment top.  :)
Je viens de tester sur Gambas, j'obtiens ce que je veux mais celui-ci m'a l'air moins biens que lazarus et Pascal... Une question de goût j'imagine.


En fait pour m'expliquer, j'essaye d'obtenir le résultat d'une commande dans un Tmemo, quand je fais :
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
begin   
 Process1.CommandLine:= 'sh -c "cat /proc/cpuinfo | grep model | cut -c14-"';
 Process1.Execute;
 Memo1.Lines.LoadFromStream(Process1.Output);
 Memo1.Lines.EndUpdate;
 Process1.free
end;

Ca fonctionne comme je veux, même si cela m'indique
Quote
Warning: Symbol "CommandLine" is deprecated

Je ne vois vraiment pas comment faire. Peut être que je m'attaque à trop compliqué pour commencer J'utilisais Delphi correctement mais il y a plus de 15 ans de cela, et là je suis un peu perdu.

Merci en tout cas ![/size][/font]

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: CommandLine is deprecated - shell operators |
« Reply #10 on: February 23, 2014, 03:51:13 pm »

Note that something was lost in translation:

Code: [Select]
Process1.Executable := '/bin/bash';
Process1.Parameters.Add('-c');
Process1.Parameters.Add('cat /proc/cpuinfo | grep model | cut-c14-');

In the above code, suddenly "cut" touches the parameter, so the shell tries to pipe it through a file called cut-c14- not cut -c14-

lugdanum

  • New Member
  • *
  • Posts: 14
Re: CommandLine is deprecated - shell operators |
« Reply #11 on: February 23, 2014, 06:46:22 pm »
Yes it's true, well done, but even correcting this, yet nothing works. sniff
I wonder if I will not end up leaving the command "CommandLine": - \

Oui c'est vrai, bien vu, mais même en rectifiant cela, rien ne fonctionne encore. sniff
Je me demande si je ne vais pas finir par laisser la commande "CommandLine"  :-\

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: CommandLine is deprecated - shell operators |
« Reply #12 on: February 23, 2014, 07:09:23 pm »
Quote
Je me demande si je ne vais pas finir par laisser la commande "CommandLine"

PS : Hum, pour ma part, j utilise exclusivement "CommandLine".
Parameters.Add ne fonctionne que s' il n'y a qu'un seul et simple paramètre.
Et tant pis pour le message déprécié, au moins ça marche.

Translate : I do use only  "CommandLine". Too many problems with Parameters.Add.
« Last Edit: February 23, 2014, 07:12:30 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

lugdanum

  • New Member
  • *
  • Posts: 14
Re: CommandLine is deprecated - shell operators |
« Reply #13 on: February 23, 2014, 07:12:21 pm »
Ok cool thank you for this lift experience.
But is that it is on it does not pose a problem, for example, compatibility, export or others?

Thank you anyway !


Ok cool je te remercie pour cette remontée d'expérience.
Mais est-ce que c'est sur que cela ne pose pas de problème, par exemple de compatibilité, d'exportation ou autres ?

Merci en tout cas, et puis content de savoir qu'il a aussi des français dans le coin ;-)

Fred vS

  • Hero Member
  • *****
  • Posts: 3158
    • StrumPract is the musicians best friend
Re: CommandLine is deprecated - shell operators |
« Reply #14 on: February 23, 2014, 07:16:29 pm »
Quote
Mais est-ce que c'est sur que cela ne pose pas de problème, par exemple de compatibilité, d'exportation ou autres ?

Cela ne m'a jamais pose de problemes et c'est cross-os compatible. (évidemment, la commande doit être valable  ;) )

Translate. AFAIK, no problems with compatibility (only a boring message).

Quote
r qu'il a aussi des français dans le coin

PS : Je suis Francophone, pas Français...  :-X
« Last Edit: February 23, 2014, 07:18:48 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018