☑️Day 10: Diving in Shell Scripting

☑️Day 10: Diving in Shell Scripting

🔹Table Of content :

  • ✅Introduction

  • ✅What is Bash?

  • ✅Difference Between a Script and a Program

  • ✅Shell Script Basics

    • ✅File Extension

      ✅Running a Script

  • ✅SSH and Scripting

  • ✅Key Concepts Covered Today

    • ✅Variables in Shell Scripts

      ✅Passing Arguments to Scripts

      ✅Taking User Input

      ✅Simple Conversations Using sleep

      ✅Simple if-else Script

  • ✅Takeaways from Day 10

  • ✅What’s Next?


Hey DevOps Enthusiasts,

Welcome to Day 10 of my #90DaysOfDevOps challenge! Today, I took my first steps into the world of Shell Scripting using Bash, which is an essential skill for automating tasks, managing server configurations, and enhancing efficiency in DevOps workflows.

Here’s a recap of what I covered today, along with simple scripts to help you get started with Bash scripting.


✅What is Bash?

Bash (Bourne Again SHell) is a command-line interpreter commonly used in Unix/Linux environments. It allows you to execute commands, automate tasks, and write scripts that interact with the OS.


Difference Between a Script and a Program

  • Script: A series of commands executed sequentially by an interpreter (e.g., Bash).

  • Program: A compiled set of instructions that is executed by the system (e.g., C, Java).


✅Shell Script Basics

  • File extension: Shell scripts typically have the .sh extension.

  • Running a script: First, create a script with nano, and then run it using ./ after making it executable.

nano script.sh      # Create or edit a script
chmod 777 script.sh  # Make the script executable
./script.sh         # Execute the script

SSH and Scripting

Although I previously covered SSH for connecting to remote servers, it’s essential to know this when starting to script on EC2 or any other server.

ssh -i "your-key.pem" ec2-user@ec2-xx-xxx-xx-xx.compute.amazonaws.com

This command allows you to remotely access your server, where you can create and run your shell scripts.


✅Key Concepts Covered Today

✅1. Variables in Shell Scripts

Variables are used to store data, which can be reused throughout the script.

#!/bin/bash
name="Kedar"
echo "Hello, $name!"

✅2. Passing Arguments to Scripts

You can pass arguments to your shell script when running it.

#!/bin/bash
echo "Argument 1: $1"
echo "Argument 2: $2"

Run it like this:

./script.sh arg1 arg2

✅3. Taking User Input

Taking input from users is useful for interactive scripts.

#!/bin/bash
echo "Enter your name:"
read name
echo "Hello, $name!"

✅4. Simple Conversations Using sleep

To simulate a conversation in the terminal, you can use the sleep command to pause between messages.

#!/bin/bash
echo "Hello!"
sleep 2
echo "How are you?"
sleep 2
echo "Goodbye!"

✅5. Simple if-else Script

An if-else statement allows decision-making within a script.

#!/bin/bash
if [ "$1" = "Ram" ]
then
 echo "Hello, Ram"
else
 echo "hello, everyone"
fi

Takeaways from Day 10

  • Understanding Bash: I now have a clearer understanding of what Bash is and how it fits into DevOps workflows.

  • Variables and Arguments: I learned how to create variables and pass arguments into shell scripts.

  • Interactive Scripting: Taking user input and using the if-else structure is an essential part of writing dynamic scripts.

  • Automating with SSH: Being able to automate tasks on remote servers via SSH is a powerful tool in my DevOps toolkit.


What’s Next?

Next, I’ll dive deeper into loops and more advanced shell scripting techniques. Automation is at the heart of DevOps, and these scripting skills will be key to enhancing my workflow.


🚀Stay tuned for more updates on my DevOps journey! 📅

Feel free to reach out to me if you have any questions or need any more help. Let’s connect, learn, and succeed together!

Thanks for joining me on Day 10 of this exciting journey. Let’s keep leveling up our skills, one productive hour at a time!

Happy Learning!😊

#90DaysOfDevOps

💡
Follow for more updates on LinkedIn , Github and Twitter(X)