143 lines
2.6 KiB
Markdown
143 lines
2.6 KiB
Markdown
|
|
# Your First Bash Script
|
||
|
|
|
||
|
|
Time to build your first Bash script. We'll create a script to monitor server status, including:
|
||
|
|
* Disk usage
|
||
|
|
* CPU load
|
||
|
|
* RAM usage (Memory)
|
||
|
|
* TCP connections
|
||
|
|
* Kernel version
|
||
|
|
|
||
|
|
Customize as needed.
|
||
|
|
|
||
|
|
### Script Setup
|
||
|
|
|
||
|
|
First, create your script file, `status.sh`:
|
||
|
|
|
||
|
|
```bash
|
||
|
|
touch status.sh
|
||
|
|
```
|
||
|
|
|
||
|
|
Open `status.sh` with your text editor. Start with the shebang, `#!/usr/bin/env bash`. This tells the system how to execute the script.
|
||
|
|
|
||
|
|
Add initial comments for clarity:
|
||
|
|
|
||
|
|
```bash
|
||
|
|
#!/usr/bin/env bash
|
||
|
|
|
||
|
|
# Script to report current server status.
|
||
|
|
```
|
||
|
|
|
||
|
|
### Variables
|
||
|
|
|
||
|
|
Variables store data. Assign values using `=` (no spaces). Use `$()` for command substitution.
|
||
|
|
|
||
|
|
Capture the server's hostname:
|
||
|
|
|
||
|
|
```bash
|
||
|
|
server_name=$(hostname)
|
||
|
|
```
|
||
|
|
|
||
|
|
Echo the variable to see its value:
|
||
|
|
|
||
|
|
```bash
|
||
|
|
echo "${server_name}"
|
||
|
|
```
|
||
|
|
|
||
|
|
### Functions
|
||
|
|
|
||
|
|
Functions group commands for reuse. Let's create one for memory usage:
|
||
|
|
|
||
|
|
```bash
|
||
|
|
function memory_check() {
|
||
|
|
echo ""
|
||
|
|
echo "Memory usage on ${server_name}: "
|
||
|
|
free -h
|
||
|
|
echo ""
|
||
|
|
}
|
||
|
|
```
|
||
|
|
|
||
|
|
This function prints a message and calls `free -h` for memory details. Call it by name (no parentheses):
|
||
|
|
|
||
|
|
```bash
|
||
|
|
memory_check
|
||
|
|
```
|
||
|
|
|
||
|
|
Before seeing the complete solution, try implementing functions for:
|
||
|
|
* Disk usage
|
||
|
|
* CPU load
|
||
|
|
* TCP connections
|
||
|
|
* Kernel version
|
||
|
|
|
||
|
|
Google commands if needed. There are multiple valid approaches.
|
||
|
|
|
||
|
|
### The Complete Script
|
||
|
|
|
||
|
|
Here's the full `status.sh` script:
|
||
|
|
|
||
|
|
```bash
|
||
|
|
#!/usr/bin/env bash
|
||
|
|
|
||
|
|
##
|
||
|
|
# Server status script:
|
||
|
|
# - Memory usage
|
||
|
|
# - CPU load
|
||
|
|
# - TCP connections
|
||
|
|
# - Kernel version
|
||
|
|
##
|
||
|
|
|
||
|
|
server_name=$(hostname)
|
||
|
|
|
||
|
|
function memory_check() {
|
||
|
|
echo ""
|
||
|
|
echo "Memory usage on ${server_name}: "
|
||
|
|
free -h
|
||
|
|
echo ""
|
||
|
|
}
|
||
|
|
|
||
|
|
function cpu_check() {
|
||
|
|
echo ""
|
||
|
|
echo "CPU load on ${server_name}: "
|
||
|
|
echo ""
|
||
|
|
uptime
|
||
|
|
echo ""
|
||
|
|
}
|
||
|
|
|
||
|
|
function tcp_check() {
|
||
|
|
echo ""
|
||
|
|
echo "TCP connections on ${server_name}: "
|
||
|
|
echo ""
|
||
|
|
cat /proc/net/tcp | wc -l
|
||
|
|
echo ""
|
||
|
|
}
|
||
|
|
|
||
|
|
function kernel_check() {
|
||
|
|
echo ""
|
||
|
|
echo "Kernel version on ${server_name}: "
|
||
|
|
echo ""
|
||
|
|
uname -r
|
||
|
|
echo ""
|
||
|
|
}
|
||
|
|
|
||
|
|
function all_checks() {
|
||
|
|
memory_check
|
||
|
|
cpu_check
|
||
|
|
tcp_check
|
||
|
|
kernel_check
|
||
|
|
}
|
||
|
|
|
||
|
|
all_checks
|
||
|
|
```
|
||
|
|
|
||
|
|
Save `status.sh`, then make it executable and run:
|
||
|
|
|
||
|
|
```bash
|
||
|
|
chmod +x status.sh
|
||
|
|
./status.sh
|
||
|
|
```
|
||
|
|
|
||
|
|
### Conclusion
|
||
|
|
|
||
|
|
Bash scripting combines Linux commands to automate routine tasks, freeing you for more impactful work. This basic script demonstrates the power of automation.
|
||
|
|
|
||
|
|
> {notice} This content was inspired by a piece by softwareshinobi on Dev Team Six.
|