-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask5.sh
executable file
·120 lines (106 loc) · 2.72 KB
/
task5.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/bin/bash
# Initialize variables
input_file=""
output_file=""
substitute_a=""
substitute_b=""
reverse_flag=false
lowercase_flag=false
uppercase_flag=false
swapcase_flag=false
# Parse input arguments
while [[ $# -gt 0 ]]; do
case "$1" in
-v)
swapcase_flag=true
shift
;;
-s)
if [[ -z "$2" || -z "$3" ]]; then
echo "Error: You must provide both <A_WORD> and <B_WORD> for substitution."
exit 1
fi
substitute_a="$2"
substitute_b="$3"
shift 3
;;
-r)
reverse_flag=true
shift
;;
-l)
lowercase_flag=true
shift
;;
-u)
uppercase_flag=true
shift
;;
-i)
input_file="$2"
shift 2
;;
-o)
output_file="$2"
shift 2
;;
*)
echo "Invalid option: $1"
echo "Usage: ./task5.sh [-v] [-s A_WORD B_WORD] [-r] [-l] [-u] [-i input_file] [-o output_file]"
exit 1
;;
esac
done
# Check for conflicting flags: Cannot use both -l (lowercase) and -u (uppercase) together
if [[ "$lowercase_flag" == true && "$uppercase_flag" == true ]]; then
echo "Error: Cannot use both -l (lowercase) and -u (uppercase) together."
exit 1
fi
# Ensure input file is valid, if provided
if [[ -z "$input_file" ]]; then
echo "Error: Input file is required."
exit 1
fi
if [[ ! -f "$input_file" ]]; then
echo "Error: Input file does not exist."
exit 1
fi
# Read input from the file
text=$(cat "$input_file")
# Check for empty input
if [[ -z "$text" ]]; then
echo "Error: No input provided in the file."
exit 1
fi
# Perform substitution if valid
if [[ ! -z "$substitute_a" && ! -z "$substitute_b" ]]; then
if [[ "$text" == *"$substitute_a"* ]]; then
text=$(echo "$text" | sed "s/$substitute_a/$substitute_b/g")
else
echo "Warning: '$substitute_a' not found in the text."
fi
fi
# Reverse lines
if [[ "$reverse_flag" == true ]]; then
text=$(echo "$text" | awk '{print NR, $0}' | sort -r | cut -d' ' -f2-)
fi
# Swap case
if [[ "$swapcase_flag" == true ]]; then
text=$(echo "$text" | tr 'a-zA-Z' 'A-Za-z')
fi
# Convert to lowercase
if [[ "$lowercase_flag" == true ]]; then
text=$(echo "$text" | tr 'A-Z' 'a-z')
fi
# Convert to uppercase
if [[ "$uppercase_flag" == true ]]; then
text=$(echo "$text" | tr 'a-z' 'A-Z')
fi
# Output to file or terminal
if [[ -n "$output_file" ]]; then
echo "$text" > "$output_file"
echo "Processed text written to $output_file."
else
echo "Processed text:"
echo "$text"
fi