Skip to Content
Linux Shell Scripting Cookbook - Third Edition
book

Linux Shell Scripting Cookbook - Third Edition

by Clif Flynt, Sarath Lakshman, Shantanu Tushar
May 2017
Beginner
552 pages
28h 47m
English
Packt Publishing
Content preview from Linux Shell Scripting Cookbook - Third Edition

How it works...

The active_users.sh script reads from /var/log/wtmp or a wtmp log file defined on the command line. The last -f command extracts the log file contents. The first column in the log file is the username. The cut command extracts the first column from the log file. The sort and uniq commands reduce this to a list of unique users.

The script's outer loop iterates through the users. For each user, grep is used to extract the log lines corresponding to a particular user.

The last column of each line is the duration of this login session. These values are summed in the inner while read t loop.

The session duration is formatted as (HOUR:SEC). This value is extracted with awk to report the last field and then piped to tr -d to remove ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Linux Shell Scripting - Second Edition

Mastering Linux Shell Scripting - Second Edition

Mokhtar Ebrahim, Andrew Mallett

Publisher Resources

ISBN: 9781785881985