Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some help for a newbie 1

Status
Not open for further replies.

mdeal

Technical User
Aug 30, 2002
9
US
Howdy!

I know this is a really lame way to get some help with this, but unix scripting isn't my bag. I'm just trying expand my horizons a little bit and have had a lot of success and help from other forums here. So, here goes, I need a bourne shell script named SPY that will let me know what users are online, and whose output will contain a list of system login IDs and what they are currently doing (any users not logged on will not be displayed.) The script should be called like:
spy username1 username2

Any help would be super. Thanks.

Mike D.
 
Mike:

Most every unix flavor has the "w" command, display info about what users are on the system and what they are doing. Do a:

man w

to see all the options.

Regards,

Ed
 
Ed,

Thanks for the help. What I need to know is how to do this with the user I want to look up entered following the script call, such as:

spy username1 username2 username3

Like I said, I'm not at all very familiar with the syntax, so a slow walk through would be most helpful, but any help at all is greatly appreciated.

Thanks,
Mike
 
create an executable (chmod it 777) script called spy:

#!/bin/ksh
w|egrep "$1|$2|$3"

then run it !
./spy fred bert joe

HTH ;-)


Dickie Bird
Honi soit qui mal y pense
 
Thanks a ton for the help, but I've one more quick question. How would I do this same thing, but with an unknown number of userIDs that could possibly be input. Say one time there may be 10 userIDs looked up and the next 20 or 4, you get the idea. Thanks again for all the help.

Mike D.
 
#!/bin/ksh

w | egrep "`echo ${@} | sed -e 's/ /\|/g'`"
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
You can do up to nine in one line

#!/bin/ksh
w|egrep &quot;$1|$2|$3|$4|$5|$6|$7|$8|$9&quot;

and if you entered only 1 or 2 or 9 users, that would be OK
for more than 9 usernames, you'd have to use 'shift' - see the man pages
HTH ;-)


Dickie Bird
Honi soit qui mal y pense
 
Thanks for the help, can you tell me what that means?

Mike D.
 
Vlad wins again - I'd forgotten about $@ Dickie Bird
Honi soit qui mal y pense
 
Thanks for the help folks, it works, but how can I run the script like:
spy

Instead of:
./spy

Thanks,
Mike
 
check your ${PATH} environment variable to include the directory where &quot;spy&quot; lives [no pun inteded].
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanks Vlad, ready for round two now, I need to return an error message if the script isn't called properly, for instance:

spy
spy: arguments required for usage
usage: spy username1 username 2 ...

Any help would be most helpful.

Mike
 
#!/bin/ksh

usage()
{
cat 2>&1 <<EOF

${0}: arguments required usage
usage: ${0} username1 username2 ...
EOF

}

if [ &quot;${#}&quot; -eq 0 ] ; then
usage;
exit 1
fi;

w | egrep &quot;$(echo ${@} | sed -e 's/ /\|/g')&quot;
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top