Sunday, December 27, 2009

Writ unix Shell programming Write shell program to display list of users logined to system?

The ';who'; command will generate a list of users


who are currently logged into the system, ie:





聽聽聽聽$ who


聽聽聽聽joe pts/4 Nov 27 10:35 (bearclaw)


聽聽聽聽fred pts/5 Nov 27 14:04 (:0.0)


聽聽聽聽fred pts/12 Nov 26 10:44 (:0.0)


聽聽聽聽tom pts/6 Nov 27 14:07 (chicken)





Since you only want the users (and presumably do


NOT want the other output fields), we can filter


this output (ie. using ';awk';).





Furthermore, to remove duplicate user names, we


can filter the output through ';uniq';:





聽聽聽聽$ cat /tmp/users


聽聽聽聽#!/bin/sh


聽聽聽聽


聽聽聽聽who | awk '{ print $1; }' | uniq


聽聽聽聽


聽聽聽聽$ chmod +x /tmp/users


聽聽聽聽


聽聽聽聽$ /tmp/users


聽聽聽聽joe


聽聽聽聽fred


聽聽聽聽tom





Tested on both Solaris and Linux.

No comments:

Post a Comment