Recent

Author Topic: Executing shell commande on UNIX FreeBSD  (Read 1169 times)

BSaidus

  • Sr. Member
  • ****
  • Posts: 379
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Executing shell commande on UNIX FreeBSD
« 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.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

MarkMLl

  • Hero Member
  • *****
  • Posts: 2712
Re: Executing shell commande on UNIX FreeBSD
« Reply #1 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
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BSaidus

  • Sr. Member
  • ****
  • Posts: 379
  • lazarus 1.8.4 Win8.1 / cross FreeBSD
Re: Executing shell commande on UNIX FreeBSD
« Reply #2 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.
lazarus 1.8.4 Win8.1 / cross FreeBSD
dhukmucmur vernadh!

MarkMLl

  • Hero Member
  • *****
  • Posts: 2712
Re: Executing shell commande on UNIX FreeBSD
« Reply #3 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

Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

 

TinyPortal © 2005-2018