How to calculate time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to calculate time
# 1  
Old 11-11-2011
How to calculate time

Hello Guys,
I am trying to calculate total hours and minutes a given user has used the system since the beginning of the current month.
Code:
#!/usr/bin/sh
hr=0
min=0
last $1 | grep -w `date "+%b"` | grep -v '\(0[2-9]:.*\)' | grep -vw sshd | cut -c 66-
| tr -d "[ ]\(\)" | cut -f1 -d ":" | grep -v '[a-z].*' | while read line
do
        hr=`expr "$hr" + "$line"`
        echo User $1 spent total $hr
done
last $1 | grep -w `date "+%b"` | grep -v '\(0[2-9]:.*\)' | grep -vw sshd | cut -c 66-
| tr -d "[ ]\(\)" | cut -f2 -d ":" | grep -v '[a-z].*' | while read line
do
        min=`expr "$min" + "$line"`
         if [ $min -ge 60 ]
        then
                min=`expr "$min" - 60`
                hr=`expr "$hr" + 1`
         echo User $1 spent $hr hours and $min minutes the current month.
        continue
        fi
done
echo "User $1 spent $hr hours and $min minutes the current month."

My code is working fine. I have problem with variable. I can't use variable outside loop. It showing 0 hours and 0 minutes.

Thanking You,

Last edited by kasparov; 11-11-2011 at 08:28 PM.. Reason: typos, spelling
# 2  
Old 11-11-2011
Please display the output of your "last" command.
# 3  
Old 11-12-2011
Yes, the variable will not be available... this is a known issue... If you see your code, while is piped from the cut command so it has its own shell(subshell) where the execution happens...

--ahamed
# 4  
Old 11-12-2011
One of the advantages that Korn shell has over bash is that this is not a problem for it. If you aren't required to use bash, then you might consider switching and using Kshell.
# 5  
Old 11-12-2011
It's not a "bash" thing, it's an "everything except certain versions of ksh" thing. Demanding a specific version of a specific shell everywhere you go doesn't strike me as a good idea.

Two solutions.

1) temp file:
Code:
a | b | c | d > /tmp/$$
while read LINE
do
...
done < /tmp/$$
rm /tmp/$$

2) refactor your code so you're not using 9-long pipe chains. This is probably a good idea anyway.
# 6  
Old 11-12-2011
3. New Features in Bash (4.2)
...
t. There is a new `lastpipe' shell option that runs the last command of a pipeline in the current shell context. The lastpipe option has no effect if job control is enabled.
# 7  
Old 11-12-2011
Demanding a specific version of a specific shell everywhere you go doesn't strike me as a good idea.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate Time diff in milli milliseconds(Time format : HH:MM:SS,NNN)

Hi All, I have one file which contains time for request and response. I want to calculate time difference in milliseconds for each line. This file can contain 10K lines. Sample file with 4 lines. for first line. Request Time: 15:23:45,255 Response Time: 15:23:45,258 Time diff... (6 Replies)
Discussion started by: Raza Ali
6 Replies

2. Programming

How to calculate compilation time using c?

is there function with library "time.h" to calculate the processing time of the code? (3 Replies)
Discussion started by: fwrlfo
3 Replies

3. Shell Programming and Scripting

calculate time from a given format

Hi i have a file which consists of the time records in following format H:MM:SS.sss 0:00:09.249 0:00:00.102 0:00:00.105 0:00:08.499 0:00:08.499 0:00:06.980 0:00:04.249 0:00:05.749 0:00:00.108 0:00:00.107 0:00:03.014 0:00:00.000 I need to calculate their equivalent milliseconds... (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

4. Shell Programming and Scripting

Calculate age of a file | calculate time difference

Hello, I'm trying to create a shell script (#!/bin/sh) which should tell me the age of a file in minutes... I have a process, which delivers me all 15 minutes a new file and I want to have a monitoring script, which sends me an email, if the present file is older than 20 minutes. To do... (10 Replies)
Discussion started by: worm
10 Replies

5. Shell Programming and Scripting

How to calculate time difference between start and end time of a process!

Hello All, I have a problem calculating the time difference between start and end timings...! the timings are given by 24hr format.. Start Date : 08/05/10 12:55 End Date : 08/09/10 06:50 above values are in mm/dd/yy hh:mm format. Now the thing is, 7th(08/07/10) and... (16 Replies)
Discussion started by: smarty86
16 Replies

6. Programming

Calculate time to some date

Hello! I need to find how many days, hours and minutes remain to some specific date and I wonder why the following program shows incorrect values, such as 4 days 23 hours etc to 14:00 this Saturday from 17:33 today... #include <stdio.h> #include <time.h> int main() { time_t elaps,... (4 Replies)
Discussion started by: Sapfeer
4 Replies

7. UNIX for Dummies Questions & Answers

calculate the time

hello i want to display the time firstly when i run my shell script and after 25 min i want to display a message it says that the time left is 5 min. When the calculated time is 30 mins, the script should exit. can any one help me with that! Thanks in advance Regards :o (5 Replies)
Discussion started by: dndoon
5 Replies

8. Shell Programming and Scripting

How to calculate this time difference

Hi, Please help me in calculating the time difference between below mentioned timestamps. a=07/17/2007 02:20:00 AM MST b=07/17/2007 02:07:46 AM MST Thanks (2 Replies)
Discussion started by: Prat007
2 Replies

9. AIX

calculate time

Hi, How do I calculate time? I need to create an alert if a process is running more than 30 minutes. I need to get the first time and then get another, calculate it if more than 30 mins and then alert it to pager. Can't find it in internet. Thanks in advance, itik (2 Replies)
Discussion started by: itik
2 Replies

10. HP-UX

how to calculate CPU time under HP-UX

Hi, I am loking for a c++ function that calculate CPU time under HP-UX Thank you (1 Reply)
Discussion started by: limame
1 Replies
Login or Register to Ask a Question