Comparing Arrays in Shell Scripting

ShellShellBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, you will learn how to compare arrays in Shell scripting. You will be given three lists of arrays and your task will be to find the common elements among all three arrays.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) 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/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/cd("`Directory Changing`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/UserandGroupManagementGroup -.-> linux/set("`Shell Setting`") 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/subshells("`Subshells and Command Groups`") shell/SystemInteractionandConfigurationGroup -.-> shell/shell_options("`Shell Options and Attributes`") shell/SystemInteractionandConfigurationGroup -.-> shell/globbing_expansion("`Globbing and Pathname Expansion`") subgraph Lab Skills shell/if_else -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} linux/echo -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} linux/cd -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} linux/chmod -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} linux/set -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/comments -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/quoting -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/variables_decl -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/variables_usage -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/str_manipulation -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/arrays -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/param_expansion -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/for_loops -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/cond_expr -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/subshells -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/shell_options -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} shell/globbing_expansion -.-> lab-153901{{"`Comparing Arrays in Shell Scripting`"}} end

Initialize the arrays

Create a new file called ~/project/array-comparison.sh.

Start by initializing three arrays: a, b, and c.

a=(3 5 8 10 6)
b=(6 5 4 12)
c=(14 7 5 7)

Compare arrays a and b

Next, compare the elements of arrays a and b to find the common elements. To do this, you can use nested loops.

## initialize an empty array to store the common elements
z=()

## loop through all elements of array a
for x in "${a[@]}"; do
  ## set a flag to false
  in=false

  ## loop through all elements of array b
  for y in "${b[@]}"; do
    if [ $x = $y ]; then
      ## if a match is found, add the element to array z
      z+=($x)
    fi
  done
done

Compare array c with array z

Next, compare the elements of array c with the elements of array z to find the common elements. Again, you can use nested loops for this step.

## initialize an empty array to store the common elements
j=()

## loop through all elements of array c
for i in "${c[@]}"; do
  ## set a flag to false
  in=false

  ## loop through all elements of array z
  for k in "${z[@]}"; do
    if [ $i = $k ]; then
      ## if a match is found, add the element to array j
      j+=($i)
    fi
  done
done

Print the common elements

Finally, print the common elements found in array j.

echo ${j[@]}

Execute the script.

cd ~/project
chmod +x array-comparison.sh
./array-comparison.sh
Common elements: 5

Summary

In this lab, you learned how to compare arrays in Shell scripting. By using nested loops, you were able to find the common elements among three different arrays.

Other Shell Tutorials you may like