How to run a program automatically on my terminal login in unix - unix

Is it possible to write a shell script that automatically starts running whenever i login to my unix terminal? Please provide any reference material

Add your script inside your .profile . .profile is executed whenever you log in. If a .profile currently doesn't exist in your home directory, create one!

Depends on your shell. Assuming you are using bash, you can add the command to .bash_profile in your home directory.

Related

exec bash command in .profile file not letting Control-M job to run

I have an issue where my Control-M job is not able to execute anything on the unix box.
after investigation found out that .profile file in the unix server is the culprit.
content of the .profile file is
exec bash
I tried renaming the file and run the job in UAt and it did work where as I am not sure whats the implication of not having this file.
Can some one pls help me with explaining
what would be the overall impact if I rename the .profile file
how the content of .profile file being used in the server
I don't know what this "Control-M" thing is, but you should be able to safely remove that one-line .profile from your account with no problem. All it does is replace whatever command shell is assigned by default to your account with the command shell called bash. If you don't care if you use the default shell or bash, and especially if you are having problems with that .profile file, then just remove it.
If you really want to use bash then you might try changing your default shell with the chsh command. That may also cause problems for this "Control-M" thing, so you'll want to read the chsh manual page to be sure you know how to determine what your current shell is and how to change back to the original value if there are any problems.

Find and Locate Current .profile

How can I find and/or locate the file and directory of the currently loaded .profile in a bash shell. I made modifications to the current .profile on my machine, and would like to update it some more. I had created the current version in vi and couldn't tell where it was creating the file.
I am hoping that there is a command similar to "whoami"...
Bash executes startup scripts in the following order if you start a login shell:
/etc/profile
.bash_profile under you home directory
.bash_login under you home directory
.profile under you home directory
And .bashrc under your home dir in case of non-login shell.
So, probably, you just have to run vi ~/.profile

how to open another file from within vim as a different user (sudoedit | vim )

so I've recently learned about sudoedit and how I can edit a file more safely than the standard "sudo vim".
the problem is now, when I'm in vim and "vsplit" or "tabnew" I open it as my user account (no root privileges)
sudoedit launches a separate instance of Vim, because it has to manage the lifecycle of the editing session; i.e. write back the edited temporary file with root priviledges. It cannot achieve that from a running Vim session.
However, there are plugins that achieve sudoedit-like functionality, for example the aptly named SudoEdit.
Maybe you just want a option to save file as sudo.
You can find mapings for write file as sudo or use tpope enuch plugin.
You will get :SudoWrite and :SudoEdit commands and couple more.
vim-enuch

Added Alias to .bashrc but no results

I added an alias (alias homedir='cd /export/home/file/myNmae'
) to .bashrc in my home directory and restarted the session. When I run the alias it says homedir: command not found.
Please advice.
This is because .bashrc is not sourced everytime, only for interactive non login shells .bashrc is sourced.
From the bash man page.
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/pro-
file, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the
first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
When a login shell exits, bash reads and executes commands from the files ~/.bash_logout and /etc/bash.bash_logout, if the files exists.
When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the
--norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
i found the solution - i added it to the .profile file and restarted the session - it worked

Unix: Getting Export PATH to "Stick"

When setting the export path in Unix, example:
export PATH=$PATH: $EC2_HOME/bin
If I quit terminal and open it back up to continue working, I have to go through all the steps again, setting up the paths each time.
I'm wondering how I can set the path and have it "stick" so my system knows where to find everything the next time I open terminal without having to do it all over again.
Thanks!
Open ~/.bashrc. This file is loaded every time you start up a new shell (if you're using Bash, which most people are). If you're using a different shell, the file may have a different name, like ~/.shrc.
Add the line you need to the bottom of the file:
export PATH=$PATH:$EC2_HOME/bi
Other info rolled up from elsewhere in the thread:
There are multiple places to put this, depending on your shell and your needs. All of these files are in your home directory:
For Bash:
.bashrc (executed when you shart a shell)
OR
.bash_profile (executed when you log in)
For csh and tcsh:
.cshrc
For sh and ksh:
.profile
Add it to your .cshrc file (for csh and tcsh), .profile file (for sh and ksh), or .bash_profile file (for bash)
You need to find your profile file and put that line in there. Suppose you use bash, the profile files are .bashrc and .bash_profile, found in ~. These files will vary depending on which shell you use.
You have to put those commands into one of the "autostart" files of your shell.
For bash this would be .bashrc in your homedirectory (create it if necessary)
add it to your .bashrc or another .bash startup file.
... and for ksh edit .profile.

Resources