Ok first I would like to thank everyone that has given me instruction or direction, but I will state
none of the instruction or links that were offered give me a viable method. After Many hours
of trial and error I decided to try something that was not obvious at first.
Because on fpc wiki it says to pass arguments by 1
so I assumed they meant something like so:
hprocess.Parameters.Add('-c');
hprocess.Parameters.add('echo);
hprocess.Parameters.Add('sPass');
hprocess.Parameters.Add('|');
I will not go through the entire list of arguments to be passed but you get the idea.
I achieved the above translation as such because this:
Commandline arguments should be specified one per item in Parameters: each item in Parameters will be passed as a separate command-line item. It is therefor not necessery to quote whitespace in the items. As a consequence, it is not allowed to specify multiple command-line parameters in 1 item in the stringlist. If a command needs 2 options -t and -s, the following is not correct:
With Parameters do
begin
add('-t -s');
end;
Instead, the code should read:
With Parameters do
begin
add('-t');
Add('-s');
end;
Does not the above instructions or one argument per parameter just mean 1 ????
I conclude that the above had me and many others racking our heads as two why
we could translate a working syntax according to the old syntax that of witch you
will see below uses multiple aguments into one line. That the above says will not work
bet yet it works flawlessly and does not complain about command line depreciated.
The above code should read and I quote, only switches ie -c -l so forth
need to be one per parameter, though arguments may be passed as one parameter
because the below example will not work.
hProcess.Executable := '/bin/sh';
hprocess.Parameters.add('-c echo ' + sPass + ' | sudo -S fdisk -l');
however if you pass it as such
hProcess.Executable := '/bin/sh';
hprocess.Parameters.Add('-c');
hprocess.Parameters.add('echo ' + sPass + ' | sudo -S fdisk -l');
It will compile and work as it was meant too. because -c is a switch or option
and 'echo ' + sPass + ' | sudo -S fdisk -l' is multiple aguments that are to be
passed to sh shell
Complete working example below
Procedure TForm1.Button1Click(Sender: TObject);
var
hprocess: TProcess;
sPass: String;
Begin
sPass := 'baconisgreat'; // sudo password for your linux box
hProcess := TProcess.Create(nil);
//hProcess.CommandLine := 'sh -c "echo baconisgreat | sudo -S fdisk -l"';
// the above line works, however that syntax is depreciated, I would like to
// convert that line into the new standard using Executable with parameters.
// Conversion Begin
hProcess.Executable := '/bin/sh';
hprocess.Parameters.Add('-c');
hprocess.Parameters.add('echo ' + sPass + ' | sudo -S fdisk -l'); // Hmm multiple argument on
// one line??? But this is how to make it work. But wiki say its not supose to work and that only
// one argument per parameter.
// Conversion End
hProcess.Options := hProcess.Options + [poWaitOnExit, poUsePipes];
hProcess.Execute;
memo1.Lines.LoadFromStream(hprocess.Output);
memo2.Lines.LoadFromStream(hProcess.Stderr);
hProcess.Free;
end;