Find the answer to your Linux question:
Results 1 to 1 of 1
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2011
    Posts
    17

    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 kaspareov; 11-11-2011 at 11:22 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •