90DaysOfDevOps Challenge - Day 4 -Basic Linux Shell Scripting for DevOps Engineers By Prashant Chaturvedi

90DaysOfDevOps Challenge - Day 4 -Basic Linux Shell Scripting for DevOps Engineers By Prashant Chaturvedi

Welcome to the #90DaysOfDevOps challenge day 4. We will discuss the fundamentals of Linux shell scripting and its applicability to DevOps engineers in today's session. Shell scripting is an effective tool for managing system configurations, automating processes, and streamlining DevOps operations. Now let's get started and learn the basics of shell scripting.

What is Shell Scripting for DevOps?

Shell scripting, to put it simply, is authoring a script file with a number of commands that the shell interpreter will run. By interpreting and carrying out the commands in the script, the shell serves as a conduit between the user and the operating system.

Shell scripting is essential for DevOps engineers because it helps automate repetitive activities, configure systems, manage infrastructure, and coordinate different tools and processes. DevOps experts may use it to automate deployments, build unique processes, keep an eye on the health of their systems, and much more.

Shell scripting enables you to:

  • Automate tasks: By writing scripts, you can automate repetitive tasks, saving time and reducing the risk of errors.
  • Customize workflows: Shell scripts provide the flexibility to create custom workflows tailored to your specific requirements.
  • Perform system configurations: Shell scripts allow you to configure and manage system settings, install software packages, and set up environments.
  • Integrate tools and processes: You can use shell scripts to integrate different tools and processes into cohesive pipelines, enabling seamless collaboration and automation.

Tasks:

1 - Writing a Shell Script to Print a Message

Let's start with a simple example of a shell script that prints a message. Create a new file, such as task1.sh, and add the following code:

COPY

COPY

bashCopy code#!/bin/bash
echo "I will complete the #90DaysOfDevOps challenge"
        

In this script, the shebang #!/bin/bash specifies the interpreter as Bash. The echo command is used to display the message "I will complete the #90DaysOfDevOps challenge" on the terminal when the script is executed.

Save the file, make it executable (chmod 700 task1.sh), and run it (./task1.sh). You should see the message displayed on the terminal.

2 - Taking User Input and Printing Variables

Shell scripting allows you to interact with users and handle input dynamically. Here's an example of a script that takes user input, stores it in a variable, and prints the variables.

COPY

COPY

bashCopy code#!/bin/bash

# variable
var="I'm a variable"

# Read user input and store it in the variable 'name'
echo "Enter your name: "
read name

# Print the variable 'name'
echo "My name is $name"

# Print the predefined variable 'var'
echo "I'm printing the variable 'var': $var"
        

In this script, the read command is used to prompt the user to enter their name, and the input is stored in the name variable. The variable var is predefined with a value. The script then prints the user's name and the predefined variable value.

Executing this script will prompt the user to enter their name. After entering the name, the script will display the entered name and the predefined variable value.

3 - Using If-Else Statements in Shell Scripting

Conditional statements are essential for decision-making in shell scripts. Let's look at an example that uses an if-else statement to compare two numbers.

COPY

COPY

bashCopy code#!/bin/bash

# Read user input and store it in the variable 'number1'
echo "Enter the first number: "
read number1

# Read user input and store it in the variable 'number2'
echo "Enter the second number: "
read number2

# Compare both numbers and specify if they are equal or not
if [ $number1 -eq $number2 ]; then
    echo "Both numbers are the same"
else
    echo "Both numbers are different"
fi
        

In this script, the user is prompted to enter two numbers. The script then uses the if-else statement to compare the numbers. If the numbers are equal, it displays the message "Both numbers are the same". Otherwise, it displays "Both numbers are different".

Shell scripting provides powerful constructs like if-else statements, loops, and functions that allow you to build complex automation logic and decision-making within your scripts.

You've now learned the basics of Linux shell scripting for DevOps engineers. Shell scripting opens up a world of possibilities for automation, customization, and efficient management of your systems and processes.

Stay tuned for Day 5, where we will learn about advanced Linux Shell scripting for DevOps engineers with user management

Pawan Soni

Lead Android Developer | Financial Services | Android | Compose | Kotlin

1 年

Going Great Prashant C.

shashank singh

DevOps Engineer at BJS Hub

1 年

Very insightful

回复

要查看或添加评论,请登录

Prashant C.的更多文章

社区洞察

其他会员也浏览了