Shell Integration
Rawi’s shell integration allows you to seamlessly combine AI assistance with standard Unix tools, pipes, redirects, and shell scripting for powerful automation workflows.
Pipe Integration
Section titled “Pipe Integration”Input Pipes
Section titled “Input Pipes”Send data to Rawi through pipes for analysis and processing:
# Analyze log filestail -f /var/log/app.log | rawi ask "Summarize any errors in this log"
# Code reviewcat src/app.js | rawi ask --act code-reviewer "Review this JavaScript code"
# Process JSON datacurl -s https://api.example.com/data | rawi ask "Analyze this API response"
# Analyze command outputps aux | rawi ask "Which processes are using the most memory?"
# Git integrationgit log --oneline -10 | rawi ask "Summarize these recent commits"
Output Pipes
Section titled “Output Pipes”Chain Rawi output with other tools:
# Save AI responsesrawi ask "Explain Docker containers" | tee docker-notes.txt
# Process AI outputrawi ask "List 10 popular Python libraries" | grep -E "^\d+" | sort
# Format outputrawi ask "Generate a JSON example" | jq '.'
# Count words in responserawi ask "Write a short essay on AI" | wc -w
Bidirectional Pipes
Section titled “Bidirectional Pipes”Combine input and output pipes for complex workflows:
# Analyze and savecat error.log | rawi ask "Categorize these errors" | tee error-analysis.txt
# Process and filtercurl -s api/users.json | rawi ask "Extract email addresses" | grep "@company.com"
# Chain multiple AI operationscat code.py | rawi ask --act code-reviewer "Find issues" | rawi ask "Prioritize these issues"
File Operations
Section titled “File Operations”Reading Files
Section titled “Reading Files”# Analyze configuration filesrawi ask "Explain this nginx config" < nginx.conf
# Code documentationrawi ask --act technical-writer "Document this API" < api.py
# Data analysisrawi ask --act data-scientist "Analyze this CSV" < data.csv
Writing Files
Section titled “Writing Files”# Generate documentationrawi ask "Create README for this project" > README.md
# Append to filesrawi ask "Add error handling examples" >> troubleshooting.md
# Create multiple filesrawi ask "Generate package.json for Express app" > package.jsonrawi ask "Create basic Express server" > server.js
File Processing Loops
Section titled “File Processing Loops”# Process multiple filesfor file in src/*.js; do echo "=== Analyzing $file ===" cat "$file" | rawi ask --act code-reviewer "Review this file"done
# Batch documentationfor component in components/*.vue; do cat "$component" | rawi ask --act technical-writer "Document this Vue component" > "docs/$(basename "$component" .vue).md"done
Advanced Shell Integration
Section titled “Advanced Shell Integration”Error Handling
Section titled “Error Handling”# Handle API failures gracefullycat code.py | rawi ask "Review this code" || echo "AI service unavailable"
# Retry on failurefor i in {1..3}; do if rawi ask "Test question" >/dev/null 2>&1; then break fi echo "Attempt $i failed, retrying..." sleep 2done
Conditional Execution
Section titled “Conditional Execution”# Only analyze if file exists[[ -f "app.log" ]] && cat app.log | rawi ask "Find critical errors"
# Different actions based on file typeif [[ "$file" == *.py ]]; then cat "$file" | rawi ask --act python-expert "Review this Python code"elif [[ "$file" == *.js ]]; then cat "$file" | rawi ask --act javascript-expert "Review this JavaScript code"fi
Background Processing
Section titled “Background Processing”# Analyze logs in backgroundtail -f app.log | rawi ask "Monitor for security issues" &
# Process multiple files concurrentlyfor file in *.py; do (cat "$file" | rawi ask --act code-reviewer "Review $file" > "reviews/$(basename "$file").md") &donewait # Wait for all background jobs to complete
Environment Variables
Section titled “Environment Variables”Configuration
Section titled “Configuration”# Set API keysexport OPENAI_API_KEY="sk-your-key"export ANTHROPIC_API_KEY="sk-ant-your-key"
# Default profileexport RAWI_PROFILE="work"
# Default templateexport RAWI_DEFAULT_ACT="software-engineer"
Dynamic Configuration
Section titled “Dynamic Configuration”# Profile based on directoryif [[ $PWD == *"/work/"* ]]; then export RAWI_PROFILE="work"else export RAWI_PROFILE="personal"fi
# Model based on task complexityif [[ "$1" == *"complex"* ]]; then export RAWI_MODEL="gpt-4"else export RAWI_MODEL="gpt-3.5-turbo"fi
Automation Scripts
Section titled “Automation Scripts”Development Workflow Script
Section titled “Development Workflow Script”#!/bin/bash# dev-helper.sh - Development workflow automation
case "$1" in "review") echo "🔍 Starting code review..." for file in $(git diff --name-only HEAD~1); do if [[ "$file" =~ \.(js|py|ts|jsx|tsx)$ ]]; then echo "Reviewing $file..." cat "$file" | rawi ask --act code-reviewer "Review this file for issues and improvements" > "reviews/$(basename "$file").md" fi done ;;
"commit") echo "📝 Generating commit message..." git diff --cached | rawi ask "Generate a concise commit message for these changes" | head -1 ;;
"docs") echo "📚 Generating documentation..." find src -name "*.js" | while read -r file; do cat "$file" | rawi ask --act technical-writer "Generate JSDoc comments for this file" > "temp_docs" # Merge with original file done ;;
*) echo "Usage: $0 {review|commit|docs}" exit 1 ;;esac
Log Analysis Script
Section titled “Log Analysis Script”#!/bin/bash# log-analyzer.sh - Intelligent log analysis
LOG_FILE="${1:-/var/log/app.log}"OUTPUT_DIR="log-analysis/$(date +%Y%m%d)"
mkdir -p "$OUTPUT_DIR"
# Real-time error monitoringecho "🔍 Starting log analysis for $LOG_FILE"
# Analyze recent errorstail -1000 "$LOG_FILE" | grep ERROR | rawi ask --act system-administrator "Categorize and prioritize these errors" > "$OUTPUT_DIR/error-summary.txt"
# Monitor for new issuestail -f "$LOG_FILE" | while read -r line; do if echo "$line" | grep -q "CRITICAL\|FATAL"; then echo "$line" | rawi ask --act incident-responder "Assess this critical error and suggest immediate actions" | tee -a "$OUTPUT_DIR/critical-alerts.txt" fidone
API Testing Script
Section titled “API Testing Script”#!/bin/bash# api-tester.sh - AI-powered API testing
API_BASE="https://api.example.com"ENDPOINTS=("users" "posts" "comments")
for endpoint in "${ENDPOINTS[@]}"; do echo "Testing /$endpoint endpoint..."
# Test the endpoint response=$(curl -s "$API_BASE/$endpoint") status_code=$(curl -s -o /dev/null -w "%{http_code}" "$API_BASE/$endpoint")
# Analyze response with AI echo "$response" | rawi ask --act api-tester "Analyze this API response. Status: $status_code. Check for proper structure and potential issues." > "tests/$endpoint-analysis.txt"done
Real-World Integration Examples
Section titled “Real-World Integration Examples”Git Workflow Integration
Section titled “Git Workflow Integration”# Add to .git/hooks/pre-commit#!/bin/bashecho "🤖 AI-powered pre-commit checks..."
# Check commit message qualitycommit_msg=$(git log --format=%B -n 1 HEAD)if [[ ${#commit_msg} -lt 10 ]]; then echo "Generating better commit message..." git diff --cached | rawi ask "Suggest a clear commit message for these changes"fi
# Quick code reviewgit diff --cached --name-only | while read -r file; do if [[ "$file" =~ \.(js|py|ts)$ ]]; then git show ":$file" | rawi ask --act code-reviewer "Quick review: any obvious issues?" | grep -q "ISSUE" && echo "⚠️ Issues found in $file" fidone
CI/CD Integration
Section titled “CI/CD Integration”# In your CI pipeline- name: AI Code Review run: | # Review changed files git diff origin/main...HEAD --name-only | while read file; do if [[ "$file" =~ \.(js|ts|py)$ ]]; then cat "$file" | rawi ask --act code-reviewer "Review for production readiness" > "reviews/$file.md" fi done
# Check for any critical issues if grep -r "CRITICAL\|SECURITY" reviews/; then echo "Critical issues found, failing build" exit 1 fi
Monitoring and Alerting
Section titled “Monitoring and Alerting”# System monitoring script#!/bin/bashwhile true; do # Check system resources df -h | rawi ask --act system-administrator "Any disk space concerns?" | grep -q "WARNING" && \ echo "Disk space warning detected" | mail -s "System Alert" admin@company.com
# Check application health curl -s http://localhost:3000/health | rawi ask --act devops-engineer "Assess application health" | \ tee /tmp/health-check.log
sleep 300 # Check every 5 minutesdone
Performance Optimization
Section titled “Performance Optimization”Efficient Piping
Section titled “Efficient Piping”# Use head/tail to limit input sizetail -100 large-file.log | rawi ask "Analyze recent entries"
# Compress before sendinggzip -c large-file.txt | base64 | rawi ask "Analyze this compressed data"
# Filter before analysisgrep ERROR app.log | rawi ask "Categorize these errors"
Caching Responses
Section titled “Caching Responses”# Cache common analysescache_key=$(echo "$input" | md5sum | cut -d' ' -f1)cache_file="/tmp/rawi-cache/$cache_key"
if [[ -f "$cache_file" ]]; then cat "$cache_file"else echo "$input" | rawi ask "$question" | tee "$cache_file"fi
Parallel Processing
Section titled “Parallel Processing”# Process files in parallelfind . -name "*.py" | xargs -P 4 -I {} bash -c 'cat "{}" | rawi ask --act code-reviewer "Review this file" > "reviews/{}.md"'
Troubleshooting Shell Integration
Section titled “Troubleshooting Shell Integration”Common Issues
Section titled “Common Issues”Pipe broken errors:
# Add error handlingcat large-file.txt | rawi ask "Analyze this" 2>/dev/null || echo "Analysis failed"
Output formatting issues:
# Force plain text outputrawi ask --format plain "Your question"
# Clean up outputrawi ask "Your question" | sed 's/\x1b\[[0-9;]*m//g' # Remove ANSI colors
Environment variable conflicts:
# Isolate environmentenv -i PATH="$PATH" rawi ask "Your question"
Best Practices
Section titled “Best Practices”1. Input Validation
Section titled “1. Input Validation”# Validate input sizeif [[ $(wc -c < input.txt) -gt 100000 ]]; then echo "File too large, processing first 1000 lines..." head -1000 input.txt | rawi ask "Analyze this sample"else cat input.txt | rawi ask "Analyze this file"fi
2. Error Handling
Section titled “2. Error Handling”# Robust error handling{ cat problematic-file.txt | rawi ask "Analyze this"} 2>&1 | tee analysis.log || { echo "Analysis failed, using fallback method" grep -E "(ERROR|WARN)" problematic-file.txt}
3. Resource Management
Section titled “3. Resource Management”# Limit concurrent processesmax_jobs=3job_count=0
for file in *.txt; do if (( job_count >= max_jobs )); then wait # Wait for a job to complete job_count=0 fi
(cat "$file" | rawi ask "Process this file" > "output/$file") & ((job_count++))donewait # Wait for all remaining jobs
See Also
Section titled “See Also”- ask command — Basic AI interactions
- Session Management — Maintaining context in scripts
- Profile Management — Using different configurations
- Workflows — Complete automation examples