Day 5 of 90-Day DevOps Challenge: Learn Shell Scripting in Linux

As I reached Day 5 of the 90-Day DevOps Challenge, I dove into the world of shell scripting in Linux. Shell scripting is a powerful tool that automates repetitive tasks, streamlines workflows, and enhances productivity—especially in the DevOps environment. Today, I explored the basics of writing and executing shell scripts, and I'm excited to share what I’ve learned.


What is Shell Scripting?

Shell scripting is the process of writing a series of commands in a text file, known as a script, that the shell (the command-line interpreter) executes. Scripts are used to automate tasks that would otherwise require manual input, making them invaluable for system administrators and DevOps engineers.


Why Shell Scripting Matters in DevOps

In DevOps, automation is key to managing complex environments and ensuring smooth operations. Shell scripting allows you to:

  • Automate Routine Tasks: Whether it’s setting up environments, managing backups, or deploying applications, shell scripts can handle these tasks automatically.

  • Improve Efficiency: By scripting repetitive commands, you save time and reduce the risk of human error.

  • Create Custom Tools: You can build custom command-line tools tailored to your specific needs, integrating them seamlessly into your workflow.


Getting Started with Shell Scripting

Here’s how I got started with shell scripting on Day 5:

  1. Writing Your First Shell Script

I began by creating a simple shell script. Here’s a step-by-step guide to writing and executing a basic script:

Step 1: Create a Script File: Use your favorite text editor (like nano or vim) to create a new file with a .sh extension. For example:

vim script.sh

Step 2: Write the Script: Inside the file, I wrote a simple script that prints "Hello, World!" to the terminal:

#!/bin/bash 
echo"Hello, World!"

Step 3: Save and Exit: Save the file and exit the text editor.

Step 4: Make the Script Executable: Before running the script, I made it executable using the chmod command:

chmod +x script.sh

Step 5: Execute the Script: Finally, I ran the script using the following command:

./script.sh

The output was a simple "Hello, World!" message—my first successful shell script!

  1. Understanding Basic Shell Script Components

After running a basic script, I explored the core components of shell scripting:

  • Shebang (#!/bin/bash): This is the first line in a script that tells the system which interpreter to use to execute the script. In this case, /bin/bash is the path to the Bash shell.

  • Variables: I learned how to define and use variables within a script. For example:

      #!/bin/bash
      greeting="Hello, DevOps!"
      echo $greeting
    

    This script stores the text "Hello, DevOps!" in a variable called greeting and then prints it.

  • Comments: Comments are added using the # symbol and are not executed by the script. They’re useful for documenting the script and explaining what each part does.

  • Input and Output: I experimented with reading input from the user and outputting results. For example:

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

    This script prompts the user to enter their name and then greets them.

  1. Implementing Conditional Statements

Next, I delved into conditional statements, which allow scripts to make decisions based on certain conditions:

  • if Statements: These are used to execute code based on a condition. For example:

      #!/bin/bash
      echo "Enter a number:"
      read number
      if [ $number -gt 10 ]
      then
              echo "The number is greater than 10."
      else
              echo "The number is 10 or less."
      fi
    

    This script checks if the user’s input is greater than 10 and prints a corresponding message.

  1. Looping Constructs

Looping is another powerful feature of shell scripting, enabling repeated execution of a block of code:

  • for Loops: I explored how to loop through a list of items:

      #!/bin/bash
      for i in 1 2 3 4 5
      do
      echo "Iteration $i"
      done
    

    This script prints "Iteration 1", "Iteration 2", and so on, up to 5.

  • while Loops: These loops continue as long as a condition is true:

      #!/bin/bash
      count=1
      while [ $count -le 5 ]
      do
      echo "Count is $count"
      ((count++))
      done
    

    This script prints the count from 1 to 5.


Practical Applications of Shell Scripting in DevOps

To see shell scripting in action, I created a script that automates the process of backing up a directory. Here’s how I did it:

Step 1: Create the Script: I wrote a script named backup.sh

#!/bin/bash 
source_dir="/home/tushar/projects" 
backup_dir="/home/tushar/backups" 
timestamp=$(date +%Y%m%d_%H%M%S) 
backup_file="$backup_dir/backup_$timestamp.tar.gz"
tar -czf $backup_file $source_dir
echo "Backup completed: $backup_file"

Step 2: Explanation: source_dir is the directory to be backed up. backup_dir is the location where the backup will be stored. timestamp generates a unique timestamp for the backup file name.

tar -czf

creates a compressed archive of the source directory.

Step 3: Execute the Script: I made the script executable and ran it. The script created a backup of my project directory, naming it with the current timestamp.


Conclusion

Day 5 was all about unlocking the power of shell scripting in Linux. I explored how scripts can automate tasks, manage complex operations, and enhance productivity—skills that are essential in the DevOps field. By understanding the basics of scripting, including variables, conditionals, and loops, I’m now equipped to create more sophisticated scripts that will streamline my DevOps workflows.

As I move forward in the challenge, I’m eager to build upon these scripting skills, applying them to real-world DevOps tasks. Stay tuned for Day 6, where I’ll be delving into more advanced topics and continuing this exciting journey through the world of DevOps!