How to do user-preset login to Bash shell then automate path modification?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to do user-preset login to Bash shell then automate path modification?
# 1  
Old 02-01-2018
How to do user-preset login to Bash shell then automate path modification?

How do a user login with full user-environment preset to Bash shell then automatically do path modification with few script codes, either on command-line or put it in a script file.
what i tried:

Code:
bash --login -c PATH="/ANewPath:${PATH}"
bash --login -c 'PATH="/ANewPath:${PATH}"; export PATH'
bash --login -c 'export PATH="/ANewPath:${PATH}"'
bash --login ~/ChangePath.sh

with no avail

Anybody please usefully and sincerely help me...
Thanks in advance

Last edited by Scrutinizer; 02-02-2018 at 01:52 AM.. Reason: code tags
# 2  
Old 02-02-2018
When a bash shell starts several scripts are executed automatically. You can put whatever commands you want to have executed automatically there.

First, a (general) script is executed: /etc/profile. This is done for every user and therefore it should contain only settings/actions which should be equal for every user logging on.

Next a user-specific script is called: ~/.bash_profile (if the shell is called as bash) or ~/.profile (if the shell is called as sh). If you do not want to differentiate between these two you can create a symbolic link from one to the other.

These two files are executed for so-called "login-shells": if you connect to a system, identify yourself as a certain user, then a login-shell is started. Which one that is is determined in /etc/passwd, where the attributes of all users are stored. Here is an example line from a Linux system:

Code:
root:x:0:0:Superuser:/root:/bin/bash

It says that the user root needs to identify with a passwort (the x - the password itself is stored elsewhere), has the user ID 0, its primary group has the group ID 0, the users description is Superuser, its home directory is /root and its login shell is /bin/bash.

There are other (non-login) shells, which are i.e. started when you start an xterm (or konsole or another terminal emulator) in a graphical environment. These shell instances do NOT execute the files mentioned before but they read and execute a file called ~/.bashrc.

So, here is a simple what-is-where:

/etc/profile Put in there whatever every user should have set/executed.

~/.profile Put in there what the respective user should have executed upon login. To set the login shells environment to the same as every other shells environment (if this is desired) add as the last line of this script the lines:

Code:
ENV=~/.bashrc
. ~/.bashrc

This will execute ~/.bashrc for every login script. Notice the dot in front!

Code:
~/.bashrc     # WRONG
. ~/.bashrc   # CORRECT

This means the script is executed not in a subshell but in the same shell! Otherwise whatever you would set in there would be lost when the script ends!

~/.bashrc Put everything in there what you want to have executed in every shell. This file is also on a per-user basis. Notice that you already have a certain environment in a shell once it gets to executing this script. i.e. you want to do:

Code:
export PATH="$PATH:/some/added/path"

because if you do this:

Code:
export PATH="/some/added/path"

Whatever the PATH was before it would be reduced to the one directory mentioned.

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 02-02-2018
I'm not quite sure what you are trying to achieve. I think you are wanting to customise the environment by settings the PATH, variables, current directory etc. You would be better writing a script with the settings in and the you can source it.

Something like:-
my_env_1
Code:
PATH=/my_path1:$PATH
cd /my_dir1
BASE_VAR="DEV1"

my_env_2
Code:
PATH=/my_path2:$PATH
cd /my_dir2
BASE_VAR="DEV2"

etc.

You can then (from the shell prompt or within a sh/ksh/bash script) do source my_eny_1 or shorthand, you could . my_env_2 and it will set the various variables/values/directory etc. in the current shell. You could even (in .bashrc perhaps) create an alias to call this in for you, e.g. alias DEV1='source my_env_1 would allow you to just type DEV1 on the command line to set it up.

You need to be careful because switching back & forth between these two would grow the PATH variable each time, possibly leading to confusion.


Does that help, or have I missed the request entirely?

Robin

Last edited by rbatte1; 02-02-2018 at 05:06 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Automate OTPW login to ssh via bash script

Hello everyone. I'm a Linux novice trying out a lot of bash scripting lately, as it is so very addictive. Lately I have been setting up one of my boxes remotely and have been hardening it as much as possible. Please allow me to explain the scenario, as it does tend to become a little... (1 Reply)
Discussion started by: instro
1 Replies

2. UNIX for Dummies Questions & Answers

Which of the following command displays your login shell in bash shell?

Options:: A)$shell B)echo $ bash C)echo $ O D)$ O (1 Reply)
Discussion started by: raghugowda
1 Replies

3. Shell Programming and Scripting

How to ask for a path from the user in XML and shell?

I am working on a small plugin for Galaxy, which is written in shell script.The XML wrapper shows multiple options of paths where the user would like to store the file. I now want to add an option where the user can specify the path. Also,he can specify the name of the output file.As I am new to... (6 Replies)
Discussion started by: menorca
6 Replies

4. Shell Programming and Scripting

How to automate user selection options in shell script?

Hi There, I am trying to write a script which has to pick the prompted options by itself(i mean option to choose will be passed) here is real scenario i am trying to do. i have an executable(diagnos) which gets called in shell script, when the executable (diagnos) runs i get following as... (8 Replies)
Discussion started by: sairam_9191
8 Replies

5. Shell Programming and Scripting

How to Login as another user through Shell script from current user[Not Root]

Hi Every body, I would need a shell script program to login as different user and perform some copy commands in the script. example: Supppose ora_toms is the active user ora_toms should be able to run a script where user: ftptomsp pass: XXX should login through and run the commands ... (9 Replies)
Discussion started by: ujjwal27
9 Replies

6. Shell Programming and Scripting

Automate CVS login using shell script

Hi, Can anyone pls help me to automate login to cvs. I basically want to login to cvs and update a file. the script always gets to the login and returns the prompt for a password. Is there any way to send the password in the script itself. Here is the script: #!/bin/ksh... (0 Replies)
Discussion started by: raghu_shekar
0 Replies

7. Shell Programming and Scripting

setting a path in bash shell

Hello all, Sorry if the question if stupid but I have no big experience with programming. I am trying to set a path to be used in a makefile.in, for installation of a Fortran code. The makefile.in contains the string $(CODE_NAME) Now, when I type in the bash shell export... (6 Replies)
Discussion started by: laura74
6 Replies

8. Shell Programming and Scripting

Error in setting PATH variable in bash shell

Hi, I am new to shell scripting.I tried adding an entry to the path variable like below export PATH=$PATH:/opt/xxx/bin But am getting an error invalid identifier /opt/xxx/bin Can someone tell me the error above and correct me . Thanks and Regards, Padmini (2 Replies)
Discussion started by: padmisri
2 Replies

9. Solaris

How to get bash to be the login shell?

Is there a way to edit a .profile file that would let a user have bash as their login shell? We tried adding: exec /bin/bash --login to the .profile, but this produced an endless loop and we could not login using that account. Thanks! Aaron (8 Replies)
Discussion started by: amheck
8 Replies

10. UNIX for Dummies Questions & Answers

mistyped shell path, now i can't login as the only user that can su to root

i mistyped the location of bash and now i can't login as the only other user who belongs to wheel on my freebsd box. since i'm having many problems with samba, this has frozen my attempts to get things resolved with the former issue. i've been told that 'su -m' should do the trick, but it's... (3 Replies)
Discussion started by: xyyz
3 Replies
Login or Register to Ask a Question