LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-19-2008, 01:05 PM   #1
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Rep: Reputation: 164Reputation: 164
Run script from all accounts after login


Probably a dumb brainfart question ...

I created a script that sets up a bash prompt. Currently I run it from each user's .bashrc file. I would like to have it run universally (each time any user logs in) as if run from that user's .bashrc.

I looked at /etc/profile. It appears that only gets processed when logged in as root.

So, where would I put such a command?

P.S. Please don't say rc.local, as this is run BEFORE login.
 
Old 05-19-2008, 01:12 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Why not in the global bashrc? For example on OpenSuSE it is /etc/bash.bashrc, on Fedora /etc/bashrc and so on. This is usually the place where PS1 is set. You can change it at your pleasure.
 
Old 05-19-2008, 01:33 PM   #3
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by colucix View Post
Why not in the global bashrc? For example on OpenSuSE it is /etc/bash.bashrc, on Fedora /etc/bashrc and so on. This is usually the place where PS1 is set. You can change it at your pleasure.
Yeah, so I guess I figured that one out at about the same time you were replying to me. Heh. But now I'm a bit more confused.

I copied the script I originally had in my .bashrc into a new file and gave the file execute permission. Now when I log in I get the junk default prompt, which is expected as I removed the code from .bashrc.

Now, I've got the same code in this executable file, but when I manually run it it does not reset my prompt. Note, that I recopied the script I placed in the executable back into my .bashrc and it works as before. So I know there's not a problem with the script.

Here's the code and execute status of the file:
Code:
$ cat SetCommandPrompt
PROMPTCOLOR='\[\e[1;37m\]'
COMMANDCOLOR='\[\e[0;37m\]'
ROOTCOLOR='\[\e[1;31m\]'
NORMALUSERCOLOR='\[\e[1;32m\]'
HOSTNAMECOLOR='\[\e[1;35m\]'
DIRECTORYCOLOR='\[\e[1;33m\]'

### username ###
if [ `whoami` = "root" ]
        then USERCOLOR=$ROOTCOLOR ; PROMPTCHAR="#"
        else USERCOLOR=$NORMALUSERCOLOR ; PROMPTCHAR="\$"
fi
PS1=$USERCOLOR"\u"

### hostname ###
#PS1="$PS1$PROMPTCOLOR@"
#PS1="$PS1$HOSTNAMECOLOR$(hostname -s)"

### working directory ###
PS1="$PS1$PROMPTCOLOR ["
PS1="$PS1$DIRECTORYCOLOR\w"
PS1="$PS1$PROMPTCOLOR]"

### prompt end marker ###
PS1="$PS1$USERCOLOR $PROMPTCHAR "

### command color ###
PS1="$PS1$COMMANDCOLOR"

### export ###
export PS1
Code:
$ ls -la SetCommandPrompt 
-rwxrwxr-x 1 **** **** 916 2008-05-19 14:21 SetCommandPrompt

Last edited by SlowCoder; 05-19-2008 at 02:27 PM.
 
Old 05-19-2008, 02:20 PM   #4
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
Ok, so copying the contents of my script into /etc/bashrc allows it to work. This is not optimal for me, as I would like to be able to call the script from bashrc, not copy it. Almost like I can't set PS1 in anything but a bashrc file?

Leads me to another question. How does bash execute /etc/bashrc and ~/.bashrc if they are not executable?
 
Old 05-19-2008, 02:31 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
You have to source it, not execute it. If you execute it, the variable PS1 is set only within the scope of the script itself. Therefore in the /etc/bashrc you can put
Code:
. SetCommandPrompt
where the dot in bash stats for the source command. For this reason the x permission is not necessary since the script is not actually executed, but simply "included" in the current shell.

Edit: I checked and in bash you can also use the command source, which is equivalent to the dot. I used the dot in the Bourne Shell /bin/sh where the command source was not available.

Last edited by colucix; 05-19-2008 at 02:34 PM.
 
Old 05-19-2008, 02:49 PM   #6
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by colucix View Post
You have to source it, not execute it. If you execute it, the variable PS1 is set only within the scope of the script itself. Therefore in the /etc/bashrc you can put
Code:
. SetCommandPrompt
where the dot in bash stats for the source command. For this reason the x permission is not necessary since the script is not actually executed, but simply "included" in the current shell.

Edit: I checked and in bash you can also use the command source, which is equivalent to the dot. I used the dot in the Bourne Shell /bin/sh where the command source was not available.
That worked. Love you!

What is the difference between executing and sourcing? In this way, it's like I included my script as part of the bashrc script?

Also, I thought the export command in "export PS1" is supposed to export the PS1 variable to the shell, so that it isn't only available inside the script?
 
Old 05-19-2008, 03:05 PM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by SlowCoder View Post
What is the difference between executing and sourcing? In this way, it's like I included my script as part of the bashrc script?
Exactly!
Quote:
Also, I thought the export command in "export PS1" is supposed to export the PS1 variable to the shell, so that it isn't only available inside the script?
No. The export command inside a script makes a variable available to all the subsequent commands (this is useful when you execute a script from inside another script). In other words it creates the environment for the child processes, but does not change the environment back to the parent.

If you refer to the export command inside the initialization files, like /etc/bashrc or /etc/profile, remember that they are sourced from the shell. This is equivalent to run export from the parent shell.

Last edited by colucix; 05-19-2008 at 03:10 PM.
 
Old 05-19-2008, 03:13 PM   #8
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
Ok, colucix,

I'm learning here. Figuring out bash scripting, and you're helping me a lot. Thanks.

So, for clarification ...

- Sourcing a file is effectively the same as placing a copy of the file inside your current script. Like including a function?

- There is not a way in bash to replace the value of an environment variable with one generated in a script?
 
Old 05-19-2008, 03:24 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by SlowCoder View Post
I'm learning here. Figuring out bash scripting, and you're helping me a lot. Thanks.
You're welcome!
Quote:
So, for clarification ...

- Sourcing a file is effectively the same as placing a copy of the file inside your current script. Like including a function?
Yes. From the bash builtins man page: source: read and execute commands from filename in the current shell environment. That is, instead of writing commands inside the script, they are written in an extrenal file. Exactly like including a function!

Quote:
- There is not a way in bash to replace the value of an environment variable with one generated in a script?
As far as I know... no. Cheers!
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the script to run when first login zpcanada Linux - General 1 08-31-2006 12:33 AM
run script during login Lotharster Linux - General 5 06-12-2006 02:34 PM
how to monitor all accounts run all commands from login to present? BRAHmS Solaris / OpenSolaris 4 08-20-2004 12:00 PM
run a script once at login? masand Linux - Software 4 05-02-2004 09:41 AM
run a script once at login? shanenin Linux - Software 2 04-25-2004 10:32 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration