CommandLine is deprecated - shell operators | lazarus / 32-Linux ---------------------------------------------------------------------- lugdanum · 2014-02-22 18:16 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. ---------------------------------------------------------------------- Cyrax · 2014-02-22 18:46 Try this: Code: [Select]Process1.Executable := '/bin/bash'; Process1.Parameters.Add('-c'); Process1.Parameters.Add('cat /proc/cpuinfo | grep model | cut-c14-'); ---------------------------------------------------------------------- lugdanum · 2014-02-22 20:26 Thank you for responding so quickly, but it does not work, nothing appears. it's still weird Thank you again. ---------------------------------------------------------------------- Leledumbo · 2014-02-23 02:03 Quote from: Cyrax on February 22, 2014, 07:46:00 pmTry 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 · 2014-02-23 10:17 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 ---------------------------------------------------------------------- Cyrax · 2014-02-23 11:21 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 · 2014-02-23 11:39 ... and the wiki page that explains about this: http://wiki.lazarus.freepascal.org/Executing_External_Programs ---------------------------------------------------------------------- lugdanum · 2014-02-23 11:56 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 · 2014-02-23 11:58 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 ---------------------------------------------------------------------- lugdanum · 2014-02-23 14:14 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 QuoteWarning: 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 QuoteWarning: 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 · 2014-02-23 14:51 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 · 2014-02-23 17:46 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 · 2014-02-23 18:09 QuoteJe 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. ---------------------------------------------------------------------- lugdanum · 2014-02-23 18:12 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 · 2014-02-23 18:16 QuoteMais 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). Quoter qu'il a aussi des français dans le coin PS : Je suis Francophone, pas Français... ---------------------------------------------------------------------- Fred vS · 2014-02-23 18:26 Si le message du compilateur t'ennuie, tu peux utiliser ceci : Code: [Select]{$WARN SYMBOL_DEPRECATED OFF}     VProcess.CommandLine := ACommandLine; {$WARN SYMBOL_DEPRECATED ON} Translate : If the compiler-message borow you, you can add this in your code . ---------------------------------------------------------------------- marcov · 2014-02-23 19:13 Quote from: Fred vS on February 23, 2014, 07:09:23 pmI do use only  "CommandLine". Too many problems with Parameters.Add. What are the bugreports for that? I'm not aware of any problems. (that commandline doesn't have) Yes, shellexecute can do things you can't do with parameters.add, but you can't do them with .commandline either. The problem is that commandline must be parsed into parameters before handing it to the system. In general .commandline roughly works, but won't get any new "quoting" related fixes. The following quick test prints the model here: uses Process,sysutils; var  xx : array[0..1000000] of char;    process1:TProcess;    aread,aout:integer; begin  process1:=tprocess.create(nil);  Process1.Options := [poUsePipes,poStderrToOutPut];  Process1.Executable := '/bin/bash';  Process1.Parameters.Add('-c');  Process1.Parameters.Add('cat /proc/cpuinfo | grep model | cut -c14-');  Process1.Execute;  sleep (10);  AOut := Process1.Output.NumBytesAvailable;  writeln(aout);  ARead := Process1.Output.Read(xx, high(xx));  writeln(xx); end. ---------------------------------------------------------------------- Fred vS · 2014-02-23 21:55 @ Marcov : QuoteThe following quick test prints the model here: Hum, indeed and nice.  QuoteWhat are the bugreports for that? Maybe (surely) i was too lazy to do it work with Parameters.Add (it never works likes i wanted) and "CommandLine" does always perfectly his job, a simpler way...