-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgxpr
More file actions
executable file
·64 lines (59 loc) · 1.59 KB
/
gxpr
File metadata and controls
executable file
·64 lines (59 loc) · 1.59 KB
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
#!/bin/sh
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2014 Jeremy Brubaker <jbru362@gmail.com>
#
# abstract: like expr(1), but uses Google's calculator to evaluate <expression>
#
# Usage: gxpr <expression>
#
# 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'
SEARCH="https://www.google.com/search"
EXPR=$(echo "$@" | sed -e 's/+/%2B/g' -e 's/ /+/g')
res=$(
$CURL "$SEARCH?q=$EXPR" |
perl -ne '/<h2 class="r".*?>(.*)<\/h2>/ and print $1' |
perl -ne '/= (.*)/ and print $1' |
perl -pe 's/[^\x00-\x7F]//g'
)
# if we don't have a result, assume it's an invalid expression
test -z "$res" && {
echo "invalid expression:" "$@" 1>&2
exit 1
}
echo "$res"