BASH Post Services

Viewing: 1751249544_faxgram.sh

Try wget https://bash.commongrounds.cc/uploads/1751249544_faxgram.sh from the console

Raw File Link

#!/bin/bash

# Prompt for text file if not provided
if [[ -z "$1" ]]; then
    read -p "Enter the text file to fax (e.g., pr1.txt): " txt_file
    if [[ -z "$txt_file" || ! -f "$txt_file" ]]; then
        echo "Error: Text file not specified or not found"
        exit 1
    fi
else
    txt_file="$1"
    if [[ ! -f "$txt_file" ]]; then
        echo "Error: Text file $txt_file not found"
        exit 1
    fi
fi

# Prompt for CSV file if not provided
if [[ -z "$2" ]]; then
    read -p "Enter the CSV file with fax numbers (e.g., cpress.csv): " csv_file
    if [[ -z "$csv_file" || ! -f "$csv_file" ]]; then
        echo "Error: CSV file not specified or not found"
        exit 1
    fi
else
    csv_file="$2"
    if [[ ! -f "$csv_file" ]]; then
        echo "Error: CSV file $csv_file not found"
        exit 1
    fi
fi

# Check if efax is installed
if ! command -v efax &> /dev/null; then
    echo "Error: efax not installed"
    exit 1
fi

# Array to store busy fax numbers
declare -a busy_numbers

# Function to validate phone number (basic check for digits, +, -, spaces, and parentheses)
is_valid_phone() {
    local number="$1"
    # Remove non-digit characters for validation
    local cleaned=$(echo "$number" | tr -d '[:space:]+-()')
    if [[ $cleaned =~ ^[0-9]{7,15}$ ]]; then
        return 0
    else
        return 1
    fi
}

# Function to send fax
send_fax() {
    local number="$1"
    local file="$2"
    local attempt="$3"
    echo "Attempting to send fax to $number (Attempt $attempt)..."

    # Run efax command
    efax -d /dev/ttyS1 -t "T$number" "$file" > efax.log 2>&1
    local status=$?

    # Check efax exit status
    case $status in
        0)
            echo "Fax sent successfully to $number"
            return 0
            ;;
        1)
            echo "Fax to $number failed: busy or device in use"
            return 1
            ;;
        *)
            echo "Fax to $number failed with error code $status. Check efax.log"
            return 2
            ;;
    esac
}

# Main processing loop
process_fax_numbers() {
    local attempt="$1"
    local fax_numbers=("${@:2}")
    local temp_busy=()

    for number in "${fax_numbers[@]}"; do
        if is_valid_phone "$number"; then
            if send_fax "$number" "$txt_file" "$attempt"; then
                # Success, continue to next number
                continue
            else
                # Check if failure was due to busy signal
                if grep -q "busy" efax.log; then
                    temp_busy+=("$number")
                fi
            fi
        else
            echo "Skipping invalid phone number: $number"
        fi
    done

    # Update busy_numbers array
    busy_numbers=("${temp_busy[@]}")
}

# Read fax numbers from column 4 of csv_file
mapfile -t fax_numbers < <(awk -F',' '{print $4}' "$csv_file" | tail -n +2 | tr -d '[:space:]"')

# First attempt
echo "Starting first fax attempt..."
process_fax_numbers 1 "${fax_numbers[@]}"

# Retry busy numbers
if [[ ${#busy_numbers[@]} -gt 0 ]]; then
    echo "Retrying ${#busy_numbers[@]} busy numbers..."
    process_fax_numbers 2 "${busy_numbers[@]}"
else
    echo "No busy numbers to retry."
fi

# Final status
if [[ ${#busy_numbers[@]} -gt 0 ]]; then
    echo "The following numbers still failed after retry:"
    printf '%s\n' "${busy_numbers[@]}"
else
    echo "All faxes either sent successfully or failed for non-busy reasons."
fi

# Clean up
rm -f efax.log
exit 0
BASH to Home