Case 1 - If you want to change shell variable and then execute another program within a single Perl script:
$ENV{DISPLAY} = "10.16.119.3:0.0";
`another_program`;
Case 2 - If you have a shell script that calls a Perl script to permanently set environment variable, and then run another program:
Well, you can't. As soon as your perl script exits, the original shell variables are restored by the shell
(well, more strictly speaking, depending on the implementation of the underlying operating system, the shell script interpreter forks and then executes the perl script, the perl script has only modified a local copy of the environment variables in the child process.) However this doesn't mean this is impossible. The trick is to let Perl print the following line to STDOUT (assume that you are using bash):
print "export DISPLAY=$display_value\n";
And then in your shell script, you do this:
#!/usr/bin/bash
# Create a temporary shell script using perl
perl_program.pl > /tmp/setenv.$$
# source the temporary script and then remove it
. /tmp/setenv.$$
rm -f /tmp/setenv.$$
# run another program with the modified environment
another_program
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.