create a shell script that displays the ids

Create Shell Script to Display IDs

By Robert Thompson
AD

Creating a shell script that displays the ids of users logged into a system provides immediate visibility into active sessions and resource usage for any Linux or Unix environment. This straightforward automation task helps system administrators monitor who is accessing the machine at any given moment without manually parsing command output.

By writing a simple shell script, you can consistently retrieve user identifiers, login names, and terminal information in a clean, reusable format that fits into broader monitoring workflows. Understanding how to build this foundation helps you expand into more complex administrative scripts and scheduling tasks using cron or other orchestration tools.

Understanding User ID Display in Shell Scripts

User IDs, often represented by the numeric identifier associated with each account, are essential for security, auditing, and process management. Displaying these values programmatically allows for integration with alerting systems, dashboards, or log analysis pipelines.

AD
Learn Interactive Shell Scripting in Linux
Learn Interactive Shell Scripting in Linux
Tutorial: Bare-Metal Shell for Kinetis
Tutorial: Bare-Metal Shell for Kinetis
Seashell template
Seashell template

The `who` and `w` commands provide quick views of logged in users, but embedding them into a script gives you control over formatting, filtering, and output destinations such as files, emails, or APIs. This flexibility is crucial for maintaining robust infrastructure visibility across multiple servers.

Using the `who` Command for Session Information

The `who` command reads from the `utmp` file and prints login names, terminal lines, login times, and associated user IDs in a concise format. It serves as a lightweight method for extracting active session data directly from the kernel-maintained record of current users.

the letter e made out of seashells and pearls
the letter e made out of seashells and pearls
seashell
seashell
an image of seashells in the ocean with their names and pictures on it
an image of seashells in the ocean with their names and pictures on it

When combined with text processing tools like `awk` or `cut`, you can isolate specific fields such as the user name or terminal, enabling your script to present a tailored view that matches operational requirements or compliance standards.

Leveraging the `w` Command for Detailed Activity

The `w` command builds upon `who` by also showing the current process and the amount of time the system has been running, giving a richer picture of user activity. This is particularly helpful when you need to correlate logged in users with ongoing computational workloads.

AD

By parsing the output of `w`, you can extract usernames and terminal information while potentially discarding the command summary section, ensuring your script returns only the relevant identity data for downstream processing or display.

a wooden sign with seashells attached to it
a wooden sign with seashells attached to it
Florida Shells ID Guide – Beginner Friendly
Florida Shells ID Guide – Beginner Friendly
a wooden sign with shells on it that says in the news of life, we find out true direction
a wooden sign with shells on it that says in the news of life, we find out true direction
an image of seashell morphs
an image of seashell morphs
13 Seashell Art DIY Ideas for Creative Beach-Inspired Crafts
13 Seashell Art DIY Ideas for Creative Beach-Inspired Crafts
the sea shell font and cursive writing is displayed on a white background with seashells
the sea shell font and cursive writing is displayed on a white background with seashells

Designing the Shell Script to Show IDs

A well-structured script begins with a shebang line, defines clear objectives, and uses functions to separate concerns such as data retrieval, formatting, and output. This modular approach makes debugging and future enhancements much more manageable as system requirements evolve.

Error handling should be considered from the start, including checks for command availability and appropriate permissions, so the script behaves predictably in automated environments where interactive input is not available.

AD

Implementing Basic ID Extraction Logic

You can start by using a pipeline that combines `who` with `awk` to print the first and third fields, representing the username and user ID, respectively. This simple pattern quickly delivers the essential mapping between account names and their numeric identities.

the florida gulf coast shell identification poster
the florida gulf coast shell identification poster
How to Identify a Seashell in 3 Easy Steps with ChatGPT | Beginner’s Guide
How to Identify a Seashell in 3 Easy Steps with ChatGPT | Beginner’s Guide
Buchstabe - Letter A
Buchstabe - Letter A
DIY Seashell Letters: Coastal Decor You Can Make Today
DIY Seashell Letters: Coastal Decor You Can Make Today
three seashells with writing on them next to a penny piece and a coin
three seashells with writing on them next to a penny piece and a coin
New Moon Full Moon, Weather Wind, Shell Crafts, New Moon, Moon Phases, Beach House Decor, Sea Shells, Shells, How To Plan
New Moon Full Moon, Weather Wind, Shell Crafts, New Moon, Moon Phases, Beach House Decor, Sea Shells, Shells, How To Plan
the letter e is made out of seashells
the letter e is made out of seashells
Wedding guest names on seashells
Wedding guest names on seashells
the letter c is made out of seashells and starfish
the letter c is made out of seashells and starfish
a letter made out of seashells and shells
a letter made out of seashells and shells
Parts of a Sea Shell
Parts of a Sea Shell
five seashells with hand written messages on them
five seashells with hand written messages on them
an iphone screen showing different types of seashells
an iphone screen showing different types of seashells

For example, `who | awk '{print $1, $3}'` provides a clean list that can be sorted, deduplicated, or filtered further depending on whether you want to highlight shared accounts or verify unique sessions.

Formatting Output for Readability and Automation

To support both human review and machine parsing, you might incorporate column alignment, headers, or JSON output using tools like `jq`. Clear formatting ensures that the information remains useful across different contexts, from ad hoc checks to scheduled reports.

AD

Adding timestamps to each run can also help correlate identity activity with system events, providing valuable context when investigating anomalies or tracking usage patterns over time.

Enhancing the Script with Additional Features

Once the basic display functionality is in place, you can extend the script to include options such as filtering by terminal, suppressing duplicate entries, or exporting data to a log file for historical analysis. These enhancements make the script more adaptable to varying operational needs.

Using command line arguments with `getopts` allows users to toggle between concise and verbose modes, or to specify custom output destinations, increasing the overall utility of the script without complicating the everyday usage.

Scheduling the Script with Cron

Integrating the script into a regular monitoring routine via cron jobs ensures consistent oversight of user identities and their corresponding activity. You can configure intervals that match your security policies, from every few minutes to several times per day.

Redirecting output to centralized logging platforms or email notifications enables proactive response when unexpected accounts appear, allowing administrative teams to maintain tighter control over access and resource usage.

Combining with System Metrics for Comprehensive Monitoring

By merging user identity data with CPU, memory, and network metrics, you gain a more complete picture of how individual accounts impact system performance. This correlation can reveal patterns such as resource-intensive sessions or unauthorized usage during off-hours.

Tools like `sar`, `vmstat`, or custom scripts can be chained together to produce composite reports, where the shell script displaying the ids acts as a key component that ties user sessions to their respective resource footprints.

Building a shell script that displays the ids of current users is a practical exercise in automation, observability, and system administration best practices. With thoughtful design and incremental enhancements, this small utility can grow into a cornerstone of your monitoring infrastructure, providing clear, actionable insights into account activity and system health on a daily basis.

AD