How to get environment of a "fresh" login shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get environment of a "fresh" login shell?
# 1  
Old 10-04-2008
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  
Old 10-04-2008
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 09:09 PM..
# 3  
Old 10-04-2008
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  
Old 10-04-2008
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  
Old 10-04-2008
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 Smilie
# 6  
Old 10-04-2008
Quote:
Originally Posted by Miikka
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?
# 7  
Old 10-04-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Shell script using expect to login to couple of remote servers and read "crontab -l"

I need a shell script using expect to login to couple of remote servers and read "crontab -l -u <username>" & "cat /etc/rc.local" & "df -h" and able to create output into a file saved locally with hostname.crontab & hostname.rc.local & disk.status. I can supply a file as list of hostname or IP... (4 Replies)
Discussion started by: jaipsharma
4 Replies

3. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

4. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

5. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

6. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Dummies Questions & Answers

how to install "source" command!/ broken "login.cl"!

Hello, I am new to this forums and this is my first "asking help" message! i have 2 problems: 1- for unknown reasons the "source" command is not avalable in my system (UBUNTU). i can't either see it in my bin directory! 2- again for unknown reasons the "login.cl" file in the home... (0 Replies)
Discussion started by: astrosona
0 Replies

8. AIX

"ksh -" as login shell bypassing .profile

Hi all, I am currently trying to tell /bin/ksh to behave like a login shell. I am invoking it from an interactive shell. In the documentation is stated, that calling it with exec ksh - it should behave like a login shell, work 1st on /etc/profile, ~/.profile and so on. I tried that with... (0 Replies)
Discussion started by: zaxxon
0 Replies

9. Shell Programming and Scripting

ksh script as a login shell return "no controlling terminal"

I have created a ksh shell script and used it as a login shell for a user. </etc/passwd> lramirev:x:111:200:Luis:/export/home/menush:/usr/local/menush/menush My shell script is like this: </usr/local/menush/menush> #!/bin/ksh # if ] then . $HOME/.profile fi ... (8 Replies)
Discussion started by: lramirev
8 Replies

10. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

Hi I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message No utpmx entry: you must exec "login" from lowest level "shell" :confused: What i want is: open various... (0 Replies)
Discussion started by: peterpan
0 Replies
Login or Register to Ask a Question