LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-13-2008, 07:55 AM   #1
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371

Rep: Reputation: 30
[solved] How to check if the current shell is where I logged in?


Hi!

Is there a way to check if the current shell is the one where I logged in? (I mean, gave my username and password?)

Last edited by anupamsr; 02-13-2008 at 12:13 PM. Reason: solved
 
Old 02-13-2008, 08:57 AM   #2
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
To get the PID of you running bash:
echo $$

Check if the current bash is loginshell:
ps aux | grep $$

If there is a dash in front of bash, its a loginshell, i.e:
xxx 11740 0.1 0.0 3800 2060 tty2 Ss 15:52 0:00 -bash (This is a loginshell)
xxx 11776 0.0 0.0 3516 1928 tty2 S 15:53 0:00 bash (This is childbash of a loginshell
xxx 11827 0.0 0.0 3496 1880 pts/1 Ss 15:54 0:00 bash (This is a VT in X)
 
Old 02-13-2008, 11:01 AM   #3
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371

Original Poster
Rep: Reputation: 30
Yes but that also lists the grep command!
 
Old 02-13-2008, 11:02 AM   #4
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371

Original Poster
Rep: Reputation: 30
Never mind!

ps x|grep $$|grep -v grep
 
Old 02-13-2008, 12:12 PM   #5
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371

Original Poster
Rep: Reputation: 30
Here, this does the trick:
Code:
if [ `ps o pid,args|grep -ce '^ *'$$' *-bash\( .*\)\?$'` -gt 0 ]; then
    echo login shell
fi
Adding this to .bashrc, now I can switch to zsh except when I want to run bash specifically.

There is still a problem, because many terminal emulators start with login shell. That I don't know how to handle, except by disabling this "feature".

And thanks a lot Dinithion!

Last edited by anupamsr; 02-13-2008 at 12:14 PM.
 
Old 02-13-2008, 01:37 PM   #6
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
A login shell (Where you enter username/password) is assigned a tty device. As you can see in my other post, the one at the bottom is a VT, and is assigned to pts/1, while the other two belongs to tty2. So you could always filter them out by adding tty to ps options.
 
Old 02-13-2008, 02:20 PM   #7
anupamsr
Member
 
Registered: Sep 2004
Location: India
Distribution: Kubuntu, Archlinux, Suse, Gentoo, Mandrake
Posts: 371

Original Poster
Rep: Reputation: 30
Yes, but then the solution will not work if I ssh to another computer because then login shell is assigned a pts.

Since I am using rxvt-unicode, and starting the X server using startx from console, the current solution works nicely.

Last edited by anupamsr; 02-13-2008 at 02:22 PM.
 
Old 02-13-2008, 03:15 PM   #8
Dinithion
Member
 
Registered: Oct 2007
Location: Norway
Distribution: Slackware 14.1
Posts: 446

Rep: Reputation: 59
Oh yeah. I didn't think of that. As long as it works, its good
 
  


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
how to check who has logged on using root darwinianlo Linux - Newbie 5 08-09-2007 06:43 PM
shell script to calculate the time a user is logged in racer_mec Linux - Newbie 2 02-08-2005 11:40 AM
how to View Current Logged In Users scialom Linux - Software 1 10-20-2004 10:13 AM
How to check amount of users logged in CurtisKaj Linux - General 4 08-20-2003 09:46 PM
Talking to others logged in a Shell Road Linux - General 6 03-13-2002 03:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:40 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