Lazarus

Programming => Operating Systems => Topic started by: BSaidus on January 04, 2021, 05:49:31 pm

Title: Executing shell commande on UNIX FreeBSD
Post by: BSaidus on January 04, 2021, 05:49:31 pm
I have an issue with executing commande for setting an environnement variable on FreeBSD ( shell is csh )
the commande is :
Code: Text  [Select][+][-]
  1.   setenv PARAM_1 152100
  2.  
Directelly on shell it executes well, but in freepascal, It do not.
I tried all functions on process and unix units.
Code: Pascal  [Select][+][-]
  1. // not work
  2. RunCommand( '/bin/csh', ['-c', 'setenv PARAM_1 152100'],s); // s an Ansistring
  3.  
  4. // Not work
  5.   with TProcess.Create(nil) do
  6.   begin
  7.     try
  8.       Executable := '/bin/csh';
  9.       //CommandLine := 'setenv PARAM_1 152100';
  10.       Options := Options + [poWaitOnExit];
  11.       Parameters.Clear;
  12.       Parameters.Add('-c');
  13.       Parameters.Add('setenv');
  14.       //Parameters.Add('-c setenv PARAM_1 152100');
  15.       //Parameters.Add('setenv PARAM_1 152100');
  16.       Parameters.Add('PARAM_1');
  17.       Parameters.Add('152100');
  18.       Execute;
  19.     finally
  20.       Free;
  21.     end;
  22.   end;
  23.  
  24.  

So , Can Anyone help me ???

Thanks.
Title: Re: Executing shell commande on UNIX FreeBSD
Post by: MarkMLl on January 04, 2021, 06:12:17 pm
Won't work. You can only set a shell variable applying to the current process on unix.

DOS etc. had a backdoor to allow a child process to set an environment variable in its parent command.com etc., so if you find somebody saying OF COURSE this works be cautious. Even in this case there were limits based on how much memory was allocated.

I've got round this in the past by having layered programs, i.e. the shell, then a custom program, then a second shell which inherits variables.

MarkMLl
Title: Re: Executing shell commande on UNIX FreeBSD
Post by: BSaidus on January 07, 2021, 09:34:28 am
Won't work. You can only set a shell variable applying to the current process on unix.

DOS etc. had a backdoor to allow a child process to set an environment variable in its parent command.com etc., so if you find somebody saying OF COURSE this works be cautious. Even in this case there were limits based on how much memory was allocated.

I've got round this in the past by having layered programs, i.e. the shell, then a custom program, then a second shell which inherits variables.

MarkMLl

Thank you @MarkMLl,
i will proceed in other way.
Title: Re: Executing shell commande on UNIX FreeBSD
Post by: MarkMLl on June 12, 2021, 01:32:50 pm
Apologies for posting to a fairly old thread.

This https://hackaday.com/2019/12/30/linux-fu-leaning-down-with-exec/ hence https://www.drdobbs.com/shell-corner-bash-dynamically-loadable-b/199102950 discusses writing shell extensions for Bash.

With the important caveat that I've not tried this (and don't know whether using FPC would be problematic), in principle it might be possible to write a shell extension that listened to a unix-domain socket, and on receipt of a message changed a shell/environment variable.

The result of this would be that the next program started by that shell would see the changed variable, which is something which cannot normally be done since a child cannot access a parent's memory.

MarkMLl

TinyPortal © 2005-2018