Skip to content

Error Codes

Mikhail Deynekin edited this page Apr 11, 2026 · 1 revision

📊 Error Codes

Complete reference for all exit codes returned by sr.

Exit Code Reference

Code Meaning Typical Scenario
0 Success Replacements completed successfully
1 Invalid arguments Missing or incorrect parameters
2 No files found Pattern didn't match any files
3 No replacements Search string not found in any files
4 Runtime error Permission issues, disk full, etc.
5 Backup failed Couldn't create backup files
6 Binary file detected Binary file encountered without --binary flag
7 Rollback failed Restoration encountered errors

Detailed Descriptions

Exit Code 0 - Success

The operation completed successfully. All found matches were replaced and backups were created.

sr "*.txt" "hello" "world"
echo $?  # Returns 0

Exit Code 1 - Invalid Arguments

The command was invoked with incorrect arguments.

Common causes:

  • Missing required arguments (pattern, search, replace)
  • Invalid option flags
  • Conflicting options
# Check usage
sr --help

# Correct syntax
sr [OPTIONS] "FILE_PATTERN" "SEARCH" "REPLACE"

Exit Code 2 - No Files Found

The file pattern didn't match any files.

Common causes:

  • Pattern not quoted (shell expanded it)
  • Wrong directory
  • No files match the pattern
# Debug: check what files exist
find . -name "*.txt" -type f | head

# Fix: quote the pattern
sr "*.txt" "search" "replace"

Exit Code 3 - No Replacements Made

Files were found but the search string wasn't found in any of them.

Common causes:

  • Search string doesn't match (case sensitivity)
  • Pattern matches wrong files
  • String already replaced
# Debug: search manually
grep -r "search_string" .

# Fix: use case-insensitive
sr -i "*.txt" "search" "replace"

Exit Code 4 - Runtime Error

An error occurred during execution.

Common causes:

  • Permission denied on files
  • Disk full
  • Missing required utilities
# Check disk space
df -h .

# Check file permissions
ls -la *.txt

# Check required utilities
for cmd in bash find sed grep; do command -v $cmd; done

Exit Code 5 - Backup Failed

The backup system failed to create backups before modification.

Common causes:

  • No write permission in current directory
  • Disk full
  • Target directory doesn't exist
# Check write permissions
ls -la .

# Use no-backup as workaround (RISKY!)
sr -nb "*.txt" "search" "replace"

Exit Code 6 - Binary File Detected

sr detected a binary file and refused to process it without explicit permission.

# Check if file is actually binary
file suspicious-file

# Process binary file explicitly (use with caution)
sr --binary "*.bin" "search" "replace"

# Exclude binary file types
sr -xp "*.png,*.jpg,*.exe" "**/*" "search" "replace"

Exit Code 7 - Rollback Failed

The rollback operation encountered errors restoring files.

Common causes:

  • Backup directory corrupt or missing
  • Permission denied restoring files
  • Backup files modified externally
# List available backups
sr --rollback-list

# Try specific backup
sr --rollback=sr.backup.20240115_143022

# Manual restore from backup directory
cp sr.backup.20240115_143022/files/path/to/file.txt ./path/to/file.txt

Using Exit Codes in Scripts

#!/bin/bash
# Example: Use exit codes for error handling

sr --dry-run "*.conf" "old-server" "new-server"
DRY_RUN_EXIT=$?

case $DRY_RUN_EXIT in
  0) echo "Files found and would be modified"; ;;
  2) echo "No matching files found"; exit 1; ;;
  3) echo "Search string not found"; exit 0; ;;
  *) echo "Unexpected error: $DRY_RUN_EXIT"; exit 1; ;;
esac

# If dry-run looks good, apply changes
sr "*.conf" "old-server" "new-server"
if [ $? -eq 0 ]; then
  echo "Success!"
else
  echo "Failed, rolling back..."
  sr --rollback
fi

Related Topics

Clone this wiki locally