Lesson 11: Practical Linux Bash Examples and Projects
Welcome to Lesson 11: Practical Linux Bash Examples and Projects. Mastering the Linux command line and Bash scripting can greatly boost your productivity. In this lesson, we will explore practical Bash examples and projects. These examples show how to apply Bash scripting to real-world tasks.
Introduction
This lesson is designed for Linux users who want to automate tasks, simplify processes, and solve challenges. We’ll focus on hands-on examples to help you work more efficiently.
Some key topics we’ll cover include:
- Automating System Maintenance: Use Bash scripts to automate tasks like system updates, file clean-up, and backups.
- File Manipulation: Learn to move, copy, delete, and organize files with Bash scripts.
- Text Processing with Regular Expressions: Discover how to use
grep
,sed
, andawk
for searching and editing text. - Batch Renaming Files: Write scripts to rename or organize large sets of files, useful for things like photo sorting or log file management.
- Scheduling Tasks with Cron: Use
cron
to schedule and automate Bash scripts at specific times.
These practical examples and projects demonstrate the versatility and power of Bash scripting. By applying these scripts to real-world tasks, you can automate repetitive processes, improve system management, and enhance your overall productivity.
Example 1: Solution: Automate Daily Backup of Remote Ubuntu Server Folder with a Bash Script
Backing up important data from remote servers to local systems is a critical part of maintaining data integrity and system reliability. In this script, we automate the process of synchronizing a remote folder with a local backup directory using the rsync
tool.
Key Features
- Logs the time the backup operation starts.
- Initiates a synchronization process between a remote system and a local server.
- Logs all output to a dedicated backup log file for tracking and troubleshooting
Example 2: Solution: CPU Temperature Monitoring on Raspberry Pi4
Monitoring system temperatures is crucial for ensuring hardware stability and preventing overheating, especially in servers and high-performance machines. This script provides a simple solution to periodically check the temperature of your system and log the data for future reference.
Key Features
- Logs Temperature Data: The script reads the temperature of the system’s sensors (e.g.,
temp1
fromsensors
) and logs it with a timestamp to a log file. - Daily Log Files: The log file is named based on the date, ensuring that each day’s temperature readings are stored separately.
- Continuous Monitoring: The script runs in an infinite loop, capturing temperature readings every 10 minutes (
sleep 600
), and checks if the date has changed to create a new log file for the next day.
Solution: CPU Temperature Monitoring on Raspberry Pi4
Example 3: Solution: Ubuntu Internet Connection Monitoring Script (Create a Log File, Possible to Send per Email)
In many cases, maintaining an active internet connection is crucial for servers or devices that rely on online services or monitoring. This script automates the process of checking the internet connection by pinging a specific IP address. If connectivity fails repeatedly, the script logs the failure and can trigger actions to remedy the issue.
Key Features
- Ping Monitoring: The script continuously pings a remote IP address (set to
8.8.8.8
by default, which is Google’s DNS server) to check connectivity. - Logging Failed Pings: If the connection is lost, the script logs each failure, recording the time and the number of consecutive lost pings.
- Threshold for Failure: If a specified number of consecutive pings fail, the script logs an alert and takes further actions, such as restarting a service or notifying an administrator.
- Daily Log Files: It creates a new log file each day to store the results of the connectivity checks.
Example 4: Solution: Connect from Ubuntu to the FortiGate Firewall and Execute Command (Bash Script)
Managing network interfaces effectively is essential for troubleshooting network connectivity or applying temporary configuration changes. This script automates the process of disabling and enabling the wan1
interface on a FortiGate device via SSH.
Key Features
- SSH Command Execution: The script connects to the FortiGate device using SSH on a custom port (default 11022) and sends the commands necessary to disable and enable the
wan1
interface. - Logging: The script logs each significant action, such as disabling and enabling the interface, with timestamps. It saves the log file to a location on the local server for tracking the script’s execution.
- Time Delay: The script waits for 60 seconds between disabling and enabling the interface, providing enough time for any network changes to take effect.
- Password Handling: Passwords are currently passed as plain text using
sshpass
, though it’s highly recommended to use SSH keys or prompt for passwords securely to avoid hardcoding them in scripts.
Solution: Connect from Ubuntu to the FortiGate Firewall and Execute Command (Bash Script)