Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 969
  • Last Modified:

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
0
saini_er
Asked:
saini_er
  • 8
  • 7
  • 2
  • +1
1 Solution
 
Adam314Commented:
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.
0
 
saini_erAuthor Commented:
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
0
 
Adam314Commented:
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

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
tdiopsCommented:
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

0
 
tdiopsCommented:
Not Perl ... Shell (sorry). But it still does the trick.
0
 
saini_erAuthor Commented:
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
0
 
Adam314Commented:

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

Open in new window

0
 
saini_erAuthor Commented:
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
0
 
TintinCommented:
Instead of

eval ./xev.pl

do

. ./xev.pl
0
 
saini_erAuthor Commented:
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:~#
0
 
Adam314Commented:
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.  
0
 
saini_erAuthor Commented:
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
0
 
Adam314Commented:
After running
    eval `./xev.pl`
if you do
    env|grep PROMPT_COMMAND
or
    set|grep PROMPT_COMMAND
do you get anything?
0
 
Adam314Commented:
About that....before running the eval, do
    unset PROMPT_COMMAND
if you have previously set it
0
 
saini_erAuthor Commented:
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
0
 
Adam314Commented:
I don't know of a way.... If you make it a shell, the shell script will still run as child of your main script.

It sounds like your goal is to have a terminal window run, and have the title of the terminal window be the name of what that window is doing.  If so, you might be able to start your programs like this instead:
    Have ./xev be a shell script that does this (where xev.pl is your perl script):
    gnome-terminal --command=./xev.pl --title='user authentication' &
This will open a new terminal window with the specified title, and will run the specified command.
There are other options, see man gnome-terminal.
0
 
saini_erAuthor Commented:
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.
0
 
saini_erAuthor Commented:
Adam your gnome-terminal option worked like a charm..
I really appreciate your help
Thanks
Manmeet
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 8
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now