Help with script execution in bashrc
Hello,
I am running Ubuntu 7.10 and yesterday i added a script in my .bashrc file. I want this script to be executed only when i login via SSH to my account. I have tested the script execution and it works. Unfortunately though, the script gets executed also when i locally open a terminal window. Is there a way i can make this script executed ONLY when i login via SSH? Thank you in advance. |
Hi there,
All the scripts in .bashrc will be automatically executed with each new terminal. The trick is to use an alias ssh_new_script='whatever_path_to_real_script', and in this way you can call only the alias. Have fun! |
Basically, you have two possibilities.
Option 1: add the script invocation to .bash_profile, and your other stuff in .bashrc (with .bash_profile sourcing .bashrc, assuming you've got something you want in there too). .bash_profile is sourced on login sessions, .bashrc on interactive non-login sessions. But this would also get you console logins and screen session starts, so may not be ideal. Option 2: add it to .bash_profile or .bashrc with a conditional Code:
[[ -n $SSH_CLIENT ]] && /path/to/script.sh Combining the two approaches and a little more gives us a complete solution though. In .bash_profile (or, really, .bashrc would work at this point too): Code:
[[ -n SSH_CLIENT && -z $SSH_IS_CONNECTED ]] && export SSH_IS_CONNECTED=1 && /path/to/script |
Hi complich8,
thanks for the prompt reply. I have used Code:
[[ -n SSH_CLIENT && -z $SSH_IS_CONNECTED ]] && export SSH_IS_CONNECTED=1 && /etc/script.sh When i open a terminal window though, as you correctly said above, the $SSH_IS_CONNECTED env. variable is not defined. When i connect via SSH then it is. Any suggestions? Thanks in advance. |
Put your commands in ~/.ssh/rc.
Quote:
|
All times are GMT -5. The time now is 04:33 PM. |