项目文件夹

文件
wehub-resource-sync bb5c75ce05
Component Security Validation / Security Audit (push) Has been cancelled
Deploy to Cloudflare Pages / deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:38:58 +08:00

1 行
4.3 KiB
JSON

{"content": "---\nname: bash-linux\ndescription: Bash/Linux terminal patterns. Critical commands, piping, error handling, scripting. Use when working on macOS or Linux systems.\nallowed-tools: Read, Write, Edit, Glob, Grep, Bash\n---\n\n# Bash Linux Patterns\n\n> Essential patterns for Bash on Linux/macOS.\n\n---\n\n## 1. Operator Syntax\n\n### Chaining Commands\n\n| Operator | Meaning | Example |\n|----------|---------|---------|\n| `;` | Run sequentially | `cmd1; cmd2` |\n| `&&` | Run if previous succeeded | `npm install && npm run dev` |\n| `\\|\\|` | Run if previous failed | `npm test \\|\\| echo \"Tests failed\"` |\n| `\\|` | Pipe output | `ls \\| grep \".js\"` |\n\n---\n\n## 2. File Operations\n\n### Essential Commands\n\n| Task | Command |\n|------|---------|\n| List all | `ls -la` |\n| Find files | `find . -name \"*.js\" -type f` |\n| File content | `cat file.txt` |\n| First N lines | `head -n 20 file.txt` |\n| Last N lines | `tail -n 20 file.txt` |\n| Follow log | `tail -f log.txt` |\n| Search in files | `grep -r \"pattern\" --include=\"*.js\"` |\n| File size | `du -sh *` |\n| Disk usage | `df -h` |\n\n---\n\n## 3. Process Management\n\n| Task | Command |\n|------|---------|\n| List processes | `ps aux` |\n| Find by name | `ps aux \\| grep node` |\n| Kill by PID | `kill -9 <PID>` |\n| Find port user | `lsof -i :3000` |\n| Kill port | `kill -9 $(lsof -t -i :3000)` |\n| Background | `npm run dev &` |\n| Jobs | `jobs -l` |\n| Bring to front | `fg %1` |\n\n---\n\n## 4. Text Processing\n\n### Core Tools\n\n| Tool | Purpose | Example |\n|------|---------|---------|\n| `grep` | Search | `grep -rn \"TODO\" src/` |\n| `sed` | Replace | `sed -i 's/old/new/g' file.txt` |\n| `awk` | Extract columns | `awk '{print $1}' file.txt` |\n| `cut` | Cut fields | `cut -d',' -f1 data.csv` |\n| `sort` | Sort lines | `sort -u file.txt` |\n| `uniq` | Unique lines | `sort file.txt \\| uniq -c` |\n| `wc` | Count | `wc -l file.txt` |\n\n---\n\n## 5. Environment Variables\n\n| Task | Command |\n|------|---------|\n| View all | `env` or `printenv` |\n| View one | `echo $PATH` |\n| Set temporary | `export VAR=\"value\"` |\n| Set in script | `VAR=\"value\" command` |\n| Add to PATH | `export PATH=\"$PATH:/new/path\"` |\n\n---\n\n## 6. Network\n\n| Task | Command |\n|------|---------|\n| Download | `curl -O https://example.com/file` |\n| API request | `curl -X GET https://api.example.com` |\n| POST JSON | `curl -X POST -H \"Content-Type: application/json\" -d '{\"key\":\"value\"}' URL` |\n| Check port | `nc -zv localhost 3000` |\n| Network info | `ifconfig` or `ip addr` |\n\n---\n\n## 7. Script Template\n\n```bash\n#!/bin/bash\nset -euo pipefail # Exit on error, undefined var, pipe fail\n\n# Colors (optional)\nRED='\\033[0;31m'\nGREEN='\\033[0;32m'\nNC='\\033[0m'\n\n# Script directory\nSCRIPT_DIR=\"$(cd \"$(dirname \"${BASH_SOURCE[0]}\")\" && pwd)\"\n\n# Functions\nlog_info() { echo -e \"${GREEN}[INFO]${NC} $1\"; }\nlog_error() { echo -e \"${RED}[ERROR]${NC} $1\" >&2; }\n\n# Main\nmain() {\n log_info \"Starting...\"\n # Your logic here\n log_info \"Done!\"\n}\n\nmain \"$@\"\n```\n\n---\n\n## 8. Common Patterns\n\n### Check if command exists\n\n```bash\nif command -v node &> /dev/null; then\n echo \"Node is installed\"\nfi\n```\n\n### Default variable value\n\n```bash\nNAME=${1:-\"default_value\"}\n```\n\n### Read file line by line\n\n```bash\nwhile IFS= read -r line; do\n echo \"$line\"\ndone < file.txt\n```\n\n### Loop over files\n\n```bash\nfor file in *.js; do\n echo \"Processing $file\"\ndone\n```\n\n---\n\n## 9. Differences from PowerShell\n\n| Task | PowerShell | Bash |\n|------|------------|------|\n| List files | `Get-ChildItem` | `ls -la` |\n| Find files | `Get-ChildItem -Recurse` | `find . -type f` |\n| Environment | `$env:VAR` | `$VAR` |\n| String concat | `\"$a$b\"` | `\"$a$b\"` (same) |\n| Null check | `if ($x)` | `if [ -n \"$x\" ]` |\n| Pipeline | Object-based | Text-based |\n\n---\n\n## 10. Error Handling\n\n### Set options\n\n```bash\nset -e # Exit on error\nset -u # Exit on undefined variable\nset -o pipefail # Exit on pipe failure\nset -x # Debug: print commands\n```\n\n### Trap for cleanup\n\n```bash\ncleanup() {\n echo \"Cleaning up...\"\n rm -f /tmp/tempfile\n}\ntrap cleanup EXIT\n```\n\n---\n\n> **Remember:** Bash is text-based. Use `&&` for success chains, `set -e` for safety, and quote your variables!\n"}