LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2009, 08:09 PM   #1
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Rep: Reputation: 15
Scripting in Unix - Need Help Starting to Write Script that track Logins


I am very new to Unix but I am learning. I am working on script writing. I know a lot of perl but as far as bash and awk and all the other scripting languages that is what I am trying to learn.

I want to create a script that when it is ran it will execute the last command to display who has logged into my system. I want to capture all these users and display how many times they have logged in.

What is the best language to use and where do I begin. I need some help getting started. I am completely lost.

Thank you
 
Old 03-19-2009, 08:31 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,121

Rep: Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451
Quote:
Originally Posted by circuit_girl View Post
I am very new to Unix but I am learning. I am working on script writing. I know a lot of perl but as far as bash and awk and all the other scripting languages that is what I am trying to learn.

I want to create a script that when it is ran it will execute the last command to display who has logged into my system. I want to capture all these users and display how many times they have logged in.

What is the best language to use and where do I begin. I need some help getting started. I am completely lost.

Thank you
If you know perl, I'd stick with it. "Best" is a relative term, and I prefer Perl for most of the stuff I do.

If you don't want to see system boots, you can do:

Code:
system("last | grep -v boot");
In your Perl program. Then break out the elements into an array, and count the unique elements, like so:

Code:
#!/usr/bin/perl
use warnings;
use strict;

my @bc;
my %count;
while (<DATA>) {
    my $elements = (split /\t/)[0];
    $count{$elements}++;
}

for (keys %count) { print "$_ $count{$_}\n" }
That'll barf out the unique array elements, along with a count of that element. The above assumes you're passing it data, tab-delimited, modify as you see fit.
 
Old 03-19-2009, 08:40 PM   #3
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
Perl's cool, but the other tools are important too. When doing basic stuff in Linux/Unix, you may frequently find uses for a variety of tools, including, e.g., sed, awk, grep, tr, sort, pipes, sub processes, and so on. While awk can be turned to somewhat significant programs, it, and the others, are often used in one liners, with perhaps multiple of them piped together, taking their input from the standard in and sending output to standard out. Most anything you can do as a one liner, you can include in a shell script, and vise versa. Then you learn the flow control commands and so on in the shell.

I tend to work with ksh in Solaris, but lots of people seem to prefer bash.

Whatever other programming tools you learn, the basic tools described above should be part of your standard fare.

Two books I like and use as reference are Unix Power Tools (http://www.amazon.com/UNIX-Power-Too.../dp/1565922603) and Learning the Korn Shell (http://www.amazon.com/Learning-Korn-.../dp/1565920546). Also, Unix in a Nutshell (http://www.amazon.com/Unix-Nutshell-.../dp/0596100299) is always next to my keyboard, almost never in my bookshelf. It has quick reference sections on all the tools.
 
Old 03-19-2009, 08:48 PM   #4
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
TBONE
I get most of your code. In my script I am going to do something like

system 'last'..
how do I store this output so I can go through and check the users. I do not want to list the same user twice. but calculate the total amount of times they have logged on.

Thank you your reply helped a lot
 
Old 03-19-2009, 09:18 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,102

Rep: Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327
With very little modification of @TB0ne's code, you could merely pipe the output of the "last" command in for testing purposes. All (well most of) the work is done for you in that code - seems you need to get comfortable with associative arrays. awk also has them, and it could be used to do this as well.

Personally I prefer hardcopy books when I'm learning a language (even a script one) - but I'm old fashioned I guess.
 
Old 03-20-2009, 09:36 AM   #6
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
Following up on simple solutions using built in tools.

The following line works on my system. Output included.

Code:
$ last | awk '{ print $1 }' | sort | uniq -c

   1 
  17 choogendyk
 101 danahoogendyk
   8 hanna
   9 reboot
  28 rita
   3 root
   8 shutdown
   1 wtmp
 
Old 03-20-2009, 09:32 PM   #7
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,121

Rep: Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451Reputation: 6451
Quote:
Originally Posted by circuit_girl View Post
TBONE
I get most of your code. In my script I am going to do something like

system 'last'..
how do I store this output so I can go through and check the users. I do not want to list the same user twice. but calculate the total amount of times they have logged on.

Thank you your reply helped a lot
You're welcome. Please pass it forward, and help others along the way.
 
1 members found this post helpful.
Old 03-22-2009, 10:33 PM   #8
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
I entered that code and this is my output am I missing something? When I first entered it I was given ">" so I entered it again and got those errors. Please Help.
Code:
myloginname[~]$ last | awk '{ print $1 } | sort | uniq -c
> last | awk '{ print $1 } | sort | uniq -c
awk: { print $1 } | sort | uniq -c
awk:              ^ syntax error
awk: cmd. line:1: last | awk {
awk: cmd. line:1:        ^ syntax error
awk: cmd. line:2: last | awk {
awk: cmd. line:2:             ^ unexpected newline or end of string
myloginname[~]$
 
Old 03-22-2009, 10:50 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
You're missing a ' after { print $1 }.
Kevin Barry
 
Old 03-23-2009, 06:49 AM   #10
choogendyk
Senior Member
 
Registered: Aug 2007
Location: Massachusetts, USA
Distribution: Solaris 9 & 10, Mac OS X, Ubuntu Server
Posts: 1,197

Rep: Reputation: 105Reputation: 105
yup.

when you have not closed a quote, you typically get a ">" prompt on the next line to continue what you are entering. If you weren't expecting it, then it ends up being a prompt to go back and check your syntax/typing. Control-c gets you out of it. Closing the quote gets you out as well, but it's pointless if the original entry is wrong.
 
Old 03-23-2009, 08:51 AM   #11
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by choogendyk View Post
Perl's cool, but the other tools are important too. When doing basic stuff in Linux/Unix, you may frequently find uses for a variety of tools, including, e.g., sed, awk, grep, tr, sort, pipes, sub processes, and so on. While awk can be turned to somewhat significant programs, it, and the others, are often used in one liners, with perhaps multiple of them piped together, taking their input from the standard in and sending output to standard out. Most anything you can do as a one liner, you can include in a shell script, and vise versa. Then you learn the flow control commands and so on in the shell.

I tend to work with ksh in Solaris, but lots of people seem to prefer bash.

Whatever other programming tools you learn, the basic tools described above should be part of your standard fare.

Two books I like and use as reference are Unix Power Tools (http://www.amazon.com/UNIX-Power-Too.../dp/1565922603) and Learning the Korn Shell (http://www.amazon.com/Learning-Korn-.../dp/1565920546). Also, Unix in a Nutshell (http://www.amazon.com/Unix-Nutshell-.../dp/0596100299) is always next to my keyboard, almost never in my bookshelf. It has quick reference sections on all the tools.
Actually, if you know Perl, and nobody/nothing (tiny embedded system ?) limits you in using it, you functionally do not need 'sed', 'awk' - the same functionality is present in Perl and is more convenient.

I think nowadays Perl is part of distribution on any UNIX-like system.
 
Old 03-23-2009, 09:03 AM   #12
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,102

Rep: Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327Reputation: 3327
I was going to let this pass, but I'll toss it in anyway.
Does the piping of commands (as a {valid} solution here) go any way to helping the OP in learning scripting ?. Replace awk with cut, and the association is even more tenuous ...

I prefer the code offered by @TB0ne - with little modification it can fill all the requirements; even some unstated ones like weeding out unnecessary/null entries.
Maybe I'm taking too narrow a view of the initial request ...
 
Old 03-23-2009, 09:14 AM   #13
circuit_girl
Member
 
Registered: Sep 2006
Posts: 35

Original Poster
Rep: Reputation: 15
tbone,
thank you so much it works I am going to research this code. could you explain what each item does. thank you
 
Old 03-23-2009, 09:24 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by syg00 View Post
Does the piping of commands (as a {valid} solution here) go any way to helping the OP in learning scripting ?
think of piping as equivalent to Perl/Python's map() function. think of sed/grep as equivalent to Perl's regular expression engine. think of awk as equivalent to the bunch of Perl's string manipulation functions such as index(), substring() etc...In the end, your view is broadened, if you know what i mean

Last edited by ghostdog74; 03-23-2009 at 09:26 AM.
 
Old 03-23-2009, 10:02 AM   #15
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ghostdog74 View Post
think of piping as equivalent to Perl/Python's map() function. think of sed/grep as equivalent to Perl's regular expression engine. think of awk as equivalent to the bunch of Perl's string manipulation functions such as index(), substring() etc...In the end, your view is broadened, if you know what i mean
The item in bold caused me to think for a minute or so.

I would rephrase it this way: pipe + consequent command (e.g. '| grep foo') acts like Perl/Python 'map' + its processing block (e.g. map {/foo/} @list), where the '@list' is items produced by the command on the left side of pipe.

Well, never mind.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
i need to write a script that automatically logins venix Linux - Newbie 3 03-03-2009 01:34 PM
Strange remote logins after starting a gnome session Clemente Linux - Desktop 2 11-11-2008 02:14 PM
Looking for software to track logins and commands with auto-email DukeLeto Linux - Software 1 07-20-2007 05:14 PM
scripting the unix command script lmcthbe Linux - General 7 05-28-2003 02:49 PM
How to schedule unix script periodically from unix os level??? gopi_20us Programming 2 03-11-2002 06:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration