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? |
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 |
Quote:
One way to do it: Code:
#!/bin/bash Code:
#!/bin/bash |
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...:) |
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. |
All times are GMT -5. The time now is 11:03 PM. |