LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-06-2005, 03:44 AM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 32
How to check in a script whether the shell is login or non login?


I am sure this has been asked before, but since I can't find it and since I am sure the answer is quite short....I'll ask again.

Thanks.
 
Old 09-06-2005, 03:54 AM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,798

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
If using bash, try :
Code:
shopt | grep login_shell
 
Old 09-06-2005, 04:09 AM   #3
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
Thanks, I didn't know about shopt...but, now I keep opening xterms (Konsoles to be accurate) and when I 'shopt', in every terminal I open the login_shell toggles are switched to ON. That can't be right. Can it?
 
Old 09-06-2005, 04:20 AM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,798

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
Your shell in konsole is a login shell, but if you start a bash script
without the -l option, it won't be a login shell

try :
Code:
#!/bin/bash
echo $(shopt | grep login_shell)
Then execute the script in your konsole terminal
 
Old 09-06-2005, 04:42 AM   #5
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 32
I see what you are saying. Only if it's a child process it is not a login shell. Well, that certainly doesn't solve my problem.

I guess then I should ask my question as: "How can I check in a script whether the current shell is the one in which I actually logged in, gave my uname and password or any other shell that I opened by starting Xserver and clicking on the xterm icon?"
 
Old 09-06-2005, 06:01 AM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,798

Rep: Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943Reputation: 943
There are some ways, for example echo $TERM, if it is linux, then
you are surelly in a console terminal, so not in X
 
Old 10-21-2015, 09:41 AM   #7
Quantim
LQ Newbie
 
Registered: Jul 2015
Posts: 4

Rep: Reputation: Disabled
To tell if you are in a login shell:

prompt> echo $0
-bash # "-" is the first character. Therefore, this is a login shell.

prompt> echo $0
bash # "-" is NOT the first character. This is NOT a login shell.

Information can be found in `man bash` (search for Invocation). Here is an excerpt:

A login shell is one whose first character of argument zero is a -, or
one started with the --login option.

You can test this yourself. Anytime you SSH, you are using a login shell. For Example:

prompt> ssh user@localhost
fervor@localhost's password:
prompt> echo $0
-bash

The importance of using a login shell is the any settings in `/home/user/.bash_profile` will get executed. Here is a little more information if you are interested (from `man bash`)

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/profile, 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.
 
Old 10-21-2015, 10:09 AM   #8
NevemTeve
LQ Guru
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 5,075
Blog Entries: 1

Rep: Reputation: 1932Reputation: 1932Reputation: 1932Reputation: 1932Reputation: 1932Reputation: 1932Reputation: 1932Reputation: 1932Reputation: 1932Reputation: 1932Reputation: 1932
> How can I check in a script whether the current shell is the one in which I actually logged in, gave my uname and password or any other shell that I opened by starting Xserver and clicking on the xterm icon?

untested:
Code:
if ps h $PPID | grep -q login; then echo login; fi
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use a shell script to do login and other commands jpan Linux - General 15 01-12-2005 06:52 AM
Connection, login and upload files to ftp server via shell script Paulo Góes Linux - Networking 2 02-21-2004 01:01 PM
Shell script login command? chup Linux - General 3 08-24-2003 06:43 PM
login message shell script? jczr2 Linux - Newbie 22 05-28-2003 09:10 PM
telnet login via shell script lethe Linux - General 8 05-13-2002 09:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 PM.

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