I guess you can put in your .bashrc something like:
lastlog | grep 'myusername' >> logfile.txt
not sure, not tested![]()
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,
I guess you can put in your .bashrc something like:
lastlog | grep 'myusername' >> logfile.txt
not sure, not tested![]()
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
Typing "last ejodoin" is not sufficient? Either way, it may give you a tip for a better approach.
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)Originally Posted by ejodoin
I suggest you save the information like this: (ps. you don't need 'cat' here)Originally Posted by ejodoin
.loginInfo should now containCode:#Record the new information for next time echo "lastDate=\"$(date)\"" > .loginInfo echo "logCounter=\"$logCounter\"" >> .loginInfo
which is valid bash-code. You can then include this bash-code to your script withCode:lastDate="...the date..." logCounter="...the number..."
which sets $lastDate and $logCounter to proper values.Code:#read the file source .loginInfo
You also need to increment logCounter at some point e.g. with
Code:((logCounter = logCounter + 1))

Dell PowerEdge FX2s + 8x FC430 Blades 16x E5-2630v4 160 Cores Rails No PSU/RAM
$699.00
NETAPP HCI NAF-1701 SuperMicro Server 4X BLADE H410C 8X GOLD 5120 - 4X S25G NICs
$1999.97
Dell PowerEdge M630 Blade Server 2x Xeon E5-2680 v4, 2F3MP , 4GDP5 ,R10KJ ,JVFVR
$96.60
Dell Poweredge VRTX 25Bay 2.5" 2x PERC 8, 2xCMC, I/O 1GB Pass Through, 4x PS
$578.08
Dell R740XD VxRail P570 24x 2.5” 2U CTO Server– 2x Gold 6240, H740p, 2x 1600W
$699.00
Dell PowerEdge FX2 FX2s Server 10GbE CMC Blade Enclosure Chassis
$274.99
Dell PowerEdge VRTX Rackmount Chassis w/4x PSUs 2x CMC Modules
$399.98
Dell PowerEdge M620 Blade Server 2x Xeon E5-2680v2 2.8ghz 20-Cores | 128GB
$149.99
Dell PowerEdge FC430 Blade Server 2x Xeon E5-2630v4 20 Cores FX2 FX2s No RAM/HDD
$119.99
AS IS READ DESCRIP. Supermicro SuperServer 6026TT-HIBQRF 12-Bay 4x X8DTT-HIBQF
$215.00