Solved

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

Posted on 2008-10-15
18
926 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article will explain how to establish a SSH connection to Ubuntu through the firewall and using a different port other then 22. I have set up a Ubuntu virtual machine in Virtualbox and I am running a Windows 7 workstation. From the Ubuntu vi…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now