Long and Short

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will write a shell script to display the longest and shortest user-names on the system. We will fetch user-names from the /etc/passwd file and use string operations to find the longest and shortest names. This challenge will help us learn about the etc configuration files and how to work with arrays and string operations in shell scripts.


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`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) 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/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/pipeline("`Data Piping`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/UserandGroupManagementGroup -.-> linux/passwd("`Password Changing`") 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/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-18307{{"`Long and Short`"}} linux/cat -.-> lab-18307{{"`Long and Short`"}} linux/cut -.-> lab-18307{{"`Long and Short`"}} linux/echo -.-> lab-18307{{"`Long and Short`"}} linux/pipeline -.-> lab-18307{{"`Long and Short`"}} linux/redirect -.-> lab-18307{{"`Long and Short`"}} linux/passwd -.-> lab-18307{{"`Long and Short`"}} shell/shebang -.-> lab-18307{{"`Long and Short`"}} shell/comments -.-> lab-18307{{"`Long and Short`"}} shell/quoting -.-> lab-18307{{"`Long and Short`"}} shell/variables_decl -.-> lab-18307{{"`Long and Short`"}} shell/variables_usage -.-> lab-18307{{"`Long and Short`"}} shell/str_manipulation -.-> lab-18307{{"`Long and Short`"}} shell/arrays -.-> lab-18307{{"`Long and Short`"}} shell/param_expansion -.-> lab-18307{{"`Long and Short`"}} shell/for_loops -.-> lab-18307{{"`Long and Short`"}} shell/cond_expr -.-> lab-18307{{"`Long and Short`"}} shell/arith_expansion -.-> lab-18307{{"`Long and Short`"}} shell/cmd_substitution -.-> lab-18307{{"`Long and Short`"}} shell/subshells -.-> lab-18307{{"`Long and Short`"}} shell/globbing_expansion -.-> lab-18307{{"`Long and Short`"}} end

Longest and Shortest User-Names

In this challenge, you will be tasked with finding the longest and shortest user-names on the system using a Bash script.

Tasks

You need to complete the following task:

  • Fetch user-names from the first field in the /etc/passwd file.
  • Print the longest and shortest user names.

Requirements

You must meet the following requirements:

  • The script should be named longAndShort.sh.
  • Work directory should be ~/project.

Example

labex:project/ $ bash longAndShort.sh

[OUTPUT] Largest and Shortest UserName [OUTPUT]
Largest Username is systemd-timesync
Smallest Username is lp

Summary

After completing this challenge, we have learned how to fetch user-names from the /etc/passwd file and use string operations to find the longest and shortest names. We have also learned about the etc configuration files and how to work with arrays and string operations in shell scripts. This challenge has helped us improve our skills in shell scripting and has given us a better understanding of the Linux system.

Other Linux Tutorials you may like