Bash Script Exercises
Bash Script Exercises
please provide my 50 most exercices for bash script to automate most tasks efficiently and cool
Here are 50 Bash scripting exercises to help you master automation efficiently and explore cool use
cases. They are categorized based on difficulty and practical usage:
11. Automate user creation with a default password and set expiry for the account.
12. Write a script to monitor disk usage and send an alert if it exceeds 80%.
13. Automate searching for specific text in log files (e.g., `/var/log/syslog`).
14. Create a script to rename all `.txt` files in a directory to `.bak`.
15. Write a script to schedule a cron job for daily system updates.
16. Automate downloading a file from a URL and verifying its checksum.
17. Create a script to extract all email addresses from a given text file.
18. Automate the generation of SSH keys and configure them for remote login.
19. Write a script to monitor a folder and log any changes to its contents.
20. Automate setting up a simple HTTP server using Python or `python3 -m http.server`.
21. Create a script to monitor CPU usage and kill processes exceeding a set limit.
22. Automate archiving log files older than 7 days in `/var/log`.
23. Write a script to find and delete duplicate files in a directory.
24. Automate database backups for MySQL/MongoDB and encrypt them with GPG.
25. Create a menu-driven script for common system tasks (e.g., user creation, updates).
26. Write a script to check for open ports on the system and save the results.
27. Automate setting up a LAMP/LEMP stack (Linux, Apache/nginx, MySQL, PHP).
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 1/8
28. Create a script to detect the largest files on the system and move them to a folder.
29. Write a script to automate Git operations (add, commit, push).
30. Automate transferring files between machines using SCP or Rsync.
31. Write a script to scan a network for active hosts (basic port scanning).
32. Automate detecting and blocking IP addresses with too many failed SSH login attempts.
33. Create a script to set up a simple firewall with `iptables` rules.
34. Automate capturing network traffic using `tcpdump` and save it to a file.
35. Write a script to monitor SSL certificate expiry dates for a list of domains.
36. Automate downloading, extracting, and analyzing website data with `wget` or `curl`.
37. Create a script to configure a static IP address for a machine.
38. Automate setting up VPN connections using OpenVPN.
39. Write a script to automate converting `.csv` files to `.json` using Bash tools.
40. Automate finding and replacing text in multiple files using `sed` or `awk`.
41. Create a script to sort a large text file and remove duplicates.
42. Write a script to split a text file into smaller chunks based on line count.
43. Automate generating random passwords and saving them securely.
44. Create a script to parse and display specific fields from a CSV file.
45. Automate creating a custom Bash prompt (`PS1`) with system info.
46. Write a script to monitor system resource usage and log hourly reports.
47. Automate creating user-specific aliases and adding them to `.bashrc`.
48. Create a script to automatically clean up temporary files every day.
49. Automate installing and updating packages for a list of software (e.g., `apt-get` or `yum`).
50. Write a script to fetch weather information from an API and display it neatly.
Would you like me to elaborate on any specific exercise or help you get started with one of them?
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 2/8
Bash Scripting Exercises
# Exercise 1: Display "Hello, World!"
echo "Hello, World!"
Here’s a comprehensive
# Exercise course to help you understand and master `awk` under Linux. The course is
8: Backup a folder
divided into modules, progressing
read -p "Enter folder name from basics
to backup: to advanced concepts.
" folder
tar -czf backup_$(date +%F).tar.gz "$folder"
Module 1: 9:
# Exercise Introduction to `awk`
Display IP address
1. What is `awk`?
A programming language for pattern scanning and processing.
Commonly used for text processing and data extraction.
2. Basic Syntax
bash
bash
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 3/8
Prints the first column of `file.txt`.
4. Key Features
Field and record-based processing.
In-built variables and operators.
Support for regex.
1. Field Separator
bash
bash
Module 3: Patterns
1. Matching Patterns
bash
bash
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 4/8
bash
Module 4: Actions
1. Print
bash
2. Formatting Output
bash
3. Arithmetic Operations
bash
bash
1. `if-else`
bash
awk '{if ($3 > 50) print $1, $3; else print $1, "Fail"}' scores.txt
2. `for` Loops
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 5/8
bash
3. `while` Loops
bash
Module 6: Functions
1. Built-in Functions
String Functions:
bash
Math Functions:
bash
2. User-Defined Functions
bash
1. Basic Patterns
bash
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 6/8
bash
bash
awk 'FNR==NR {a[$1]=$2; next} {print $1, a[$1], $2}' file1 file2
2. Merge Files
bash
bash
bash
bash
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 7/8
1. Debugging Mode
bash
awk -d script.awk
2. Optimizing Performance
Reduce unnecessary operations.
Use proper regex patterns.
bash
2. Data Transformation
bash
3. Dynamic Reports
bash
Printed using Save ChatGPT as PDF, powered by PDFCrowd HTML to PDF API. 8/8