Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Help with the functionality of the last (&/or who) command. Post 302577747 by agama on Tuesday 29th of November 2011 09:15:06 PM
Old 11-29-2011
This is a hack, and probably not the way I'd do it for anything other than a quick script, but it uses last to print the last 'n days' worth of users who were logged in. Prevents dups if a user logs in more than once per day and will not work with a BSD version of last.

Code:
#!/usr/bin/env ksh
# $1 from command line is number of days to go back; assuming someone 
# logged in each day (doesn't handle days where nobody logged in)

last -F -R | awk '
    BEGIN { soup = "JanFebMarAprMayJunJulAugSepOctNovDec"; }

    NF < 6 || /reboot/ || /begins/ || /still logged/ {next;}   # skip rubbish

    {
        m = int(index( soup, $4 ) / 3) + 1;     #convert month name to int
        d = sprintf( "%4d%02d%02d", $7, m, $5 );   # build a sortable date
        if( ! seen[d,$1]++ )
            who[d] = who[d] $1 " ";            # create user list by date
    }

    END {
        for( w in who )                 # for each user list print
            print w, who[w];
    }
' | sort -k 1n,1 | tail -${1:-15}
exit


Last edited by agama; 11-29-2011 at 10:16 PM.. Reason: typo
This User Gave Thanks to agama For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Restartibility Functionality....

Hello, I am trying to write a script that has a option of restarting the script from where it failed. I have to write a script called Batch.sh. This script has to run quite a few sql files as shown below: logcmd.sh -f test1.sql logcmd.sh -f test2.sql logcmd.sh -f test3.sql logcmd.sh -f... (4 Replies)
Discussion started by: rkumar28
4 Replies

2. SuSE

inconsistent ls command display at the command prompt & running as a cron job

Sir, I using the following commands in a file (part of a bigger script): #!/bin/bash cd /opt/oracle/bin ls -lt | tail -1 | awk '{print $6}' >> /tmp/ramb.out If I run this from the command prompt the result is: 2007-05-16 if I run it as a cron job then... (5 Replies)
Discussion started by: rajranibl
5 Replies

3. Shell Programming and Scripting

cp && rm command, rm: <file> not removed. No such file or directory

Hi, I am running this is korn shell cp $source/$fname $dest/dir && rm $source/$fname This was returned: rm: /dir/file not removed: No such file or directory The file could be found in the $dest directory which meant the cp was success. The above code is used in a for loop to move... (5 Replies)
Discussion started by: Leion
5 Replies

4. UNIX for Dummies Questions & Answers

sleep 10 && command doesn't work

Hi there, I found a trick to easily postpone a command by a few seconds: supernova:~# sleep 10 && command &If you logout, the command should still be executed... But not all the time. Could anyone of you explain me why the following command is executed even after logging out: supernova:~# sleep... (2 Replies)
Discussion started by: chebarbudo
2 Replies

5. UNIX for Dummies Questions & Answers

Problem with xterm & tcsh & sourcing a script in a single command

Hi friends, I have a script that sets the env variable path based on different conditions. Now the new path variable setting should not done in the same terminal or same shell. Only a new terminal or new shell should have the new path env variable set. I am able to do this only as follows: >cd... (1 Reply)
Discussion started by: sowmya005
1 Replies

6. UNIX for Dummies Questions & Answers

Command Functionality

Hi everyone, today i need that someone help to understand this particular line of command. So you can explain to me step by step, it will be great. ---------- Post updated at 11:53 AM ---------- Previous update was at 11:51 AM ---------- (9 Replies)
Discussion started by: Newer
9 Replies

7. Shell Programming and Scripting

[Solved] BASH - chaining TEST and COMMAND with && and II

Can you explain what this line of script is doing. What I have understood is : -- variable C is the name of a software which is either not installed, so it must be installed or allready installed and then should be update if newer version found -- branch B="$B $C" is to install the software --... (4 Replies)
Discussion started by: jcdole
4 Replies

8. Shell Programming and Scripting

Read input while another command is running &&

This script runs tshark, then displays the output. I want to read a keypress at all times throughout this script, especially during the 10 seconds that tshark is running. This code however, only takes input before tshark runs. #!/bin/bash main() { while (( "1" == "1" )) ; do keypress &&... (3 Replies)
Discussion started by: Kiah07
3 Replies

9. Shell Programming and Scripting

If && command giving wrong output

