Skip to main content

Command Palette

Search for a command to run...

DevOps Day-10 Basic Linux Shell Scripting for DevOps Engineers

The challenge is for the DevOps Community to get stronger in DevOps

Published
2 min read
DevOps Day-10 Basic Linux Shell Scripting for DevOps Engineers
A

I am a highly skilled QA and testing manager with over 19 years of experience in the industry. I am passionate about ensuring the delivery of high-quality software and have a proven track record of successful project delivery. Additionally, I have extensive experience as a DevOps engineer, which has given me a deep understanding of the software development lifecycle and the importance of collaboration between teams. I am committed to staying up-to-date with the latest technologies and methodologies in the industry and am always seeking new challenges to enhance my skills.

Welcome to Day 10 of our Basic Linux Shell Scripting for DevOps Engineers! In today's session, we will cover the following topics:

  1. Functions: Functions are used to group a set of commands into a single unit that can be called multiple times in a script. The most commonly used syntax for defining a function is "function_name() { command1; command2; ... }". For example, the following script defines a function named "greeting" that prints a greeting message:
javascriptCopy codegreeting() {
    echo "Hello, world!"
}

To call a function, we simply use its name followed by parentheses. For example, the following command will call the "greeting" function:

Copy codegreeting
  1. Arrays: Arrays are used to store multiple values in a single variable. The most commonly used syntax for declaring an array is "array_name=(value1 value2 ...)". For example, the following command will declare an array named "fruits" with the values "apple", "banana", and "orange":
makefileCopy codefruits=(apple banana orange)

To access the values of an array, we use the "${array_name[index]}" syntax. For example, the following command will print the second element of the "fruits" array:

bashCopy codeecho ${fruits[1]}
  1. Command line arguments: Command line arguments are used to pass arguments to a script when it is executed. The most commonly used syntax for accessing command line arguments is "$1", "$2", etc., where "$1" refers to the first argument, "$2" refers to the second argument, and so on. For example, the following script takes two arguments and prints a message using them:
bashCopy code#!/bin/bash
echo "Hello, $1 and $2!"

And the following command will execute the script with the arguments "John" and "Mary":

bashCopy code./script.sh John Mary

These are some additional concepts used in shell scripting. By mastering these concepts, you will be able to create more complex and sophisticated scripts. Keep practicing and experimenting with shell scripting, and you'll become a proficient DevOps engineer in no time!