-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask5.sh
80 lines (65 loc) · 1.58 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
#Write script with following functionality:
#If -v tag is passed, replaces lowercase characters with uppercase and vise versa
#If -s is passed, script substitutes <A_WORD> with <B_WORD> in text (case sensitive)
#If -r is passed, script reverses text lines
#If -l is passed, script converts all the text to lower case
#If -u is passed, script converts all the text to upper case
#Script should work with -i <input file> -o <output file> tags
#!/bin/bash
input_file=""
output_file=""
toggle_case=false
lower_case=false
upper_case=false
reverse_case=false
substitute_case=false
a_word=""
b_word=""
while getopts ":i:o:vlurs:" opt; do
case $opt in
i)
input_file="$OPTARG"
;;
o)
output_file="$OPTARG"
;;
v)
toggle_case=true
;;
l)
lower_case=true
;;
u)
upper_case=true
;;
r)
reverse_case=true
;;
s)
substitute_case=true
IFS=":" read -r a_word b_word <<< "$OPTARG"
;;
esac
done
if $toggle_case; then
tr '[:lower:][:upper:]' '[:upper:][:lower:]' < "$input_file" > "$output_file"
else
cp "$input_file" "$output_file"
fi
if $lower_case; then
tr '[:upper:]' '[:lower:]' < "$input_file" > "$output_file"
else
cp "$input_file" "$output_file"
fi
if $upper_case; then
tr '[:lower:]' '[:upper:]' < "$input_file" > "$output_file"
else
cp "$input_file" "$output_file"
fi
if $reverse_case; then
tac -s ' ' "$input_file" > "$output_file"
fi
if $substitute_case; then
placeholder="__PLACEHOLDER__"
sed -e "s/$a_word/$placeholder/g" -e "s/$b_word/$a_word/g" -e "s/$placeholder/$b_word/g" "$input_file" > "$output_file"
fi