Hi All, I am trying to run a script which will search for 2 strings(stopped,started) in a text file and echo an output depending on below condition -bash-3.2$ cat trial1.txt v ggg f -bash-3.2$ cat trial1.sh VAR9=` grep 'stopped' /tmp/trial1.txt` VAR10=` grep 'started'... (4 Replies)
Discussion started by: srkmish
4 Replies

10. UNIX for Beginners Questions & Answers

Grep command functionality - discussed

what is the following bash command does grep `date +%Y-%m-%d --date='1 day ago'` /path/to/file/FILE_PREFIX_\`date +%Y%m%d --date='1 day ago'`.dsv | grep -v 'ERROR' | cut -d "|" -f 2 | sed 's/^0/27/' (1 Reply)
Discussion started by: tera
1 Replies
AC(1)							      General Commands Manual							     AC(1)

NAME
ac - print statistics about users' connect time SYNOPSIS
ac [ -d | --daily-totals ] [ -y | --print-year ] [ -p | --individual-totals ] [ people ] [ -f | --file filename ] [ -a | --all-days ] [ --complain ] [ --reboots ] [ --supplants ] [ --timewarps ] [ --compatibility ] [ --tw-leniency num ] [ --tw-suspicious num ] [ -z | --print-zeros ] [ --debug ] [ -V | --version ] [ -h | --help ] DESCRIPTION
ac prints out a report of connect time (in hours) based on the logins/logouts in the current wtmp file. A total is also printed out. The accounting file wtmp is maintained by init(8) and login(1). Neither ac nor login creates the wtmp if it doesn't exist, no accounting is done. To begin accounting, create the file with a length of zero. NOTE: The wtmp file can get really big, really fast. You might want to trim it every once and a while. GNU ac works nearly the same u*x ac, though it's a little smarter in several ways. You should therefore expect differences in the output of GNU ac and the output of ac's on other systems. Use the command info accounting to get additional information. OPTIONS
-d, --daily-totals Print totals for each day rather than just one big total at the end. The output looks like this: Jul 3 total 1.17 Jul 4 total 2.10 Jul 5 total 8.23 Jul 6 total 2.10 Jul 7 total 0.30 -p, --individual-totals Print time totals for each user in addition to the usual everything-lumped-into-one value. It looks like: bob 8.06 goff 0.60 maley 7.37 root 0.12 total 16.15 people Print out the sum total of the connect time used by all of the users included in people. Note that people is a space separated list of valid user names; wildcards are not allowed. -f, --file filename Read from the file filename instead of the system's wtmp file. --complain When the wtmp file has a problem (a time-warp, missing record, or whatever), print out an appropriate error. --reboots Reboot records are NOT written at the time of a reboot, but when the system restarts; therefore, it is impossible to know exactly when the reboot occurred. Users may have been logged into the system at the time of the reboot, and many ac's automatically count the time between the login and the reboot record against the user (even though all of that time shouldn't be, perhaps, if the system is down for a long time, for instance). If you want to count this time, include the flag. *For vanilla ac compatibility, include this flag.* --supplants Sometimes, a logout record is not written for a specific terminal, so the time that the last user accrued cannot be calculated. If you want to include the time from the user's login to the next login on the terminal (though probably incorrect), include this you want to include the time from the user's login to the next login on the terminal (though probably incorrect), include this flag. *For vanilla ac compatibility, include this flag.* --timewarps Sometimes, entries in a wtmp file will suddenly jump back into the past without a clock change record occurring. It is impossible to know how long a user was logged in when this occurs. If you want to count the time between the login and the time warp against the user, include this flag. *For vanilla ac compatibility, include this flag.* --compatibility This is shorthand for typing out the three above options. -a, --all-days If we're printing daily totals, print a record for every day instead of skipping intervening days where there is no login activity. Without this flag, time accrued during those intervening days gets listed under the next day where there is login activity. --tw-leniency num Set the time warp leniency to num seconds. Records in wtmp files might be slightly out of order (most notably when two logins occur within a one-second period - the second one gets written first). By default, this value is set to 60. If the program notices this problem, time is not assigned to users unless the --timewarps flag is used. --tw-suspicious num Set the time warp suspicious value to num seconds. If two records in the wtmp file are farther than this number of seconds apart, there is a problem with the wtmp file (or your machine hasn't been used in a year). If the program notices this problem, time is not assigned to users unless the --timewarps flag is used. -y, --print-year Print year when displaying dates. -z, --print-zeros If a total for any category (save the grand total) is zero, print it. The default is to suppress printing. --debug Print verbose internal information. -V, --version Print the version number of ac to standard output and quit. -h, --help Prints the usage string and default locations of system files to standard output and exits. FILES
wtmp The system wide login record file. See wtmp(5) for further details. AUTHOR
The GNU accounting utilities were written by Noel Cragg <noel@gnu.ai.mit.edu>. The man page was adapted from the accounting texinfo page by Susan Kleinmann <sgk@sgk.tiac.net>. SEE ALSO
login(1), wtmp(5), init(8), sa(8) 1995 October 31 AC(1)
All times are GMT -4. The time now is 02:39 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy