27

Do you have suggestions how to write a script which detects whether the user specified at the first parameter of the script is logged?

If you are logged on, offer him the opportunity to write a message to the user. Subsequently, as the message sent will be offered the opportunity to write another report or completing the work of the script.

In the case of not user is logged on and the message "" is unknown ...

Thanks.

Denim Datta
  • 3,740
  • 3
  • 27
  • 53
user3012382
  • 279
  • 1
  • 3
  • 7
  • 1
    After using a `who` command to detect user you can just `write $username <<< $message` from the script. – cprn Aug 28 '14 at 14:11

3 Answers3

50

'write' is one of the solution. Run command who

who

the output will be something like

nand   pts/1        2013-11-20 11:59 (:0)
nand   pts/7        2013-11-20 13:09 (:0)

Now you can message to user "nand" on pts/1 using write as

write nand pts/1

Press enter after writing this command then type any message you want to send, the other user will see the output as

Message from nand@mypc on pts/19 at 14:54 ...
hi
hi
hello
  • 7
    You can also pipe to `write`, eg `echo "hello" | write nand pts/1` or cat a file into it - if you want the message to appear all at once. – mxbi Mar 26 '18 at 16:06
8

In Linux everything is treated as file system, Each terminal has its file that can seen by who command.

Eg:

> who

Output:
username   tty7         2016-01-08 10:36 (:0)  
username   pts/0        2016-01-08 12:56 (:0.0)  
username   pts/1        2016-01-08 16:05 (:0.0)  
username   pts/2        2016-01-08 17:10 (:0.0)

Here username pts/0 is special file for 1st terminal(2nd line in output). Data written to this special file will be displayed 1st terminal

Eg:

> write username pts/0

Note: to exit from typing message, use Ctrl+z.

chetan h
  • 85
  • 1
  • 3
1

Try using these commands:

who
mesg
talk
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432