How to change Linux Terminal environment variable in a perl or bash script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to change Linux Terminal environment variable in a perl or bash script?
# 1  
Old 07-23-2013
How to change Linux Terminal environment variable in a perl or bash script?

Hi,
I meet an problem that it cannot change Terminal environment variable in a perl or bash script.
This change can only exist and become effective in script lifetime.
But I want to make this change take effect in current opened Terminal.
In our view, the thought seems to be impossible,
As far as I know, a Source Tool called modules which written by C or Tcl can realize this process.
you can view in
HTML Code:
 http://modules.sourceforge.net/
.
Modules -- Software Environment Management.

My problem is that how to change current opened Terminal through a perl or bash script?

Thanks firstly.
# 2  
Old 07-23-2013
Code:
cat pathenv
export PATH="$PATH:/opt/X11/bin"

echo $PATH
/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin

source pathenv

echo $PATH
/usr/local/bin:/usr/local/sbin:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin

# 3  
Old 07-23-2013
Quote:
Originally Posted by weichanghe2000
My problem is that how to change current opened Terminal through a perl or bash script?
Since variables do not go backwards that way, your perl script must create a new shell.

Code:
#!/usr/bin/perl

$ENV{MYVAR}="asdf";

exec 'bash', '-sh';

Code:
$ echo $MYVAR

$ exec ./myscript.pl # Using exec means you don't need to 'exit' twice after
$ echo $MYVAR
asdf

$

# 4  
Old 07-23-2013
A more flexible version:

Code:
#!/usr/bin/perl

$ENV{MYVAR}="asdf";

my $shell=shift;
if($shell) {
        exec $shell, @ARGV;
}
else
{
        exec "bash", "-sh";
}

When not given a shell it assumes bash, otherwise it runs what you tell it on the commandline.
# 5  
Old 07-25-2013
Quote:
Originally Posted by Corona688
A more flexible version:

Code:
#!/usr/bin/perl

$ENV{MYVAR}="asdf";

my $shell=shift;
if($shell) {
        exec $shell, @ARGV;
}
else
{
        exec "bash", "-sh";
}

When not given a shell it assumes bash, otherwise it runs what you tell it on the commandline.
You did a great job.
Thanks very much!
I will try do this method and verify it whether fulfill my requirement.

---------- Post updated 07-25-13 at 08:32 AM ---------- Previous update was 07-24-13 at 09:33 AM ----------

Quote:
Originally Posted by Corona688
A more flexible version:

Code:
#!/usr/bin/perl

$ENV{MYVAR}="asdf";

my $shell=shift;
if($shell) {
        exec $shell, @ARGV;
}
else
{
        exec "bash", "-sh";
}

When not given a shell it assumes bash, otherwise it runs what you tell it on the commandline.
I have already used your solution, but there is some problem as follows:
Our shell version is tcshrc, the terminal prompt like cm280:/home/wch> set in .tcshrc.
But the terminal prompt will become '%' only after executing './1.pl csh' using your method although the variable MYVAR has been changed as 'asdf'.

There cannot change Termina environment MYVAR as 'asdf' at all when executing option './1.pl csh -f'.

Is this any good suggestion and solution in tcshrc ?
I am looking forward to your answer.
Thanks greatly.
# 6  
Old 07-25-2013
-f is the opposite of what you want -- that forces it to not load your tcshrc.

Try csh -l to force it to be a login shell. -l must be the only argument.
# 7  
Old 07-26-2013
Quote:
Originally Posted by Corona688
-f is the opposite of what you want -- that forces it to not load your tcshrc.

Try csh -l to force it to be a login shell. -l must be the only argument.
Unfortunately, '1.pl csh -l' will also load .cshrc file again. and all variable including MYVAR var will be recovered as setted in .cshrc.

I have tried all csh commandline argument respectively, only -f will not load .cshrc and can modify variable MYVAR.

But after executing '1.pl csh -f', the Terminal Prompt will become '%' only not keeping raw style ctm:/home/wch>

Do you have any good ideas suggestion for me?
Could you try all process as described above in your cshell environment?
Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variable from bash to perl script

Hi All, I need to pass a variable from bash script to perl script and in the perl script i am using those variables in the sql query but its giving error : Use of uninitialized value $ENV{"COUNTRYCD"} in concatenation (.) or string at /GIS_ROOT/custom/tables/DBread_vendor.pl line 50. Can ... (6 Replies)
Discussion started by: NileshJ
6 Replies

2. UNIX for Dummies Questions & Answers

Environment variable substitution in Linux make

I know that I can do this in bash ver=${VERSION:-$DEFVERSION} so ver is $VERSION if it's set but $DEFVERSION if $VERSION isn't set I want to do the same thing as a macro in a Makefile and can't get it to work - maybe something like... VER=$(shell ${$(VERSION):-$(DEFVERSION)}) Any help... (1 Reply)
Discussion started by: JerryHone
1 Replies

3. UNIX for Dummies Questions & Answers

How to change colours in Linux Terminal Xfce?

Hi all - just started using Linux Mint 17 and I need to change the Foreground & Background Colours for the Terminal, my eyesight is not what it used to be many years ago, so any help would be much appreciated. Regards Malcolm (6 Replies)
Discussion started by: electrocad
6 Replies

4. Shell Programming and Scripting

How to start a shell script in a terminal console from graphic environment?

Hello. Normally when you double click on the file name, the shell script start in background. If you want to see what is going on, you must open a terminal console and start the shell within the terminal. Is it possible to start directly a shell script in a terminal console from the file... (0 Replies)
Discussion started by: jcdole
0 Replies

5. UNIX for Dummies Questions & Answers

setting a environment variable on linux

I want to set a enviroment variable VDC_DIR to a particular directory. I am doing it as export VDC_DIR=/abc it gets set but when i logout and do relogin than its not there. one way could be setting it in .profile file. but i have seen it on another box where it is not present in... (2 Replies)
Discussion started by: Jcpratap
2 Replies

6. Shell Programming and Scripting

Perl Csh - setenv ENV change environment variable

I have 3 programs, 1 in perl, 2 in csh: call them perl1, csh1 and run.ol I need perl1 to set csh1 variable NOLOG_qsub = "" I need perl1 to run, run.ol run.ol takes the executable and input and outputs to output run.ol#!/bin/csh -f # run.ol executable input output perl1 should... (1 Reply)
Discussion started by: austinj
1 Replies

7. Shell Programming and Scripting

Unable to change environment variables in bash script

Hello! For the moment some settings in my .bashrc contain the password of my company's firewall, which is not a good idea. I would like to use the string "PASSWORD" set in .bashrc and a script that changes all appearances of "PASSWORD" in the environment variables by the actual password (which... (4 Replies)
Discussion started by: markolopa
4 Replies

8. Shell Programming and Scripting

Not able to change the environment variable in k shell

Hi All, I am trying the following to set the environment variable in my scirpt.But it is not setting with the correct value. Can you guys please help me out to get the correct value. 1. I have environment variable NLS_LANG=American_America.UTF8 2. In my script (ksh) i am trying the following... (1 Reply)
Discussion started by: girish.raos
1 Replies

9. Linux

How to create file on Linux using environment variable in the dirpath

Hi all, I am running a Java program on a Linux server in which I read in a base directory path from the *.properties file. During processing, I build a unique file name and create a file to save data, concatenating the directory path and the file name. Works fine, except that I now need to... (2 Replies)
Discussion started by: patricia1of5
2 Replies

10. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies
Login or Register to Ask a Question