Link to home
Start Free TrialLog in
Avatar of saini_er
saini_er

asked on

How do I export Envrionment variable to Parent Shell by calling within the perl script ?

I am trying to set parent shell title by calling export variable from the perl script using following

export PROMPT_COMMAND='echo -ne "\033]0;user authentication\007"'

Whenever I call this function from within perl script , it spawned new child process and then kill it when program execution finishes.I want set the environment(title) of Parent Terminal window from where I run the main script using perl script
I am using ubuntu with bash shell
Avatar of Adam314
Adam314

There is a perl FAQ on this:

from perlfaq8:
Q:  I {changed directory, modified my environment} in a perl script. How come the change disappeared when I exited the script? How do I get my changes to be visible?

A:  In the strictest sense, it can't be done--the script executes as a different process from the shell it was started from. Changes to a process are not reflected in its parent--only in any children created after the change. There is shell magic that may allow you to fake it by eval()ing the script's output in your shell; check out the comp.unix.questions FAQ for details.
Avatar of saini_er

ASKER

Could you please tell me how to use eval() to set envrionment for parent shell.I had gone through FAQ , didn't got enough information
Here is an example:
******************** test1.pl
#!/usr/bin/perl
print "export ZZ=testing\n";
 
******************** At bash prompt
This will run test1.pl, and it's output will be as if you typed at the prompt
In this case, setting the ZZ environment variable to testing
  eval `./test1.pl`

Open in new window

You could write the variable and or value to a file in the subshell. When it exits, the parent shell could reference the file and dot it into the environment upon exit of the subshell. If the parent shell is the interactive shell, dotting the file will have to be manual otherwise the parent shell could dot the file created by the subshell. Then simply reference the environment variable.

Example: Cut and paste the snippit into a file called test.sh and then run it: sh test.sh

#!/bin/sh
 
# show there is no value for variable $VAR
echo "1 from parentshell: $VAR"
(
# subshell
cat << %%% > subshell.env
VAR=VALUE
export VAR
%%%
. subshell.env
# show the variable $VAR is set
echo "2 from subshell: $VAR"
)
# show value for variable $VAR is lost from the subshell
echo "3 from parentshell: $VAR"
# add the variable $VAR to the parent environment
. subshell.env
# show that it's set
echo "4 from parentshell: $VAR"

Open in new window

Not Perl ... Shell (sorry). But it still does the trick.
I would really appreciate if you could please tell me how to use eval for setting following environment of parent shell

export PROMPT_COMMAND='echo -ne "\033]0;user authentication\007"'

previous test1.pl example of eval doesn't seems to work for above given variable

#!/usr/bin/perl
print q{export PROMPT_COMMAND='echo -ne "\033]0;user authentication\007"'};

Open in new window

I tried your suggested solution
#!/usr/bin/perl
print q{export PROMPT_COMMAND='echo -ne "\033]0;user authentication\007"'};

then I tried to use eval `./xev.pl` and it didn't seems to set terminal title
Avatar of Tintin
Instead of

eval ./xev.pl

do

. ./xev.pl
I am getting this o/p

root@max:~# . ./xev.pl
Warning: unknown mime-type for "q{export" -- using "application/*"
Warning: unknown mime-type for "PROMPT_COMMAND=echo -ne "\033]0;user authentication\007"}" -- using "application/*"
Error: no such file "q{export"
Error: no such file "PROMPT_COMMAND=echo -ne "\033]0;user authentication\007"}"
root@max:~#


if I use ./xev.pl , i get o/p as shown below but it doen't set terminal title


root@max:~# ./xev.pl
export PROMPT_COMMAND='echo -ne "\033]0;user authentication\007"'root@max:~#
If you do this, does the title change:
    export PROMPT_COMMAND='echo -ne "\033]0;user authentication\007"'

With eval, are you using backticks?

When I do
    eval `./xev.pl`
The title of my terminal window changes.  
Yes if i use following export command directly on terminal , title change
 export PROMPT_COMMAND='echo -ne "\033]0;user authentication\007"'

but when i use eval , it just show same command as on command line and doesn't change title
I am using ubuntu with bash
After running
    eval `./xev.pl`
if you do
    env|grep PROMPT_COMMAND
or
    set|grep PROMPT_COMMAND
do you get anything?
About that....before running the eval, do
    unset PROMPT_COMMAND
if you have previously set it
Hi Adam ,
             Thanks a lot for your help and time..After using Unset it seems to work fine. But I have small issue now.Purpose I was doing all this because I don`t want user to type anything else except ./filename. In this case, is there way I can call  " eval `./xev.pl` " from within the perl script "test.pl"
I tried system() and back ticks , it didn't work for me.
My basic purpose is to put "eval `./xev`" in the beginning of my main program perl script so that I can set parent shell title to something and then execute further commands for that particular named shell
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Adam ,  this looks like good option..I will check it and will get back to you
I really appreciate you help on all this.
Adam your gnome-terminal option worked like a charm..
I really appreciate your help
Thanks
Manmeet