Solved

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

Posted on 2008-10-15
18
954 Views
Last Modified: 2013-12-21
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
Comment
Question by:saini_er
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
  • +1
18 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 22728053
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
 

Author Comment

by:saini_er
ID: 22731424
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
 
LVL 39

Expert Comment

by:Adam314
ID: 22733705
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:tdiops
ID: 22733882
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
 
LVL 2

Expert Comment

by:tdiops
ID: 22733900
Not Perl ... Shell (sorry). But it still does the trick.
0
 

Author Comment

by:saini_er
ID: 22737370
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
 
LVL 39

Expert Comment

by:Adam314
ID: 22737547

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

Open in new window

0
 

Author Comment

by:saini_er
ID: 22737920
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
 
LVL 48

Expert Comment

by:Tintin
ID: 22737950
Instead of

eval ./xev.pl

do

. ./xev.pl
0
 

Author Comment

by:saini_er
ID: 22738059
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
 
LVL 39

Expert Comment

by:Adam314
ID: 22740894
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
 

Author Comment

by:saini_er
ID: 22743059
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
 
LVL 39

Expert Comment

by:Adam314
ID: 22743363
After running
    eval `./xev.pl`
if you do
    env|grep PROMPT_COMMAND
or
    set|grep PROMPT_COMMAND
do you get anything?
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22743370
About that....before running the eval, do
    unset PROMPT_COMMAND
if you have previously set it
0
 

Author Comment

by:saini_er
ID: 22746953
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
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 22758047
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
 

Author Comment

by:saini_er
ID: 22758575
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
 

Author Closing Comment

by:saini_er
ID: 31506605
Adam your gnome-terminal option worked like a charm..
I really appreciate your help
Thanks
Manmeet
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Six Sigma Control Plans
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question