Search code examples
shellunixshkshcsh

how to get the last login time for all users in one line for different shells


I can make the following line work on ksh

for user in $( awk -F: '{ print $1}' /etc/passwd); do last $user | head -1 ; done | tr -s "\n" |sort

But I'd like to make it work on UNIX sh and UNIX csh. (in linux sh it runs fine, but linux is not unix...)

I know there are limitations for this since it seems that each UNIX(*) has its own variations on the syntax.

update: sorry, there are some restrictions here:

  1. I can't write on the disk, so I can't save scripts.
  2. how do i write this in CSH?

Solution

  • This awk-script seems to be the equivalent to you loop above:

    {
      cmd = "last "$1
      cmd | getline result
      printf "%s",  result
    }
    

    use it like this:

    awk -F: -f script_above.awk /etc/passwd
    

    Pipe the output to sort

    As a one-liner:

    $ awk -F: '{cmd = "last "$1; cmd | getline result;printf "%s", result}' /etc/passwd