#!/bin/bash
# Function to display usage instructions
usage() {
echo "Usage: $0 -e|-d <file>"
echo " -e Encrypt the file using Caesar cipher"
echo " -d Decrypt the file using Caesar cipher"
exit 1
}
# Caesar cipher function
caesar_cipher() {
local input_file=$1
local output_file=$2
local shift=$3
# Perform Caesar cipher transformation
tr 'A-Za-z' "$(echo {A..Z} {a..z} | sed "s/.\{26\}/&\n/" | tail -n 1 | tr -d ' ')" \
< "$input_file" > "$output_file"
}
# Check if sufficient arguments are provided
if [[ $# -ne 2 ]]; then
usage
fi
# Parse command-line arguments
operation=""
file=""
while getopts "ed" opt; do
case $opt in
e) operation="encrypt"; shift=3 ;;
d) operation="decrypt"; shift=-3 ;;
*) usage ;;
esac
done
file=${@:$OPTIND:1}
# Ensure the file exists
if [[ ! -f $file ]]; then
echo "Error: File '$file' not found!"
exit 1
fi
# Generate the output file name
output_file="${file%.*}_$( [ "$operation" == "encrypt" ] && echo "encrypted" || echo "decrypted").${file##*.}"
# Perform the operation
caesar_cipher "$file" "$output_file" "$shift"
# Notify the user of the result
echo "$operation completed. Output file: $output_file"