| name | Script-Hub Agent Instructions |
|---|---|
| description | Guide for creating, reviewing, and maintaining bash scripts in the Script-Hub repository. Use when working with shell scripts, system administration tasks, or automating Linux operations. |
Script-Hub is a personal collection of Bash scripts for daily system administration and automation. Each script handles specific tasks:
- System updates and maintenance
- Development environment setup
- System configuration and cleanup
This is a working repository, updated as new tasks or patterns emerge.
- Use lowercase with hyphens:
daily-update.sh,get-docker.sh - Avoid underscores; prefer hyphens for multi-word names
Every script should include:
#!/bin/bash
set -e # Exit on error
# Brief description of what the script doesKey safety practices:
- Always use
set -eto exit immediately on errors - Use
set -o pipefailif piping commands - Quote variables:
"$var"not$var - Validate commands exist before running:
if command -v flatpak &> /dev/null; then ...
- Use
echostatements to log progress:echo "Updating package lists..." - Prefix destructive operations with confirmation or clear messaging
- Test with dry-run flags when applicable (e.g.,
apt,flatpak)
#!/bin/bash
set -e
echo "Starting task..."
sudo apt update -y
echo "Task complete."| Script | Purpose | Frequency |
|---|---|---|
daily_update.sh |
System package updates (apt, flatpak) and cleanup | Daily |
get-docker.sh |
Docker Engine installation (official Docker script) | As-needed |
Note: get-docker.sh is maintained by Docker; any changes should preserve its integrity as a baseline reference.
- Create file with
.shextension, following naming convention - Add
#!/bin/bashheader andset -e - Include comment summarizing script purpose
- Add progress messages for each significant step
- Test locally before committing
- Update README.md with script description
- Test in a non-production environment or container
- Verify all error paths (e.g., missing dependencies)
- Check that progress messages are clear
- Ensure safe exit behavior with
set -e
Keep README.md in sync with available scripts. Include:
- Brief description of each script
- Usage instructions
- Any prerequisites or warnings
- Mark breaking changes clearly in comments:
# v2.0: Changed default behavior (see README) - Include a version comment near the top:
# script version: 1.2 - Document change history inline or in README for important updates
- If retiring a script, rename with
.deprecatedsuffix:old-script.deprecated.sh - Add deprecation notice at the top:
# DEPRECATED: Use new-script.sh instead - Keep in repository for reference but do not execute in automation
- Test locally in a safe environment (container, non-production machine)
- For destructive operations (apt upgrade, cleanup), verify with dry-run mode first
- Check error paths: missing dependencies, missing directories, insufficient permissions
- Verify progress messages are clear and appear at expected intervals
if command -v flatpak &> /dev/null; then
flatpak update -y
else
echo "Flatpak not found, skipping..."
fiAlways verify dependencies before using them:
# Check for required commands
for cmd in apt flatpak sudo; do
if ! command -v "$cmd" &> /dev/null; then
echo "Error: $cmd not found. Install it and try again."
exit 1
fi
done- Prefix status messages with timestamps for audit trails:
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Updating packages..." - Log to both console and file for critical operations:
LOG_FILE="/var/log/script-hub.log"
echo "Task started" | tee -a "$LOG_FILE"- Use
>&2for error messages to send them to stderr:echo "Error: failed to update" >&2
- Export variables needed by subprocesses:
export DEBIAN_FRONTEND=noninteractive - Use default values for optional vars:
TIMEOUT=${TIMEOUT:-30} - Validate environment before proceeding:
if [[ -z "$HOME" ]]; then
echo "Error: HOME not set" >&2
exit 1
fiTrap signals to ensure cleanup happens:
cleanup() {
echo "Cleaning up temporary files..."
rm -f "$TEMP_FILE"
exit 0
}
trap cleanup EXIT INT TERM- Use
sudofor operations requiring root - Make privilege requirements explicit in documentation
- Consider adding a header comment:
# Requires: sudo privileges - Verify sudo availability:
if ! sudo -n true 2>/dev/null; then echo "sudo not available"; exit 1; fi
Always use set -o pipefail when combining set -e with pipes:
#!/bin/bash
set -e
set -o pipefail
# This will now exit if grep fails, even in a pipeline
cat large-file.txt | grep "pattern" | wc -lSupport a dry-run mode for destructive operations:
# Usage: DRY_RUN=1 ./daily_update.sh
if [[ "${DRY_RUN:-0}" == "1" ]]; then
echo "[DRY RUN] Would execute: sudo apt update -y"
else
sudo apt update -y
fi-
❌ Piping to
shdirectly — Never usecurl ... | sh. Always download to a file, review, then execute:# Bad: curl https://get.docker.com | sh # Good: curl -o get-docker.sh https://get.docker.com && bash get-docker.sh
-
❌ Ignoring errors silently — Always use
set -eor explicit error checks:# Bad: silently continues if apt fails apt update apt install -y package # Good: exits immediately on error set -e apt update apt install -y package
-
❌ Forgetting
set -o pipefailwith pipes — Without it,set -ewon't catch failures in piped commands:# Bad: grep failure is silent set -e cat file.txt | grep "pattern" | sort # If grep fails, it doesn't exit # Good: grep failure causes exit set -e set -o pipefail cat file.txt | grep "pattern" | sort # Now it exits on any failure
-
❌ Unquoted variables — Leads to word splitting and globbing bugs:
# Bad: $var is split and expanded echo $var # Good: preserves exact value echo "$var"
-
❌ Hardcoding paths that may differ across systems:
# Bad: assumes path /opt/appname/run.sh # Good: check existence or use which if [[ -f /opt/appname/run.sh ]]; then /opt/appname/run.sh; fi
-
❌ Missing progress feedback — Users should know what's happening:
# Bad: no output, user thinks it hung sudo apt update && sudo apt upgrade -y # Good: clear progress echo "Updating package lists..." sudo apt update -y echo "Upgrading packages..." sudo apt upgrade -y
-
❌ No error context — Errors should be informative:
# Bad: vague error cd /nonexistent 2>/dev/null || exit 1 # Good: specific error message cd /nonexistent || { echo "Error: directory not found" >&2; exit 1; }
For questions about script patterns, error handling, or adding new automation, refer to this guide or ask for help reviewing the pattern.