-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.1.sh
executable file
·59 lines (47 loc) · 1.15 KB
/
2.1.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
#!/bin/bash
args=2
ebadargs=85
ebaddet=86
ebaddir=87
eemtra=88
apikey='trnsl.1.1.20141225T224635Z.7e7e6ca735fb92eb.ccdf830154aa5db64fbaf77e6cdfd422c731550f'
if [ $# -ne $args ]
then
echo "`basename $0` [word] [target-lang|source-target pair]"
exit $ebadargs
fi
word=$1
if ! echo $2 | fgrep "-" >/dev/null
then
apidet="https://translate.yandex.net/api/v1.5/tr.json/detect?key=$apikey&text=$word"
detres=`curl -s $apidet`
detres=`echo $detres | awk -F'"' '{print $6}'`
if [ -z "$detres" ]
then
echo "api could not detect language"
exit $ebaddet
else
dir="$detres-$2"
fi
else
dir=$2
fi
if [ -z "$dir" ]
then
echo "could not construct direction param"
exit $ebaddir
fi
apitra="https://translate.yandex.net/api/v1.5/tr.json/translate?key=$apikey&lang=$dir&text=$word"
detres=`curl -s $apitra`
if ! echo $detres | fgrep "200" > /dev/null # response is not 200, error happened
then
echo "could not translate provided word to provided language"
exit $ebadtra
fi
tra=`echo $detres | awk -F'[' '{print $2}' | awk -F']' '{print $1}' | awk -F'"' '{print $2}'`
if [ -z "$tra" ]
then
echo "translation is empty"
exit $eemtra
fi
echo $tra