-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask4.sh
executable file
·88 lines (73 loc) · 2.14 KB
/
task4.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
# Function to encrypt a letter by shifting it
shift_char() {
local char="$1"
local shift="$2"
# Adjust the shift for large values
shift=$(( shift % 26 ))
# Check if the character is a lowercase letter
if [[ "$char" =~ [a-z] ]]; then
base=97 # ASCII value for 'a'
echo "$(( ( $(printf "%d" "'$char") - $base + $shift + 26 ) % 26 + $base ))" | awk '{printf "%c", $1}'
# Check if the character is an uppercase letter
elif [[ "$char" =~ [A-Z] ]]; then
base=65 # ASCII value for 'A'
echo "$(( ( $(printf "%d" "'$char") - $base + $shift + 26 ) % 26 + $base ))" | awk '{printf "%c", $1}'
# If not a letter, return the character as-is
else
echo "$char"
fi
}
# Parse input arguments
shift_value=""
input_file=""
output_file=""
while [[ $# -gt 0 ]]; do
case "$1" in
-s)
shift_value="$2"
shift 2
;;
-i)
input_file="$2"
shift 2
;;
-o)
output_file="$2"
shift 2
;;
*)
echo "Invalid option: $1"
exit 1
;;
esac
done
# Ensure that all required arguments are provided
if [[ -z "$shift_value" || -z "$input_file" || -z "$output_file" ]]; then
echo "Usage: $0 -s <shift> -i <input file> -o <output file>"
exit 1
fi
# Ensure the shift value is a valid number
if ! [[ "$shift_value" =~ ^-?[0-9]+$ ]]; then
echo "Invalid shift value: $shift_value. Please provide a numeric value."
exit 1
fi
# Check if the input file exists and is not empty
if [[ ! -f "$input_file" ]]; then
echo "Input file not found!"
exit 1
fi
if [[ ! -s "$input_file" ]]; then
echo "Input file is empty."
exit 1
fi
# Open output file for writing
> "$output_file"
# Process each character in the input file
while IFS= read -r -n1 char; do
# Encrypt the character
encrypted_char=$(shift_char "$char" "$shift_value")
# Append encrypted character to output file
echo -n "$encrypted_char" >> "$output_file"
done < "$input_file"
echo "Encryption complete. Encrypted text saved to $output_file."