The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script that can give login password for "ssh" without involving STDIN gydave Shell Programming and Scripting 2 08-03-2008 07:03 PM
ksh script as a login shell return "no controlling terminal" lramirev Shell Programming and Scripting 8 07-16-2008 01:31 AM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-20-2007 01:52 AM
Reset environment - ".. /etc/.environment dev_env" zzwu3591 UNIX for Advanced & Expert Users 7 01-12-2007 09:27 AM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 04:15 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-04-2008
Miikka Miikka is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 3
Question How to get environment of a "fresh" login shell?

Hello fellow *nix users!

I am a bit confused how could I get an environment of a "fresh/clean" login shell, that is, the environment at that moment when user has started e.g. a new terminal/console or so.

So this is the sequence of actions I should be able to do in a single shell session:

1. open a new terminal
2. source a script which will set my environment based on some rule set
3. do my tasks in that environment
4. clean the environment from exports made in step #2
5. source a new script which will do some other environment exports based on some other rule set
6. do some stuff...
And this sequence goes on and on...

Currently, there are two ways of doing this:
a) start a new terminal each time a new environment should be taken into use
b) before sourcing, start a new shell, e.g. bash/csh/what ever, and then do the sourcing... And when done -> exit that shell to get back the login shell and then start a new shell again

The problem with both ways mentioned above is lazy users. They would like to source the script setting the environment and after finished with that, source a new environment setting script. So they don't want to start a new shell or open up a new terminal.

So this is why I'm looking for a solution how to get an environment of a clean login shell. I don't think (might be wrong here..) there is a way to do something like:
Code:
bash -clean_shell_please "env > /tmp/some_file.txt"
Rsh does not help either as it seems to inherit its parent's environment as well. Rlogin seems like a good option BUT how can I get the environment via rlogin without user interaction, e.g. somehow piping commands to rlogin session?

Any ideas are highly appreciated! Thanks in advance!


-Miikka
  #2 (permalink)  
Old 10-04-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,728
I assume you mean "clean" as in zero environment variables.

One way
Code:
env - /usr/bin/ksh
Another way
Code:
set | awk '{ print "unset", $1 }' > ./tmp.tmp
. ./tmp.tmp
Now you have to source /etc/profile and then source ~/.profile or .bashrc or /home/username/.cshrc or whatever.

Last edited by jim mcnamara; 10-04-2008 at 08:09 PM..
  #3 (permalink)  
Old 10-04-2008
Miikka Miikka is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 3
Thanks Jim for your quick reply.

With "clean" I meant the environment user gets when he opens up a new terminal and types env.

The first way you suggested + sourcing /etc/profile and ~/.profile does not result to same environment compared to new terminal session. I also get warnings "WARNING: terminal is not fully functional" after that.

The second way, using awk one liner, leads to a file full of "unset $1", i.e. $1 does not get evaluated...
  #4 (permalink)  
Old 10-04-2008
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,300
You can start your scripts with this sequence:

Quote:
1. start shell_1
2. start shell_2 within shell_1, source your script_with_variable and do your stuff
3. exit shell_2 and return to shell_1
4. repeat steps 2 and 3
5. repeat steps 2 and 3
...
...
exit shell_1
REgards
  #5 (permalink)  
Old 10-04-2008
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
Quote:
Originally Posted by Miikka View Post
With "clean" I meant the environment user gets when he opens up a new terminal and types env.

Opening a new terminal will not always give you the same environment; it will inherit whatever environment is current.

What do you really want? And why?
  #6 (permalink)  
Old 10-04-2008
Miikka Miikka is offline
Registered User
  
 

Join Date: Oct 2008
Posts: 3
You are correct, however, that is how its currently done as I mentioned in my first post:
Quote:
Currently, there are two ways of doing this:
a) start a new terminal each time a new environment should be taken into use
b) before sourcing, start a new shell, e.g. bash/csh/what ever, and then do the sourcing... And when done -> exit that shell to get back the login shell and then start a new shell again
The problem is that users would like to source those environment setting scripts directly from a shell without "start a new shell - source a script - do some stuff - exit the shell - start a new shell" cycle. So they just want to "source a script - do some stuff - source a new script - do some other stuff".

So what I'm trying to accomplish here is some sort of way to make those environment setting scripts to reset the environment back to what it was at the time of login and then applying new settings. One thing to notice also is that I cannot force users to modify their .profile to save the environment at login time.

I was hoping there would be some kind of way to use rlogin (or something else giving the same result) to get a clean login shell so that I could do the following in the beginning of each script setting an environment:

* rlogin to a new shell which does not inherit the environment of parent
* at rlogin shell: env > clean_env.txt
* at script which is sourced:
** remove all environment variables and source clean_env.txt
** add some new environment variables on top of that

I know I could do this with Java or C, but I really would like to stick with shell script/perl/awk...

Sorry if I haven't been clear enough with my question
  #7 (permalink)  
Old 10-04-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,728
I fixed the awk script - my error. However I'm with cfa- it is not really clear what you want let alone how to do it.
Closed Thread

Bookmarks

Tags
environment, script, scripting, shell

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:41 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0