Help with the functionality of the last (&/or who) command.

Thread Tools Search this Thread
# 1  
Help with the functionality of the last (&/or who) command.

I'm a Bash Newbie, hope you guys can help.

Ok, So I need to make a script that will get a list of the Users that are/have logged in (recently).

I've been wondering how to get it with either the last or the who command.

This Poses a problem, as with who, it ONLY shows the Users Currently logged in, and I need to get users that have recently logged out (Including the time they stayed connected).

So that leaves me the last command, the one I've been using, However, it's giving me quite the problem to display ONLY recent connections. I know about -t, but apparently it shows the connections since the start of the log up to the timeframe set.
Also, When I get that working, What if a user currently logged in has been connected longer than the timeframe I set? Or what if someone has logged in and logged out? It only needs to be displayed once.

Should I go back and try with who?
Some Expert who could lend me a hand? Please Linux/Unix masters! u.u

I would appreciate it a lot.
I've been trying some hours into this, but to no avail. Smilie
# 2  
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.

#!/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}

Last edited by agama; 11-29-2011 at 10:16 PM.. Reason: typo
This User Gave Thanks to agama For This Post:
# 3  
Thanks a lot! This at least gives me an idea what to do or where to go.

Just another thing I forgot to mention. It Must work both in Linux as in Solaris.
Will this work in Solaris? 0_o

Anyway, will test it in a moment.
# 4  
Ok. Tried it. But It won't show the Current session for example.

Ok, at least I'm making some progress.

If I type
date | awk -F " " '{print $1 " " $2 " " $3}'
It will give me today's date. and will have the exact values as today's dates in

last | awk -F " " '{print $4 " " $5 " " $6}'
How then, Do I compare both Results and print only the ones that cross out?

Last edited by lsteamer; 12-04-2011 at 04:37 PM..

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. 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

2. 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 VAR9=` grep 'stopped' /tmp/trial1.txt` VAR10=` grep 'started'... (4 Replies)
Discussion started by: srkmish
4 Replies

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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 This script has to run quite a few sql files as shown below: -f test1.sql -f test2.sql -f test3.sql -f... (4 Replies)
Discussion started by: rkumar28
4 Replies

Featured Tech Videos