LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 12-07-2009, 10:59 AM   #1
wet_weasel
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Rep: Reputation: 0
a script to create login/day stats, based on output of last


hey LQ.

i wont lie to you - this is about homework.

but i will not be asking you for a solution, merely discussing my own, which, in my eyes seems a bit overkill. (why? the course i'm forced to be doing at uni introduced us to _reeeaaaally_ __basic__ bash stuff, the lecturer isn't even aware of printf existing as a bash builtin - echo = GOD. so as you see, he isn't too enlightened in this particular field. but enough of that.)

we were tasked with writing five bash scripts, each of them pretty much dull - the usual ifs, checking for read/write access etc etc. pretty much all four are ENTIRELY about using ifs witht the right switches/flags.

but.

one script is supposed to take the output of last and basing on it, print out the number of logins took place each day.

no problem, right? just read it line by line, awk out the date fields and use a simple counter to count how many times that date occurs. rinse and repeat for every day.

the problem is, we're not supposed to know what awk is. not even what a field is.

here is my question - am I right to think that my awk-based solution is way too complicated for the job and there's something painfully simple i'm missing?

how would you do this?

(not asking for a ready to use script, i can do those on my own. asking for other solution ideas!)

so, what do you think?
 
Old 12-07-2009, 11:17 AM   #2
SethsdadtheLinuxer
Member
 
Registered: Jun 2007
Posts: 152

Rep: Reputation: 37
what about grep and wc in combination? That's probably what the instructors are looking for.
 
Old 12-07-2009, 12:17 PM   #3
wet_weasel
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Original Poster
Rep: Reputation: 0
hmm, good thinking.

even though simpler when it comes to the tools used, this one seems a bit more complicated when it comes to the script itself.

seems i'd have to blindly grep for dates, day after day (a lot of null results when checking rarely used accounts).

this brings up another problem - how far back does last look? i'm confused here, because the system I'm working on atm doesn't even have a /var/log/wtmp file (mac os 10.5) and yet, last somehow manages to gather up all the info.

one more thing that I do not understand: if I do a simple last ben > bens_logins a couple of times in a row, the bens_logins file is different (size-wise) every time. this is also shown by running last | wc -l a few times in a row - usually the number of lines goes up almost twice, and i'm definitely NOT doing that many logins in those few seconds it takes to run last. in fact, i'm not doing any extra logins at all.

why is this?
 
Old 12-07-2009, 12:44 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
As the last command outputs logins in date order and, once the reboot lines and footer are discarded, the day-of-month is always the 6th space-separated field it can be done entirely within shell script.

I figure from the way you asked the question you will study any solution given and thus learn even if you haven't actually written it yourself so I'll break the normal homework questions rule and give a solution (but it's not tested so you may have some learning-by-debugging including avoiding repeating the previous count if the final record is a reboot and handling the last command's final two output lines).
Code:
last_day=0
count=0
while read line
do
    if [[ "${line#reboot}" == "$line" ]]; then
        array=( $line )
        day="${array[5]}"
        if [[ $day != $last_day ]]; then
            [[ $count -ne 0 ]] && echo "On day $last_day: $count logins"
            last_day=$day
            count=1
        else
            let count=count+1            
        fi
    fi
done <<< $( last )
echo "On day $last_day: $count logins"
Please ask about any features of this script that are still puzzling after research.
 
Old 12-07-2009, 01:42 PM   #5
wet_weasel
LQ Newbie
 
Registered: Dec 2009
Posts: 3

Original Poster
Rep: Reputation: 0
thank you, catkin!

for now, there are two things I had to change in your script. one, the day is array[4] and two
Code:
 done <<< $( last )
didn't work well (it sends last's output as one, single line), so I had to do a little workaround using a temporary file (not sure if this is good..):
Code:
last user > logins.tmp
while read line
do
        *code*
done < logins.tmp
which works fine.

the last 'problem' is that your script doesn't deal with months, but i think i'll be able to add that tiny bit of functionality without a problem..

one question so far (pertaining to the done <<< $( last )) line. what is the difference between a single < and <<< ?
 
Old 12-07-2009, 04:33 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,563
Blog Entries: 29

Rep: Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179Reputation: 1179
Quote:
Originally Posted by wet_weasel View Post
one question so far (pertaining to the done <<< $( last )) line. what is the difference between a single < and <<< ?
A single < takes input from a file while <<< takes input from whatever the following expression evaluates to. You could try changing that part of the script to <<< "$( last )" because it might overcome the problem of the expression generating a single line.
 
  


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
shell script ssh: how to eliminate the login process output figo Programming 1 06-01-2009 03:05 PM
Create a Linix script that prints out all failed login attempts. nick_astn Linux - Newbie 3 08-13-2008 06:56 PM
Automation Script !! Create automated archive of files based on name of file! yoshima Programming 5 09-28-2007 06:17 AM
A command or script to rm files based on df output? Anithen Slackware 7 06-21-2007 11:18 PM
Create a login script? jedimastermopar *BSD 4 08-02-2004 09:07 AM


All times are GMT -5. The time now is 11:52 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration