Results 1 to 6 of 6

Thread: script to keep trackk of my last login

  1. #1
    Junior Member
    Join Date
    May 2006
    Location
    Dubai
    Posts
    2

    script to keep trackk of my last login

    Hi,

    I would like to edit my .bashrc to help me keep track of when I last logged on. I have my image set on my hard drive so my home directory is permanant. Now, I would like whenever I log it for the script to tell me when was the lat time I logged in, how many times I have loged in so far and record my new login time.


    I was thinking of using a file to record the login date and a login counter but I don't know how to overwrite a file at every login with the new informatiion. Nor do I know how to read a file at log in to capture this information... Any ideas?


    Thanks,

  2. #2
    Senior Member registered user
    Join Date
    Apr 2005
    Location
    italy
    Posts
    245
    I guess you can put in your .bashrc something like:
    lastlog | grep 'myusername' >> logfile.txt

    not sure, not tested

  3. #3
    Junior Member
    Join Date
    May 2006
    Location
    Dubai
    Posts
    2
    Here is what I have come up so far:

    #set up the variable
    declare -i logCounter=0
    declare lastDate

    #read the file


    #Display the information for the user
    echo -n "Your last login was on: "
    echo $lastDate
    echo
    echo "You have login $logCounter time(s) so far."
    echo

    #Record the new information for next time
    date | cat > .loginInfo
    echo $logCounter | cat >> .loginInfo



    As you can see all that I am missing is how to read the file properly. The 1st line is the date and the 2nd is the login counter. How can I read each line of the file into the variables?

    ALSO, I am not sure if my way of recording the information is the most elegant and efficiant... So I would welcome any suggestions on this part as well

  4. #4
    Member registered user
    Join Date
    Jun 2004
    Posts
    83
    Typing "last ejodoin" is not sufficient? Either way, it may give you a tip for a better approach.

  5. #5
    Senior Member registered user
    Join Date
    Nov 2002
    Location
    USA, IL
    Posts
    1,041

    Re: script to keep trackk of my last login

    Example:

    ' last dave |head '
    Code:
    dave     pts/7        :0.0             Thu May 11 11:24   still logged in   
    dave     pts/0        :0               Thu May 11 11:24   still logged in   
    dave     pts/7        :0.0             Thu May 11 11:24 - 11:24  (00:00)    
    dave     pts/2        :0.0             Thu May 11 10:48 - 11:24  (00:35)    
    dave     pts/0        :0               Thu May 11 10:48 - 11:24  (00:35)    
    dave     tty2                          Thu May 11 10:48   still logged in   
    dave     pts/9        :0.0             Wed May 10 23:43 - 01:51  (02:08)    
    dave     pts/8        :0.0             Wed May 10 22:00 - 00:53  (02:53)    
    dave     pts/8        :0.0             Wed May 10 21:08 - 21:59  (00:51)    
    dave     pts/2        :0.0             Wed May 10 21:07 - 01:51  (04:44)
    Quote Originally Posted by ejodoin
    Hi,

    I would like to edit my .bashrc to help me keep track of when I last logged on. I have my image set on my hard drive so my home directory is permanant. Now, I would like whenever I log it for the script to tell me when was the lat time I logged in, how many times I have loged in so far and record my new login time.


    I was thinking of using a file to record the login date and a login counter but I don't know how to overwrite a file at every login with the new informatiion. Nor do I know how to read a file at log in to capture this information... Any ideas?


    Thanks,

  6. #6
    Senior Member registered user
    Join Date
    Sep 2005
    Posts
    205
    Quote Originally Posted by ejodoin
    Here is what I have come up so far:

    #set up the variable
    declare -i logCounter=0
    declare lastDate

    #read the file


    #Display the information for the user
    echo -n "Your last login was on: "
    echo $lastDate
    echo
    echo "You have login $logCounter time(s) so far."
    echo

    #Record the new information for next time
    date | cat > .loginInfo
    echo $logCounter | cat >> .loginInfo



    As you can see all that I am missing is how to read the file properly. The 1st line is the date and the 2nd is the login counter. How can I read each line of the file into the variables?

    ALSO, I am not sure if my way of recording the information is the most elegant and efficiant... So I would welcome any suggestions on this part as well
    I suggest you save the information like this: (ps. you don't need 'cat' here)
    Code:
    #Record the new information for next time
    echo "lastDate=\"$(date)\"" > .loginInfo
    echo "logCounter=\"$logCounter\"" >> .loginInfo
    .loginInfo should now contain
    Code:
    lastDate="...the date..."
    logCounter="...the number..."
    which is valid bash-code. You can then include this bash-code to your script with
    Code:
    #read the file
    source .loginInfo
    which sets $lastDate and $logCounter to proper values.

    You also need to increment logCounter at some point e.g. with
    Code:
    ((logCounter = logCounter + 1))

Similar Threads

  1. Can't login at XServer Login Window
    By torbu in forum General Support
    Replies: 2
    Last Post: 05-16-2004, 11:33 AM
  2. Can't login to Debian login screen
    By turbines in forum Hdd Install / Debian / Apt
    Replies: 3
    Last Post: 04-17-2004, 04:40 PM
  3. A Live CD script for Auto Internet Login? (DSL)
    By bonecrusher in forum General Support
    Replies: 1
    Last Post: 03-05-2004, 11:17 AM
  4. Help with a script
    By lindros in forum General Support
    Replies: 10
    Last Post: 07-24-2003, 08:26 PM
  5. script
    By punk000 in forum General Support
    Replies: 3
    Last Post: 02-26-2003, 07:02 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
  •  


USB Flash Drive Memory Stick Pendrive Thumb Drive 4GB, 8GB, 32GB, 64GB 128GB LOT picture

USB Flash Drive Memory Stick Pendrive Thumb Drive 4GB, 8GB, 32GB, 64GB 128GB LOT

$230.40



Type C USB 3.0 Flash Drive Thumb Drive Memory Stick for PC Laptop 1TB 2TB lot picture

Type C USB 3.0 Flash Drive Thumb Drive Memory Stick for PC Laptop 1TB 2TB lot

$73.29



1TB/2TB USB 3.0 Flash Drive Thumb U Disk Memory Stick Pen PC Laptop Storage lot picture

1TB/2TB USB 3.0 Flash Drive Thumb U Disk Memory Stick Pen PC Laptop Storage lot

$80.39



USB Flash Drive Thumb Drive Memory Stick Pendrive 4GB, 8GB, 32GB, 64GB 128GB LOT picture

USB Flash Drive Thumb Drive Memory Stick Pendrive 4GB, 8GB, 32GB, 64GB 128GB LOT

$308.13



Sandisk 16GB 32GB 64GB 128GB Cruzer Blade Flash Drive Memory Stick USB Lot Pack picture

Sandisk 16GB 32GB 64GB 128GB Cruzer Blade Flash Drive Memory Stick USB Lot Pack

$4.90



SanDisk Cruzer Blade 32GB USB 2.0 Flash Drive Thumb Memory Stick Pen SCDZ50 32G picture

SanDisk Cruzer Blade 32GB USB 2.0 Flash Drive Thumb Memory Stick Pen SCDZ50 32G

$5.75



64GB USB 3.0 Flash Drive USB Memory Stick High Speed Retractable USB Thumb Drive picture

64GB USB 3.0 Flash Drive USB Memory Stick High Speed Retractable USB Thumb Drive

$6.99



ZIPPY USB Flash Drive Memory Stick Pendrive Thumb Drive 4GB, 8GB, 32GB, 64GB LOT picture

ZIPPY USB Flash Drive Memory Stick Pendrive Thumb Drive 4GB, 8GB, 32GB, 64GB LOT

$2.80



2 PC MS Office 2021 USB DRIVE FULL VERSION EDITION  picture

2 PC MS Office 2021 USB DRIVE FULL VERSION EDITION

$89.99



1/ 10/ 100pcs USB 2.0 2GB, 4GB ,8GB ,16GB ,32GB ,64GB 128GB USB Flash Drives Lot picture

1/ 10/ 100pcs USB 2.0 2GB, 4GB ,8GB ,16GB ,32GB ,64GB 128GB USB Flash Drives Lot

$325.50