LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices

Reply
 
Thread Tools
Old 11-08-2005, 10:01 PM   #1
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 12.2 (2.6.27.7)
Posts: 1,846
Thanked: 92
Extracting terminal/console information


[Log in to get rid of this advertisement]
I have only three tty consoles enabled in my /etc/inittab. This is a single-user workstation and that works great for me. However, how could I determine how many consoles are available from within a bash script? Is there a handy way to do this?

In a related question, seems I once came across a method to determine the current vt number. For example, whether I am in vt1, vt2, vt7, etc. How do I determine that?

Thirdly, not related to the previous questions, how do I determine the directory location from which a script is being launched? That is, if I launch a script located in /usr/local/bin, within that script itself how do I programmatically determine the directory from which it was launched?

Thanks again.
Woodsman is offline     Reply With Quote
Old 11-09-2005, 01:19 AM   #2
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu, OpenSUSE,Slackware,knoppix
Posts: 286
Thanked: 3
How about:

Code:
NUMVT=`grep vt /etc/inittab | grep -v "^#" | awk '{print NR}'`
and maybe:

Code:
HERE=`pwd`
Jerre Cope is offline     Reply With Quote
Old 11-10-2005, 06:32 PM   #3
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 12.2 (2.6.27.7)
Posts: 1,846
Thanked: 92

Original Poster
Quote:
NUMVT=`grep vt /etc/inittab | grep -v "^#" | awk '{print NR}'`
Yes, that works, more or less, although I needed to change the 'vt' to 'tty'. I have archived your string for future reference. This is a start.

However, I explained myself poorly in my post. I want to determine the X virtual terminal number. When X starts it automatically assigns the virtual terminal it uses based upon the number of ttys assigned in inittab. Thus, if one has six ttys assigned in inittab, then X will assign the next virtual terminal number to 7. If one has only three ttys assigned, then X assigns the next virtual terminal to 4, unless overridden manually. This is the terminal number I hope to determine and I want to calculate this number before launching X in startx. Thus, how does X calculate this number on the fly? I'd like to do the same thing.

Regarding the directory location of a script, I don't think the pwd command will work. Suppose I store a script in /usr/local/bin and I run that script. How can I programmatically determine that the script is stored in /usr/local/bin to inform the user with a message of that location?
Woodsman is offline     Reply With Quote
Old 11-10-2005, 07:26 PM   #4
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,282
Thanked: 1
Quote:
How can I programmatically determine that the script is stored in /usr/local/bin to inform the user with a message of that location?
Try this :
Code:
#!/bin/bash

me=$0

# check if we are in the script directory
if [ "x${me:0:2}" = "x./" ]; then
    me="$(pwd)${me:1}"
fi

echo "I am : $me"
echo "My directory is : $(dirname $me)"
[edit]
Now that I am thinking maybe you want to add another test in the
case of the script get symlinked ?

Last edited by keefaz; 11-10-2005 at 07:30 PM..
keefaz is offline     Reply With Quote
Old 11-11-2005, 12:47 AM   #5
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu, OpenSUSE,Slackware,knoppix
Posts: 286
Thanked: 3
Quote:
I want to determine the X virtual terminal number. When X starts it automatically assigns the virtual terminal it uses based upon the number of ttys assigned in inittab.
I think the tty command is what you need for the one. I think you'll find a correspondance between the inittab label the the tty command output.

The other command I think you are looking for is which, although there the executable lives and where the executable is invoked from may be two different places. Also the executable may change the environment itself.

Recently, I've taken up perl to manage this sort of thing, after years of shell scripts and awking. The code tends to be a little cleaner in perl when you're finished.
Jerre Cope is offline     Reply With Quote
Old 11-11-2005, 11:39 PM   #6
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 12.2 (2.6.27.7)
Posts: 1,846
Thanked: 92

Original Poster
Quote:
Try this :
I tried the code in three different directories and received perfect results. Good job and thank you! Although I now have to hit the bash guides to fully understand what is happening. Then later I might throw that routine into /etc/bashrc as a function.

Quote:
I think the tty command is what you need for the one. I think you'll find a correspondence between the inittab label the the tty command output.
Sort of. Maybe. I don't know! In non-X the tty command returns the tty number in the form of /dev/ttyn. Good enough. In X the same command returns /dev/pts/n. Additionally, I can get tty to return some interesting results when in X.

I use only three ttys in my inittab. This evening I started X in run-level 4. Despite using only 3 ttys, because of the way I edited the KDM Xservers file, vt7 is the virtual terminal number. So far so good. I toggled to tty3 and started X from there with startx. The virtual terminal number is vt8. I opened Konsole and typed tty. I received /dev/pts/0. I toggled to my first X session at vt7/tty1. I opened Konsole and tty returned /dev/pts/1.

I then toggled to my second X session, vt8, and exited KDE/X. I returned to X session tty1/vt7. I opened Konsole and tty returned /dev/pts/0.

I then opened a new KDE/X session using the Start New Session menu option, which gets assigned to vt8. I opened Konsole in that session and tty returned /dev/pts/0. I toggled to tty3 and again manually started X with startx. I opened Konsole and tty returned /dev/pts/1. I toggled to my first KDE/X session at vt7 and tty returned /dev/pts/2.

The preliminary indication is that when in X, the tty command responds in the order of who makes the request. The actual tty or vt is irrelevant. In non-X the tty command works as expected. Is this a bug or a feature? I don't know!

Despite this momentary fun I'm still looking for a way to programmatically determine the virtual terminal (vt) number that X assigns.

Quote:
Recently, I've taken up perl to manage this sort of thing, after years of shell scripts and awking. The code tends to be a little cleaner in perl when you're finished.
Possibly one day I will look at perl in earnest. Too much else on my plate right now!
Woodsman is offline     Reply With Quote
Old 11-12-2005, 01:34 PM   #7
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu, OpenSUSE,Slackware,knoppix
Posts: 286
Thanked: 3
My mistake. For some reason I was thinkng you were running non-X character mode only.
Jerre Cope is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

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
Terminal Information Files or directory ankush174 Red Hat 1 07-02-2004 03:40 PM
Extracting tar.bz2 archive through console grim_chel Linux - Newbie 3 06-28-2004 12:27 PM
problem in recieving information from a terminal device dinesh_2001 Linux - Networking 0 02-13-2004 10:36 AM
Extracting From Console Nern Linux - Newbie 2 10-01-2003 08:30 AM
getting information from a webpage in terminal charlie123 Linux - Newbie 1 01-31-2003 06:58 AM


All times are GMT -5. The time now is 03:08 PM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration