Essential Bash Scripting Tips and Practical Monitoring Scripts
This guide provides concise Bash scripting best practices, code snippets for generating random strings, color output functions, bulk user creation, package and service checks, host liveness verification, resource utilization monitoring, and website availability testing, all useful for system administration and interview preparation.
Precautions
1) Start scripts with the interpreter line: #!/bin/bash 2) Use four spaces for indentation and add plenty of comments.
3) Naming conventions: uppercase for constants, lowercase for local variables, lowercase for functions, names should reflect their purpose.
4) Variables are global by default; use local inside functions to limit scope.
5) Two useful debugging commands: set -e to exit on non‑zero status, set -x to print each command as it runs.
6) Always test scripts before deploying to production.
1. Generate Random String or Number
Generate a random 8‑character string:
Generate a random 8‑digit number:
Use cksum to print CRC checksum and byte count.
2. Define a Color Output String Function
Use the function keyword to define a function, with or without the keyword.
3. Bulk Create Users
4. Check If Package Is Installed
5. Check Service Status
6. Check Host Liveness
Method 1: Store erroneous IPs in an array and consider the host down after three ping failures.
Method 2: Use a FAIL_COUNT variable to track consecutive failures.
Method 3: Use a for loop to break on a successful ping; otherwise report failure.
7. Monitor CPU, Memory, and Disk Utilization
CPU: Use vmstat to analyze CPU statistics.
Memory:
Disk:
8. Bulk Host Disk Utilization Monitoring
Prerequisite: SSH password‑less or key‑based login between monitoring and target hosts.
Write a configuration file containing SSH connection info in the format: IP User Port.
9. Check Website Availability
1) Verify URL accessibility.
2) Test URL availability three times, using the same logic as host liveness checks.
These shell script examples are practical, frequently appear in interview questions, and are best learned by writing them yourself rather than copying and pasting.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
