Reputation: 1784
I want to set the terminal my script is running in as a variable in a bash shell script. as in tty7, or pts/0 or ttyacm0 etc...
I tried printenv
, sudo printenv
and declare -xp
but in the list I only saw ssh_term. But I know I have a script running in /dev/tty6
so it isnt listing all the terminals in use, just the current terminal.
is there a simple way to list all the shells in use?
UPDATE: who -a seems like all the terminals used in the uptime durration. the ones that say old are the ones where I know there are other scripts running. But what is this +/- business?
j0h - tty6 2014-05-16 07:50 old 9593
LOGIN tty1 2014-05-15 19:10 1675 id=1
j0h + tty7 2014-05-15 19:13 old 1936
Upvotes: 1
Views: 1409
Reputation: 2828
To obtain the current shell that you are using, there is the command
tty
that print the file name of the terminal connected to standard input e.g. /dev/pts/51
To see all the shell you can use w
or who
.
who -a
and who -p
should give you some information more...
Read the man to have a quick view on the possibilities. (You can select the user...)
Update:
Let we say your script is called MyScript.sh
. If you add as a 1st line
#!/bin/bash
you change the attribute
chmod u+x MyScript.sh
and you execute it with ./MyScript.sh
later you can search directly them with
pgrep -wal MyScript.sh
(It will return the pid of the processes)
Upvotes: 1
Reputation: 2160
If I have understood the problem correctly you are looking for terminals used by a particular script, if so you can use something like:
x=($(ps aux | grep script_name)| awk '{print $7}') #you may have to check which column to filter
all terminals used by script would be in array x then you can
for i in ${x[*]}
do
echo $i
done
for getting individual values
Upvotes: 1