Learn shell scripting to automate tasks on your Linux computer. Write, debug, and run your first Bash script for backups, gaining confidence to create your own scripts.
Summary
Shell scripting automates Linux tasks using Bash scripts. This chapter guides you through planning, writing, and debugging a backup script, covering variables, conditionals, and logging, with best practices for beginners.
Learning Objectives: Plan and write a shell script, make it executable, debug issues, and apply scripting to real-world tasks like backups.
Why Learn Shell Scripting?
Shell scripting streamlines repetitive tasks, such as backups or system monitoring, saving time and enhancing Linux efficiency.
Planning Your Script
Define goals for a backup script: – Back up a directory. – Compress to .tar.gz. – Move to a backup location. – Log the process.
Writing Your First Bash Script
Define Variables
Set paths and names:
#!/bin/bash
SOURCE_DIR="/home/user/documents"
BACKUP_DIR="/home/user/backups"
LOG_FILE="/home/user/backup.log"
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
BACKUP_NAME="backup_$TIMESTAMP.tar.gz"
Create Backup Directory
Ensure the directory exists:
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR"
echo "Backup directory created: $BACKUP_DIR" | tee -a "$LOG_FILE"
fi
Compress and Backup
Use tar to compress the filed in a folder. Let us say you have a directory or a folder with images. You can manually create the archive or use a script to do so.
echo "Backup started at $(date)" | tee -a "$LOG_FILE"
tar -czf "$BACKUP_DIR/$BACKUP_NAME" "$SOURCE_DIR"
if [ $? -eq 0 ]; then
echo "Backup successful: $BACKUP_DIR/$BACKUP_NAME" | tee -a "$LOG_FILE"
else
echo "Backup failed!" | tee -a "$LOG_FILE"
exit 1
fi
echo "Backup completed at $(date)" | tee -a "$LOG_FILE"
Making the Script Executable
Save as backup_script.sh and set permissions:
$ chmod +x backup_script.sh
Running the Script
Execute:
$ ./backup_script.sh
Debugging Bash Scripts
- Enable debug mode: Add
set -x. - Check exit codes:
echo $?. - Test commands individually.
Best Practices
- Use absolute paths.
- Add comments:
# Purpose. - Redirect output:
| tee -a log. - Test scripts in a safe environment.
Practical Examples
Modify to back up /var/log:
$ SOURCE_DIR="/var/log"
Delete old backups:
$ find "$BACKUP_DIR" -mtime +7 -delete
Practice Time!
Test your skills:
1. Modify the script to back up /var/www.
2. Add a feature to delete backups older than 7 days.
3. Debug using set -x. 4. Check the log file.
Try This: Run
./backup_script.shand share your success on X with #LinuxCommandLine!
Shell Script Command Reference
| Command | Description |
|---|---|
chmod +x |
Makes script executable. |
tar -czf |
Creates compressed archive. |
tee -a |
Appends output to file. |
set -x |
Enables debug mode. |
Glossary of Terms
| Term | Description |
|---|---|
| Shell scripting | Automating tasks with scripts. |
| Bash script | Script written in Bash. |
| Shebang | #!/bin/bash defines interpreter. |
Reference: See Linux Manpages for details.
Conclusion
You’ve written your first Bash script to automate backups, mastering shell scripting basics. Experiment with new features and explore AI tools to generate or debug scripts. Next, learn about backups to enhance your automation skills.
Shell scripting is an essential skill for any Linux user, from beginners to system administrators. This chapter has provided you with the foundational knowledge to create and debug your own scripts. As you continue to practice and explore more advanced scripting techniques, you’ll find even more ways to streamline your workflows and enhance your productivity.