LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 04-05-2002, 10:38 AM   #1
jaymdouglas
LQ Newbie
 
Registered: Apr 2002
Location: Thunder Bay, Ontario, Canada
Posts: 5

Rep: Reputation: 0
Help with the 'LAST' command!


Allo,

I am hoping that someone could shed some light on my little problem that I have encountered.

I am trying to write a script that when run with no operands lists all the logged in user's online activity for the current month, then at the end of the list show the user's total time for that month.

So far I've got it pulling the LOGNAME and displaying that users times and dates, but I am having trouble in having them calculate the totals.

Here is what I have so far:

echo $LOGNAME > logfile
last | grep `cut –c1-8 logfile` > logfile
awk ‘{print $10}’ logfile > userlogs

cat userlogs

Here are the results:
in
(00:48)
(00:43)
(00:27)

If anyone knows how I can add these together then please shed some light on this subject.

Jay:smash:
 
Old 04-05-2002, 10:49 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
is this homework? I've spent the last 30 mins working out your script really nicely, and i can tell you it works perfectly, but i get the impresion this is for an assignment, so i'm not going to show you unless you can guarentee it's not.

we're not here to do your education for you.

i've had 201 hours 54 minutes logged in on the last 4 days btw...
 
Old 04-05-2002, 11:28 AM   #3
jaymdouglas
LQ Newbie
 
Registered: Apr 2002
Location: Thunder Bay, Ontario, Canada
Posts: 5

Original Poster
Rep: Reputation: 0
Homework…? I can only wish it was something like that...

No, this something that someone asked me to do at work. I'm actually the Visual Basic programmer / ASP guy here, and of course they assumed that I could do it.

My LINUX knowledge is extremely limited.

Your help would be appreciated.

Sorry about the double posting too.

Jay
 
Old 04-05-2002, 11:33 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
hmm.. ok then, i checked your site and you say you're at college and all that...

ok, try this:
Code:
for i in `last | grep $LOGNAME | cut -d \( -f 2 | cut -c 1-5 | grep :`
do
  HOUR=`echo $i | cut -d ":" -f 1`
  MIN=`echo $i | cut -d ":" -f 2 `
  if [ $MIN -lt 10 ] 
  then
    MIN=`echo $MIN | cut -c 2`
  fi
  if [ $HOUR -lt 10 ]
   then
    HOUR=`echo $HOUR | cut -c 2`
  fi
  TOTALMIN=$(($TOTALMIN+$MIN))
  TOTALHOUR=$(($TOTALHOUR + $HOUR))
  if [ $TOTALMIN -gt 60 ]
  then
    TOTALMIN=$((TOTALMIN - 60))
    TOTALHOUR=$((TOTALHOUR + 1))
  fi
  echo `echo $i | cut -d ":" -f 1`:`echo $i | cut -d ":" -f 2`
done
echo ------
echo $TOTALHOUR:$TOTALMIN
You were using intermittent files in your attempts, not really a good idea.

and if anyone can tell me why on earth bash chokes when trying to evaluate $((31+ 08)) (i.e. the '0') that'd be nice... i wrote in those two inner 'if's to remove any leading 0's, which sucks
 
Old 04-05-2002, 06:02 PM   #5
jaymdouglas
LQ Newbie
 
Registered: Apr 2002
Location: Thunder Bay, Ontario, Canada
Posts: 5

Original Poster
Rep: Reputation: 0
Talking

Thank you very much for your insight and your code.

It's working exactly as I hoped.

Jay
 
Old 04-05-2002, 06:08 PM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
rah rah rah. I rock!
 
Old 04-05-2002, 06:09 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
rah rah rah. I rock!

heh, i'm quite proud of that actaully... pretty basic, but it looks kinda impressive... maybe
 
Old 04-06-2002, 09:33 AM   #8
Malicious
Member
 
Registered: Jan 2002
Location: Galveston Island
Distribution: suse, redhat
Posts: 208

Rep: Reputation: 30
Quote:
Originally posted by acid_kewpie

and if anyone can tell me why on earth bash chokes when trying to evaluate $((31+ 08)) (i.e. the '0') that'd be nice... i wrote in those two inner 'if's to remove any leading 0's, which sucks
Most scripting languages interpret leading zeros on numeric items as octal as opposed to decimal. The 8 in 08 is not a valid octal (0-7) number.

echo $((31+08)); - should give error "value too great for base"

echo $((31+10#08)); - works fine.

Just prepend the "10#" (base 10) to all numbers if you expect leading zeros, at least for bash scripts. Other languages may vary.

At least one of these two should work.

HOUR="10#"`echo $i | cut -d ":" -f 1`
MIN=`echo "10#"$i | cut -d ":" -f 2 `
 
Old 04-06-2002, 09:29 PM   #9
jaymdouglas
LQ Newbie
 
Registered: Apr 2002
Location: Thunder Bay, Ontario, Canada
Posts: 5

Original Poster
Rep: Reputation: 0
Now that you guys have gotten me officially hooked on LINUX programming, I wanted to take this script to the next level.

This script displays the total online time for the month that you’re on, how could you modify this code so you could select a particular month. e.g. ‘mylast Mar’ (from what I've read anyway)and then have it display the total online time for that month.

Thanks,

Jay
 
  


Reply



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
Is there a single command to list all hardware installed (command line)? davee Linux - Hardware 6 02-28-2009 07:19 PM
Require Linux/Perl equivalent command for windows Command alix123 Programming 7 08-19-2005 02:23 AM
Key stroke/command to shut down x and go into the command prompt screen? Fear58 Linux - General 1 07-14-2004 07:14 PM
Command to display whole filestructure hierarchy f/ command line? mjewell Linux - Newbie 10 01-19-2004 10:48 AM
Where is Command line utility for Cups and command tutorial mossy Linux - Software 8 01-16-2004 12:24 AM

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

All times are GMT -5. The time now is 02:30 AM.

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