Reverse Number Using Shell Script

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, you will learn how to use command-line arguments, arithmetic operations, and loops in shell script to reverse a given number. You will extract each digit of the number and convert it to reverse. This challenge will help you to understand the working of command-line arguments and arithmetic operations in shell script.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) 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/echo("`Text Display`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") 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/arrays("`Arrays`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/while_loops("`While Loops`") shell/ControlFlowGroup -.-> shell/cond_expr("`Conditional Expressions`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_expansion("`Arithmetic Expansion`") shell/AdvancedScriptingConceptsGroup -.-> shell/read_input("`Reading Input`") shell/AdvancedScriptingConceptsGroup -.-> shell/cmd_substitution("`Command Substitution`") 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-18291{{"`Reverse Number Using Shell Script`"}} linux/declare -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} linux/echo -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} linux/read -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/shebang -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/comments -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/quoting -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/variables_decl -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/variables_usage -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/arrays -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/for_loops -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/while_loops -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/cond_expr -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/arith_expansion -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/read_input -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/cmd_substitution -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/subshells -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} shell/globbing_expansion -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} linux/wildcard -.-> lab-18291{{"`Reverse Number Using Shell Script`"}} end

Reverse Numbers

In this challenge, you are tasked with writing a script to reverse a multi-digit number entered by the user.

Tasks

You need to complete the following task:

  • Write a script to read a multi-digit number from the user and reverse the number.

Requirements

You must meet the following requirements:

  • The script should be named reverse.sh.
  • Utilize command-line arguments in the script.
  • Use modulo operators in the script for arithmetic operations.
  • Utilize loops in the script.

Example

labex:project/ $ bash reverse.sh
**********Enter the value that is need to be reversed**********
12472
[OUTPUT]Reversed value of 12472 is : 27421[OUTPUT]

Summary

After completing this challenge, you will have a good understanding of how to use command-line arguments, arithmetic operations, and loops in shell script. You will be able to reverse a given number by extracting each digit and converting it to reverse. This challenge will help you to improve your shell scripting skills and prepare you for more complex challenges in the future.

Other Linux Tutorials you may like