ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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...
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.