In the previous post, we covered the basics of navigating and managing files in Linux. Now, it’s time to take your Linux skills to the next level by learning how to automate tasks using shell scripting. Bash, the Bourne Again Shell, is a powerful scripting language that can help you automate repetitive tasks, manage system operations, and streamline your workflows. This post will introduce you to the basics of shell scripting, including creating, running, and debugging scripts, along with examples of common automation tasks.

What is Shell Scripting?

Shell scripting is the process of writing a series of commands for the shell to execute. A shell script is a file containing these commands, which can be executed to perform a variety of tasks, from simple file operations to complex system administration tasks.

Creating a Bash Script

Step 1: Create a New Script File

To create a new bash script, open your text editor and create a new file with the .sh extension. For example, you can create a file named script.sh.

nano script.sh

Step 2: Add the Shebang

The first line of your script should be the shebang (#!), which tells the system which interpreter to use to execute the script. For bash scripts, the shebang is:

#!/bin/bash

Step 3: Write Your Script

Below the shebang, you can add the commands you want to execute. Here’s an example script that prints “Hello, World!” to the terminal:

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

Step 4: Save and Close the File

After writing your script, save the file and exit the text editor.

Running a Bash Script

Step 1: Make the Script Executable

Before you can run your script, you need to make it executable using the chmod command:

chmod +x script.sh

Step 2: Execute the Script

Now, you can run the script by typing ./ followed by the script name:

./script.sh

You should see “Hello, World!” printed in the terminal.

Basic Scripting Concepts

Variables

You can define variables in your script to store data and use it later. Here’s an example:

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

Loops

Loops allow you to repeat a set of commands multiple times. The for loop and while loop are commonly used in bash scripting.

For Loop Example:

#!/bin/bash
for i in {1..5}; do
  echo "Iteration $i"
done

While Loop Example:

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

Conditionals

Conditionals allow you to execute commands based on certain conditions. The if statement is commonly used for this purpose.

If Statement Example:

#!/bin/bash
number=5
if [ $number -gt 3 ]; then
  echo "The number is greater than 3"
else
  echo "The number is not greater than 3"
fi

Functions

Functions allow you to group commands into reusable blocks of code.

Function Example:

#!/bin/bash
greet() {
  echo "Hello, $1!"
}
greet "Alice"
greet "Bob"

Debugging Bash Scripts

Debugging is an essential part of writing scripts. Bash provides several options to help you debug your scripts.

Enable Debugging Mode

You can run your script in debugging mode by using the -x option:

bash -x script.sh

This will print each command and its arguments as they are executed, helping you identify where things might be going wrong.

Use Echo Statements

Adding echo statements to your script can help you understand the flow of execution and the values of variables at different points.

#!/bin/bash
number=5
echo "The current value of number is $number"

Common Automation Tasks

Automated Backups

Create a script to back up a directory:

#!/bin/bash
backup_source="/path/to/source"
backup_dest="/path/to/backup"

tar -czf "$backup_dest/backup-$(date +%F).tar.gz" "$backup_source"
echo "Backup completed successfully!"

System Monitoring

Create a script to check disk usage and send an alert if it exceeds a certain threshold:

#!/bin/bash
threshold=80
usage=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')

if [ $usage -gt $threshold ]; then
  echo "Warning: Disk usage is above $threshold%!"
fi

Conclusion

Shell scripting with Bash is a powerful way to automate tasks, enhance productivity, and streamline system management. By understanding the basics of creating, running, and debugging scripts, you can tackle a wide range of automation tasks in your day-to-day work as a systems engineer. Practice writing scripts for common tasks, and you’ll soon find that shell scripting becomes an indispensable tool in your arsenal.

Stay tuned for more advanced topics and practical examples to further enhance your scripting skills and system automation capabilities. Happy scripting!