#90DaysOfDevOps Challenge - Day 4 -Basic Linux Shell Scripting for DevOps Engineers

Cloud Engineer and DevOps enthusiast, just talking about AWS, Docker, Linux, Kubernetes, Python, and automation.
Search for a command to run...

Cloud Engineer and DevOps enthusiast, just talking about AWS, Docker, Linux, Kubernetes, Python, and automation.
No comments yet. Be the first to comment.
The #90DaysOfDevOps Challenge is a dedicated initiative aimed at promoting continuous learning and skill development in the field of DevOps over a 90-day period.
Running your own home lab used to mean expensive hardware, complex networking, and hours of manual configuration. This project changes that. With a single Raspberry Pi, Docker Compose, and a few hundr

Welcome to an advanced hybrid directory demo where we'll guide you through setting up a hybrid environment, connecting to simulated on-premises infrastructure, creating a managed Microsoft AD within AWS, establishing a two-way forest trust, and integ...

Hello DevOps Community! Today marks the completion of the #90DaysOfDevOps Challenge, and it's a moment to reflect on the journey we've undertaken together. Throughout this period, I've engaged with various technologies, refining my skills in the DevO...

In a modern cloud infrastructure setup, creating a robust and scalable 2-tier architecture is essential. Such an architecture helps meet the demands of your applications while ensuring repeatability, reliability, and security. In this article, we'll ...

Welcome to Part 4 of my series on building a comprehensive CI/CD pipeline in AWS. In this article, I will explore AWS CodePipeline, a powerful service for building and deploying applications in a continuous integration and continuous delivery (CI/CD)...

Welcome to Day 4 of the #90DaysOfDevOps challenge. In today's session, we will explore the basics of Linux shell scripting and its relevance for DevOps engineers. Shell scripting is a powerful skill that allows you to automate tasks, manage system configurations, and streamline DevOps workflows. Let's dive in and understand the fundamentals of shell scripting.
In simple terms, shell scripting involves writing a series of commands in a script file to be executed by the shell interpreter. The shell acts as an interface between the user and the operating system, interpreting and executing the commands in the script.
For DevOps engineers, shell scripting plays a crucial role in automating repetitive tasks, performing system configurations, managing infrastructure, and orchestrating various tools and processes. It allows DevOps professionals to create custom workflows, automate deployments, monitor system health, 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.
#!/bin/bash? Can we write #!/bin/sh as well?The #!/bin/bash (shebang) is called an interpreter directive and specifies the interpreter to be used for executing the script. In this case, it indicates that the script should be interpreted using the Bash shell. Bash (Bourne Again SHell) is a widely used shell and the default shell on most Linux distributions.
Yes, you can write #!/bin/sh instead of #!/bin/bash. The sh interpreter is a more generic reference to the system's default shell. It may or may not be the same as Bash, depending on the system. Using #!/bin/sh allows the script to be executed by the system's default shell, which could be Bash or another compatible shell.
It's important to note that Bash offers more features and capabilities compared to the basic POSIX shell (/bin/sh). If your script requires specific Bash features, it's recommended to use #!/bin/bash to ensure compatibility.
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:
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.
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.
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.
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.
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