LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 08-19-2003, 12:54 AM   #1
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Rep: Reputation: 31
Bash: How to tell a tty from a pts?


This is what I have:

#!/bin/bash
echo $(tty)
if "$(tty)" == "*pts*" ;
then

echo yes
fi


What Im getting is:
/dev/pts/1
./test: line 3: /dev/pts/1: Permission denied

What I want is:
/dev/pts/1
yes

Appatently the script is interpreting the line if "$(tty)" == "*pts*" ; as an attempt to access the device rather than as a string comparision.

Anyone know a way to get this script to echo "yes" or how to tell a pts from a tty using bash?
 
Old 08-19-2003, 04:47 AM   #2
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
firstly you are using the if condition in a wrong way...at the time of comparison in the if statement ...it tries to execute the command in the tty variable...
so the right way of using an if statement
if [ $tty = "*pts* ]

second this would not help you compare the two strings as in this case the *pts* doesnt translate to meta-characters..ie...your comparison would always fail..youd have to do some sort of string parsing....awk is the easiest way out....
echo $tty | awk '{s=match($0,"pts");if (s == 0) print ""not found " else print "found",print $0 }'
this will give you the output you want
 
Old 08-19-2003, 05:25 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
so the right way of using an if statement
if [ $tty = "*pts* ]
No. tty is a command, not a variable. So the output of the tty command needs to be captured using backticks: `tty` or using $(tty).

One way to do it:
Code:
#!/bin/bash

if tty | fgrep pts ; then
        echo yes
fi
Or, another:
Code:
#!/bin/bash

TTY=$(tty)
TMP=${TTY%/*}
TMP=${TMP##*/}

echo $TTY
if [ "$TMP" == "pts" ] ; then
        echo yes
fi
 
Old 08-19-2003, 06:16 AM   #4
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
yeah i know that tty is a command, but i sure can take the output of tty in a variable, named tty..by doing tty=`tty`.....that is what i am saying....

but i think you are right ..m0rl0ck is confused how to use the commands and variables in the if statement..

i did not know about the string manipulation like you showed in your later example....

i used awk as i was just dabbling with it for similar purposes right now...
 
Old 08-21-2003, 06:26 AM   #5
m0rl0ck
Member
 
Registered: Nov 2002
Distribution: A totally 133t distro :)
Posts: 358

Original Poster
Rep: Reputation: 31
Thanks for the help
String comparisions in shell dont seem as straight forward as in php or perl.
Decided to go with your example using fgrep, its short and to the point, thanks again.
 
  


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
pts/0, pts/1 devices ?? nadine.mauch Linux - General 1 10-07-2004 11:00 AM
change tty => system crashes (ati driver/tty resolution) mosca Debian 6 08-05-2004 07:37 AM
TTY pts/# terminals increasing. Technoslave Linux - General 1 04-22-2004 01:28 PM
Bash: tty: command not found p0rt Linux - General 5 10-02-2003 11:38 AM
rxvt: can't open slave tty /dev/pts/3 kater Linux - General 2 02-26-2003 06:39 PM


All times are GMT -5. The time now is 04:28 AM.

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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration