Right Angle Triangle Pattern

LinuxLinuxIntermediate
Practice Now

Introduction

In this challenge, we will learn how to create a pattern using loops in a shell script. We will take input from the user and generate a pattern of numbers in the format mentioned in the problem statement. This will help us understand the working of loops in shell scripts and how to execute a bash script.


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`"]) linux(("`Linux`")) -.-> linux/TextProcessingGroup(["`Text Processing`"]) 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/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/InputandOutputRedirectionGroup -.-> linux/redirect("`I/O Redirecting`") linux/BasicSystemCommandsGroup -.-> linux/read("`Input Reading`") linux/TextProcessingGroup -.-> linux/col("`Line Feed Filtering`") 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/ControlFlowGroup -.-> shell/for_loops("`For 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/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} linux/echo -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} linux/redirect -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} linux/read -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} linux/col -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/shebang -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/comments -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/quoting -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/variables_decl -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/variables_usage -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/for_loops -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/cond_expr -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/arith_expansion -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/read_input -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/subshells -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} shell/globbing_expansion -.-> lab-18289{{"`Right Angle Triangle Pattern`"}} end

Shell Script Pattern Challenge

In this challenge, you are tasked with creating a shell script that generates a pattern based on user input.

Tasks

You need to complete the following task:

  1. Read a value from the user.
  2. Generate a pattern according to the given format.

Requirements

You must meet the following requirements:

  • Script name: pattern1.sh.
  • Work directory: ~/project.

Example

labex:project/ $ bash pattern1.sh
[INPUT]Enter the value of N[INPUT]
5
[OUTPUT]Expected Pattern is Shown Below[OUTPUT]
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Summary

After completing this challenge, we will have a better understanding of the following concepts:

  • How to read input from the user in a shell script.
  • How to create a pattern using loops in a shell script.
  • How to execute a bash script.
  • How to change execute permission of a file.

This challenge will also help us improve our problem-solving skills and logical thinking.

Other Linux Tutorials you may like