Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
10-05-2015, 06:33 PM
|
#1
|
Member
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106
Rep:
|
Knowing that bash is logging in within .bashrc
Typically the user's .bash_profile script has logic in it to invoke the user's .bashrc if it exists. The .bashrc script is also invoked each time a user opens a terminal. What I want to do is to have the logic of my .bashrc file be different between when it is initially called from .bash_profile and otherwise. Is there something I can test in my .bashrc in order to know that bash is being invoked at log-in time or not ?
|
|
|
10-06-2015, 05:26 AM
|
#2
|
Senior Member
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
|
Something like this may work:
Code:
if [[ $(shopt -q login_shell) ]]
then
...
fi
Though I do suppose there's a reason that the distro's default profile doesn't check it to begin with.
Last edited by goumba; 10-06-2015 at 05:40 AM.
Reason: Spelling and other idiotic stuff.
|
|
|
10-06-2015, 05:27 AM
|
#3
|
LQ Newbie
Registered: Mar 2015
Posts: 17
Rep: 
|
Try somethung like
who|grep `id -un`|wc -l
and test result for greater than 1. It just counts how many times your logged in. Might not work if gui does not maintain utmp.
|
|
|
10-06-2015, 11:40 PM
|
#4
|
Member
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106
Original Poster
Rep:
|
Quote:
Originally Posted by goumba
Something like this may work:
Code:
if [[ $(shopt -q login_shell) ]]
then
...
fi
Though I do suppose there's a reason that the distro's default profile doesn't check it to begin with.
|
Looks like the right way to do it. Thanks !
|
|
|
10-07-2015, 01:27 AM
|
#5
|
Member
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106
Original Poster
Rep:
|
Quote:
Originally Posted by goumba
Something like this may work:
Code:
if [[ $(shopt -q login_shell) ]]
then
...
fi
Though I do suppose there's a reason that the distro's default profile doesn't check it to begin with.
|
Unfortunately it does not work. The '$(shopt -q login_shell)' path never gets invoked, even when called from .bash_profile.
|
|
|
10-07-2015, 02:24 AM
|
#6
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,250
|
How do you know that? (try without -q)
|
|
|
10-07-2015, 07:21 AM
|
#7
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,815
|
Quote:
Originally Posted by goumba
Something like this may work:
Code:
if [[ $(shopt -q login_shell) ]]
then
...
fi
|
What that is doing is running shopt with the "-q" option, which suppresses the output, and then testing whether that output is non-null. That will never be true.
This should work:
Code:
if shopt -q login_shell
then
...
fi
|
|
|
10-07-2015, 08:26 AM
|
#8
|
Senior Member
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
|
Quote:
Originally Posted by rknichols
What that is doing is running shopt with the "-q" option, which suppresses the output, and then testing whether that output is non-null. That will never be true.
[/code]
|
That's true. We wanted to test the return code. My mistake, doing things rushed again. My apologies to the OP.
In penance, I also offer than the OP may test the first character of argument 0 ($0) for a hyphen ("-").
*slinks under a rock*
Last edited by goumba; 10-07-2015 at 08:32 AM.
|
|
|
10-07-2015, 03:52 PM
|
#9
|
Member
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106
Original Poster
Rep:
|
Quote:
Originally Posted by rknichols
What that is doing is running shopt with the "-q" option, which suppresses the output, and then testing whether that output is non-null. That will never be true.
This should work:
Code:
if shopt -q login_shell
then
...
fi
|
That does work. Thank you !
|
|
|
10-07-2015, 03:55 PM
|
#10
|
Member
Registered: Nov 2006
Distribution: Mepis, CentOS, OpenSuse
Posts: 106
Original Poster
Rep:
|
Quote:
Originally Posted by goumba
That's true. We wanted to test the return code. My mistake, doing things rushed again. My apologies to the OP.
In penance, I also offer than the OP may test the first character of argument 0 ($0) for a hyphen ("-").
*slinks under a rock*
|
Apology accepted <g>. Isn't argument 0 the process name ? What does starting with a hyphen mean ?
|
|
|
10-07-2015, 07:44 PM
|
#11
|
Senior Member
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
|
Quote:
Originally Posted by eldiener
Apology accepted <g>. Isn't argument 0 the process name ? What does starting with a hyphen mean ?
|
In this case, $0 is the name of the shell, if invoked via other than -c, which will be some form of bash. I have gotten both bash and /bin/bash on the same system, the former in a text console, the latter in a terminal under X. However, the following is always true, from the bash man page:
Code:
INVOCATION
A login shell is one whose first character of argument zero is a -, or
one started with the --login option.
So, in a login shell, echo $0 will return "-bash"; otherwise, simply "bash".
Last edited by goumba; 10-07-2015 at 07:54 PM.
Reason: Removed extranneous info.
|
|
|
10-08-2015, 01:41 AM
|
#12
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,250
|
Quote:
Originally Posted by goumba
So, in a login shell, echo $0 will return "-bash"; otherwise, simply "bash".
|
No, unfortunately it is not so simple, just try to execute/source a shell script ($0 will be the name of the script itself).
But $0 will be equal to -bash in case of a login shell.
|
|
|
10-08-2015, 07:45 AM
|
#13
|
Senior Member
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
|
*sgh* Alas, you are right, as like a dummy, I did my testing with bashrc and with interactive shells. The hyphen being the first character is right, however.
Last edited by goumba; 10-08-2015 at 11:59 AM.
|
|
|
10-08-2015, 09:16 AM
|
#14
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,815
|
But, the OP was talking about doing this in .bashrc, which is sourced by the running shell. $0 and the login_shell option would have their values for that shell. Now you're talking about doing this in some other shell script, which would be interpreted by a different shell instance, not the login shell.
|
|
|
10-08-2015, 11:59 AM
|
#15
|
Senior Member
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
|
Quote:
Originally Posted by rknichols
But, the OP was talking about doing this in .bashrc, which is sourced by the running shell. $0 and the login_shell option would have their values for that shell. Now you're talking about doing this in some other shell script, which would be interpreted by a different shell instance, not the login shell.
|
I'm not sure if you're referring to my post, as yours is right under mine, but I made no mention of other scripts. :/
|
|
|
All times are GMT -5. The time now is 05:29 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|