Command-Line Argument Comparison Script

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will create a bash script to compare larger integer values from a n number of arguments using command line arguments. We will learn how to pass command-line arguments, how to compare integer values in a script, and how to print the largest value among the arguments.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/InputandOutputRedirectionGroup(["`Input and Output Redirection`"]) 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`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) shell/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") linux/BasicSystemCommandsGroup -.-> linux/declare("`Variable Declaring`") linux/BasicSystemCommandsGroup -.-> linux/exit("`Shell Exiting`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/clear("`Screen Clearing`") 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/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("`Wildcard Character`") subgraph Lab Skills shell/if_else -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} linux/declare -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} linux/exit -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} linux/echo -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} linux/redirect -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} linux/clear -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/shebang -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/comments -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/quoting -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/variables_decl -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/variables_usage -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/str_manipulation -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/arrays -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/param_expansion -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/for_loops -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/cond_expr -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/exit_status -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/arith_expansion -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/subshells -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} shell/globbing_expansion -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} linux/wildcard -.-> lab-18317{{"`Command-Line Argument Comparison Script`"}} end

Compare Integers

In this challenge, you are tasked with writing a script to compare larger integer values from a variable number of arguments using command-line inputs.

Tasks

You need to complete the following task:

  1. Write a script to compare larger integer values from a variable number of arguments using command-line inputs.
  2. Print the largest value from the arguments.
  3. Print an error message if there are no arguments passed.

Requirements

You must meet the following requirements:

  • The script should be named appropriately.
  • Use command-line inputs to pass a variable number of arguments.
  • Compare all the arguments and print the largest value.
  • Display an error message if there are no arguments provided.

Example

labex:project/ $ bash compareIntegers.sh 1 4 2 3
Largest number in the integer values is: 4

Summary

After completing this challenge, we have learned how to pass command-line arguments in a bash script, how to compare integer values in a script, and how to print the largest value among the arguments. We have also learned how to handle errors when there are no arguments passed to the script. This challenge has helped us to improve our scripting skills and understand the working of command-line arguments in bash scripts.

Other Linux Tutorials you may like