Count System Users Within ID Range

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will be working with the /etc/passwd file to count the number of users with user IDs between 500 and 10000 on the system. We will be using the cut, tr, and df commands along with loops and arrays to fetch the user IDs and display the usernames within the specified range. The user can also change the range using command-line arguments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) linux(("`Linux`")) -.-> linux/UserandGroupManagementGroup(["`User and Group Management`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/FunctionsandScopeGroup(["`Functions and Scope`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) shell(("`Shell`")) -.-> shell/SystemInteractionandConfigurationGroup(["`System Interaction and Configuration`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/cut("`Text Cutting`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/UserandGroupManagementGroup -.-> linux/id("`User/Group ID Displaying`") linux/UserandGroupManagementGroup -.-> linux/passwd("`Password Changing`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/VariableHandlingGroup -.-> shell/str_manipulation("`String Manipulation`") shell/VariableHandlingGroup -.-> shell/arrays("`Arrays`") shell/VariableHandlingGroup -.-> shell/param_expansion("`Parameter Expansion`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/ControlFlowGroup -.-> shell/exit_status("`Exit and Return Status`") shell/FunctionsandScopeGroup -.-> shell/func_def("`Function Definition`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-18279{{"`Count System Users Within ID Range`"}} linux/cat -.-> lab-18279{{"`Count System Users Within ID Range`"}} linux/cut -.-> lab-18279{{"`Count System Users Within ID Range`"}} linux/exit -.-> lab-18279{{"`Count System Users Within ID Range`"}} linux/echo -.-> lab-18279{{"`Count System Users Within ID Range`"}} linux/pipeline -.-> lab-18279{{"`Count System Users Within ID Range`"}} linux/redirect -.-> lab-18279{{"`Count System Users Within ID Range`"}} linux/id -.-> lab-18279{{"`Count System Users Within ID Range`"}} linux/passwd -.-> lab-18279{{"`Count System Users Within ID Range`"}} linux/df -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/shebang -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/comments -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/quoting -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/variables_decl -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/variables_usage -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/str_manipulation -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/arrays -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/param_expansion -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/for_loops -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/cond_expr -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/exit_status -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/func_def -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/arith_expansion -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/cmd_substitution -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/subshells -.-> lab-18279{{"`Count System Users Within ID Range`"}} shell/globbing_expansion -.-> lab-18279{{"`Count System Users Within ID Range`"}} end

User ID Counting

In this challenge, you will write a Bash script to fetch user-ids from the /etc/passwd file, display only usernames within a specific range based on command-line arguments, and count the number of users within the specified range.

Tasks

You need to complete the following task:

  1. Fetch user-ids from the /etc/passwd file.
  2. Display only usernames within a specific range based on command-line arguments.
  3. Count the number of users within the specified range.

Requirements

You must meet the following requirements:

  • The script should be named userId.sh.
  • Working directory for the script should be ~/project.
  • Use commands df, cut, and tr.
  • Utilize loops and arrays in the Bash script.

Example

labex:project/ $ bash userId.sh

[OUTPUT] UID AND USERNAME [OUTPUT]
5000: labex
999: mongodb
[OUTPUT] Total count of User-id Between 500 to 10000 is "2" [OUTPUT]

Summary

After completing this challenge, you will have a better understanding of how to work with the /etc/passwd file and fetch user IDs using various commands. You will also learn how to use loops and arrays to manipulate the data and display the required output. Additionally, you will gain experience in working with command-line arguments and customizing the script to suit your needs.

Other Linux Tutorials you may like