Extract tty from ps command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract tty from ps command
# 1  
Old 02-19-2012
Extract tty from ps command

When I try to extract tty from ps command , at time we get output , at times we dont.
for eg i executed below quesry continulusly for some time,Actually i feel its because sometime pid allocated has some additional space at begining which causes this issue.
->
Code:
ps | grep "/-ksh" | tail -1 | cut -f4 -d' '

echo $a
pts/2
->
Code:
ps | grep "/-ksh" | tail -1 | cut -f4 -d' '

is there foolproof way to extra tty , cannot use tty or whoami
# 2  
Old 02-19-2012
You could try:

Code:
ps | grep "/-ksh" | tail -1 | awk '{print $2}'

# 3  
Old 02-20-2012
I think it is the classic grep your own grep. You should add a grep -v grep or use square brackets around one of the characters in the search string to avoid grepping your own grep command. Also, shouldn't it be \- instead of /- ? We could combine this like this:
Code:
ps | grep "[-]ksh" | ...

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python: Redirecting to tty and reading from tty

In bash, you can do something like this: #!/bin/bash echo -n "What is your name? " > /dev/tty read thename < /dev/tty How can I do the same in python? I have a python script that has the following content: #!/usr/bin/python2.7 import getpass import sys import telnetlib import... (2 Replies)
Discussion started by: SkySmart
2 Replies

2. Shell Programming and Scripting

need extract command

Hi, I am having an file 20110909.tar.gz (path-home/user01/archive/20110909.tar.gz ), contains some log files some csv files I want to extract that files . if i run "ls" command then their should be display all the log files and csv files on the same path. please help me to know the... (4 Replies)
Discussion started by: aish11
4 Replies

3. Shell Programming and Scripting

How to extract a field from ls-l command and display?

So I want to put a line at the end of my script which greps for keywords from syslog.log that outputs the following after it is done: "This file was last modified on (thisdate)" I know I can use the following to get the date: rtidsvb(izivanov):/home/izivanov> ll /var/adm/syslog/syslog.log ... (4 Replies)
Discussion started by: zixzix01
4 Replies

4. Shell Programming and Scripting

Help with awk command to extract messages

Need expert's opinion urgently. I have a file which contains messages. A message is separated by $ in this file. In each message we need to get string aftr :20: and send it to an output file. If :20: is not prsent in a message then we need to search for :20C: and send the corresponding string... (5 Replies)
Discussion started by: Opamps123
5 Replies

5. Shell Programming and Scripting

expr command to extract words

how to use expr command to retrieve all the words before the equal sign "=" with shell script (3 Replies)
Discussion started by: 76455
3 Replies

6. Shell Programming and Scripting

tty command failing

We have script like this in the .bash_profile.. #-# determine if session is interactive or in background if ]; then while true; do read -p "Do you wish to load profile yes or no?" yn case $yn in * ) source /opt/oracle/.profile; break;; * ) break;; *... (2 Replies)
Discussion started by: talashil
2 Replies

7. Solaris

Command to redirect console to my tty?

Is there a utility built into Solaris that will allow me to see console messages from a tty? I've done a search and see that this is possible through software like ILOM, but I'm looking for a method to do this with built in utilities. For example, on AIX, I can use swcons `tty` (6 Replies)
Discussion started by: makodarear
6 Replies

8. UNIX for Dummies Questions & Answers

Extract time and host IP from 'who' command?

I have been slowly working towards getting a shell window in Cygwin (and/or Gnome and/or KDE) to start up and display the last time it was opened (or sourced) similarly to how the OS X Terminal does so. As close as I've been able to get so far is the 'who' command, but I can't seem to puzzle out... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

9. Shell Programming and Scripting

Extract first file only from ls command

I need help in extracting first file only from an ls command and store it in a local variable I thought using awk we could build something - not very familiar how to do it Something like ls -t $search_string and store the resultant first file alone into a local variable ( as my result... (4 Replies)
Discussion started by: prekida
4 Replies

10. Programming

extract command

hey i want to extract an argument from double qoutes, eg: when the user enters: prompt "mile" i need to extract everything within the double quotes, in my case, mile, and save it to a string. feedback/code would be appreciated thanks mile1982 (1 Reply)
Discussion started by: mile1982
1 Replies
Login or Register to Ask a Question