Welcome back to Day 8 of the #90DaysOfDevOps Challenge! Today, we're diving deep into the basics of Bash Scripting, a powerful tool in every DevOps engineer's toolkit. Whether you're automating tasks or configuring your server environment, shell scripting is essential. Let's break down todayβs tasks and get hands-on with some scriptwriting.
π‘ Task 1: Comments in Bash Scripts
Comments are essential for understanding and maintaining scripts. They are used to add notes, disable certain lines of code, or explain the purpose of the script.
Challenge:
Write a bash script that includes comments explaining what each part of the script does.
!/bin/bash
This is a comment explaining the script's purpose
echo "Welcome to Day 8 of DevOps!"
The script will print a welcome message above
π‘ Task 2: Echo Command
The echo command is used to display messages to the terminal. This is handy for showing information or the result of commands.
Challenge:
Create a bash script that prints a message of your choice.
!/bin/bash
Displaying a message using echo
echo "Learning DevOps is fun!"
π‘ Task 3: Working with Variables
Variables are placeholders used to store data. You can use them in your scripts to store values like strings, integers, or command results.
Challenge:
Create a bash script that declares variables and assigns values to them.
!/bin/bash
Declaring and assigning variables
name="DevOps Ninja"
age=25
echo "My name is $name and I am $age years old."
π‘ Task 4: Using Variables to Perform a Task
Let's get interactive! You can perform operations using variables in bash scripting. Hereβs how you can add two numbers.
Challenge:
Create a bash script that takes two variables as input and prints their sum.
!/bin/bash
Adding two numbers using variables
num1=15
num2=25
sum=$((num1 + num2))
echo "The sum of $num1 and $num2 is $sum."
π‘ Task 5: Utilizing Built-in Variables
Bash has several built-in variables that provide useful information like the script name, the number of arguments passed, and more.
Challenge:
Create a bash script that uses at least three built-in variables to display information.
!/bin/bash
Using built-in variables
echo "Script Name: $0"
echo "Number of arguments passed: $#"
echo "The process ID of this script: $$"
π‘ Task 6: Using Wildcards for Pattern Matching
Wildcards are special characters that help match patterns. They're useful when working with multiple files.
Challenge:
Create a bash script that lists all files with a specific extension in a directory.
!/bin/bash
Listing all .txt files in the current directory
echo "Listing all .txt files in the directory:"
ls *.txt
β
Submission Guidelines:
Complete the tasks in a single bash script.
Document your script with comments explaining each section.
Upload your script to a GitHub repository.
Share your submission with the community!
Conclusion: Bash scripting might seem daunting at first, but with practice, it becomes a valuable tool for automating tasks in your DevOps workflow. As we advance through the #90DaysOfDevOps Challenge, youβll build stronger scripting skills and be ready for more complex automations.
What's Next?
Tomorrow, weβll tackle more advanced scripting concepts, so stay tuned and keep scripting!
Top comments (1)
Interesting article. Some comments:
You need to put your example code in markdown code blocks to avoid this site reformatting it. (Triple backticks on a line by themselves before and after the code)
The shebang isn't there to "explain" anything. It functions as a magic number which tells the loader what program to run to process the rest of the file when it is on the first line of a file which has execute permission when that file is presented as a command. It's structured as a comment so the chosen program will ignore it.
$0
contains the stated path of the script being run (possibly a relative or absolute path to the file). It has to be edited if you want just the command name. E.g.scriptname="${0##*/}"