DevOps/Cloud By GK
  • DevOps/Cloud Introduction
  • 🚩AWS
    • Create AWS Account
    • Launch EC2 Instance
  • 🚩Linux
    • User Managment
      • Adding the User to Sudo group
      • visudo
    • Directory / File Management
      • Vi
      • Changing Permissions (chmod)
      • Filtering IO/File
    • Memory Management
    • Process Management
    • Network Management
    • Service Management
    • Environmental Variables
      • $PATH
    • Utilities
    • SHELL PROGRAMMING
      • Hello World
      • Variables
      • Command Line arguments
      • Conditional Statements
      • Array
      • Loops
      • Functions
      • AWK
      • sed
      • Example Useful Scripts
      • Basic -> Advanced
  • 🚩git
    • Introduction
    • Creating GitHub Account
    • Installation
      • Git Bash In Windows
      • Git In VS Code
      • Git In Eclipse
      • Git CLI in Linux
  • Creating Git Remote Repository
  • Git Commands L -> R
  • Git Commands L <- R
  • Git Branch Commands
  • Using SSH (Password Less)
  • 🚩maven
    • Introduction
    • Installation
    • POM.xml
    • Goals
  • 🚩SonarQube
    • Introduction
    • Sonar Cloud
    • Creating Project In SonarCloud
    • Installing Sonar-Scanner
    • Triggering Scan
  • 🚩Nexus
    • Introduction
    • Installation of Nexus
    • Creating Repository
    • Uploading Artifacts to Repository
  • 🚩Jenkins
    • Introduction
    • Installation of Jenkins
    • First Job In Jenkins
    • Git + Jenkins
      • WebHook
    • Maven + Jenkins
    • Sonar-Scanner + Jenkins
    • Nexus + Jenkins
Powered by GitBook
On this page
  1. Linux
  2. SHELL PROGRAMMING

Loops

Certainly! Here's an example of a shell script that demonstrates arrays in shell scripting:

#!/bin/bash

# Array declaration and initialization
fruits=("apple" "banana" "orange" "grape" "watermelon")

# Accessing array elements
echo "First fruit: ${fruits[0]}"
echo "Third fruit: ${fruits[2]}"

# Modifying array elements
fruits[1]="kiwi"

# Adding elements to an array
fruits+=("mango" "pineapple")

# Looping through an array
echo "All fruits:"
for fruit in "${fruits[@]}"; do
    echo "$fruit"
done

# Array length
echo "Total number of fruits: ${#fruits[@]}"

In this script, we have the following array-related operations:

  • Array Declaration and Initialization: We declare and initialize an array called fruits with multiple elements. Elements are enclosed in parentheses and separated by spaces.

  • Accessing Array Elements: We use ${fruits[index]} to access individual elements of the array. The index starts from 0. In the example, we access the first (${fruits[0]}) and third (${fruits[2]}) elements.

  • Modifying Array Elements: We can modify array elements by assigning new values to specific indexes. In this case, we change the second element to "kiwi" by assigning "kiwi" to ${fruits[1]}.

  • Adding Elements to an Array: We can add elements to an array by using the += operator followed by the new elements enclosed in parentheses. In this case, we add "mango" and "pineapple" to the fruits array.

  • Looping Through an Array: We use a for loop to iterate over all elements of the array. The "${fruits[@]}" syntax expands to all elements of the array. In the loop, we display each fruit.

  • Array Length: We use ${#fruits[@]} to get the length of the array, which represents the total number of elements.

When you run the script, it will display the first and third elements of the fruits array, modify the second element, add two new elements, loop through all the elements, and display the total number of fruits.

You can customize and expand this script by adding or removing elements from the array, manipulating array elements, and performing additional operations based on array values. Arrays in shell scripting provide a convenient way to store and manipulate multiple values within a single variable.

PreviousArrayNextFunctions

Last updated 2 years ago

🚩