-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgxpr
executable file
·47 lines (43 loc) · 1023 Bytes
/
gxpr
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
#!/bin/sh
# Usage: gxpr <expression>
# Like expr(1), but uses Google's calculator to evaluate <expression>.
# When google fails, light the WolframAlpha signal
#
# Math examples:
# $ gxpr '1 + 1'
# 2
#
# $ gxpr 2 ^ 16
# 65536
#
# $ gxpr '(2 ^ 1) + (2 ^ 2) + (2 ^ 3) + (2 ^ 5)'
# 46
#
# $ gxpr '5*9+(sqrt 10)^3='
# 76.6227766
#
# Conversion examples:
# $ gxpr 1GB in KB
# 1048576 kilobytes
#
# $ gxpr 10 megabits in megabytes
# 1.25 megabytes
#
# $ gxpr 2 miles in inches
# 126720 inches
CURL='curl -s --header User-Agent:gxpr/1.0'
GOOGLE="http://www.google.com/ig/calculator"
WOLFRAM="http://www.wolframalpha.com/input/"
EXPR=$(echo "$@" | perl -MURI::Escape -ne 'chomp;print uri_escape($_)')
res=$(
$CURL "$GOOGLE?q=$EXPR" |
perl -ne '/rhs: "?([^\[\],":]+)/ and print $1' |
perl -pe 's/[^\x00-\x7F]//g'
)
# if we don't have a result, try wolfram alpha
test -z "$res" && {
echo "google doesn't know" "$@" 1>&2
echo "⌘ click: \033[4m$WOLFRAM?i=$EXPR"
exit 1
}
echo "$res"