in reply to Re: how to set environmental vars
in thread how to set environment vars

In your solution:

Fix:

#!/usr/bin/perl -w if (!defined($ENV{foo})) { if (@ARGV > 0 && $ARGV[0] eq 'nested') { die("Unable to find value for env var FOO\n"); } exec 'csh', '-c', "source env.csh ; perl $0 nested"; die("Unable to execute csh: $!\n"); } printf "%s = %s\n",$_,$ENV{$_} foreach keys %ENV;

Replies are listed 'Best First'.
Re^3: how to set environmental vars
by blue_cowdawg (Monsignor) on Nov 11, 2005 at 03:33 UTC

    While we're at fixing each other's code:

    | | exec '/bin/csh', '-c', "source env.csh ; perl $0 nested"; | |
    You forgot about the issue of an executable being in $PATH and being executed instead of the shell we are after.

    As far as an infinite loop is concerned I did state that I am testing for a variable that I know exists and is set in the env.csh file.

    Fixing the last two bullet items you are referring to is not covered in fix either AFAICT. Any arguments being passed to the script are going to be lost. To fix that I'd add

    exec '/bin/csh', '-c', sprintf("source env.csh ; perl $0 nested %s" +,join(" ",@ARGV));

    And believe it or not there are still some ways to break this...

      ack! join(" ", @ARGV)???? What if there are spaces or special characters? You've just added another bug and another injection attack.

      This is getting very complicated. And this my point. Compare this to the simple solution I posted, which has none of these problems.

      I get an error with the code, sorry if I miss anything silly.
      #!/usr/bin/perl -w if (!defined($ENV{foo})) { if (@ARGV > 0 && $ARGV[0] eq 'nested') { die("Unable to find value for env var FOO\n"); } exec '/bin/csh', '-c', sprintf("source script1.csh ; perl $0 nested %s +",join(" ",@ARGV)); die("Unable to execute csh: $!\n"); } printf "%s = %s\n",$_,$ENV{$_} foreach keys %ENV;
      The script1.csh
      setenv PATH_ORIG "${PATH}" setenv MANPATH_ORIG "${MANPATH}" setenv LD_LIBRARY_PATH_ORIG "${LD_LIBRARY_PATH}"

      And the Error I get is
      Missing $ on loop variable at tmp.pl line 13.

      Please help me out...
      Thanks