LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 11-08-2005, 09:01 PM   #1
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
Extracting terminal/console information


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.
 
Old 11-09-2005, 12:19 AM   #2
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu,Slackware,knoppix
Posts: 323

Rep: Reputation: 37
How about:

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

Code:
HERE=`pwd`
 
Old 11-10-2005, 05:32 PM   #3
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Original Poster
Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
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?
 
Old 11-10-2005, 06:26 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
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 06:30 PM.
 
Old 11-10-2005, 11:47 PM   #5
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu,Slackware,knoppix
Posts: 323

Rep: Reputation: 37
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.
 
Old 11-11-2005, 10:39 PM   #6
Woodsman
Senior Member
 
Registered: Oct 2005
Distribution: Slackware 14.1
Posts: 3,482

Original Poster
Rep: Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546Reputation: 546
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!
 
Old 11-12-2005, 12:34 PM   #7
Jerre Cope
Member
 
Registered: Oct 2003
Location: Texas (central)
Distribution: ubuntu,Slackware,knoppix
Posts: 323

Rep: Reputation: 37
My mistake. For some reason I was thinkng you were running non-X character mode only.
 
  


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

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

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