Consider the below code:
#!/usr/bin/perl
use strict;
use warnings;
$value="export PI = 3.14";
open(IN,">>/root/.bashrc");
print IN $value;
close(IN);
`source /root/.bashrc`;
I want to create an env variable using a Perl script. I've given 777 permissions to the root folder as well as the .bashrc file. The $value in the script is getting appended to .bashrc but when i use "env " or "printenv" to display env variables, i cant see the one which is appended to .bashrc.I guess the source command is not working, because when i source the .bashrc file from CL its showing in the env list.Please help me out or suggest another way.Thanks in advance.
The backticks start a subshell process where the source will be executed. And then will the subshell exit and your perl script will not be effected. Have you tried $ENV{PI}=3.14;? This will alter the environment of the running perl script and all afterwards created subprocesses.
use strict ;
use warnings ;
$ENV{TEST} = 'Yo.' ;
my $subshelled = qx!echo \$TEST! ;
chomp $subshelled ;
printf "Subshelled result: '%s'\n" , $subshelled ;
my $subshellenv = qx!env! ;
printf "Env:\n%s\n" , $subshellenv ;
will print
Subshelled result: 'Yo.'
Env:
...
TEST=Yo.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With