-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrans2zh
executable file
·46 lines (38 loc) · 991 Bytes
/
trans2zh
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
#!/usr/bin/env sh
# vim: ft=sh ts=4 sw=4 sts=4 et :
# Function to read input, concatenate it, and process it with 'trans'
process_input() {
echo '----------------------------------------'
echo '•'
input=""
line_empty=""
# Concat all lines until an empty line is found
while IFS= read -r line; do
if [ -z "$line" ]; then
line_empty=1
break
fi
input="$input $line"
done
input=$(echo "$input" | sed 's/^ *//')
# If input is empty:
# 1. return 1 if the last line is not empty (received ctrl-d)
# 2. otherwise return 0
if [ -z "$input" ]; then
if [ -n "$line_empty" ]; then
return 0
else
return 1
fi
fi
output=$(echo "$input" | trans -b -no-auto :zh)
echo "◦"
echo "$output"
echo
}
# Continuously receive input from user until receiving ctrl-d (EOF)
while true; do
if ! process_input; then
break
fi
done