-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.sh_functions
203 lines (175 loc) · 5.88 KB
/
.sh_functions
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# Create a new directory and enter it
function mkd() {
mkdir -p "$@" && cd "$@"
}
# Determine size of a file or total size of a directory
function fs() {
if du -b /dev/null > /dev/null 2>&1; then
local arg=-sbh
else
local arg=-sh
fi
if [[ -n "$@" ]]; then
du $arg -- "$@"
else
du $arg .[^.]* *
fi
}
# Use Git’s colored diff when available
hash git &>/dev/null
if [ $? -eq 0 ]; then
function diff() {
git diff --no-index --color-words "$@"
}
fi
# CD to a directory and list it.
function cdl() {
cd $1 && ls -lah
}
# Create a data URL from a file
function dataurl() {
local mimeType=$(file -b --mime-type "$1")
if [[ $mimeType == text/* ]]; then
mimeType="${mimeType};charset=utf-8"
fi
echo "data:${mimeType};base64,$(openssl base64 -in "$1" | tr -d '\n')"
}
# Start an HTTP server from a directory, optionally specifying the port
function server() {
local port="${1:-8000}"
sleep 1 && open "http://localhost:${port}/" &
# Set the default Content-Type to `text/plain` instead of `application/octet-stream`
# And serve everything as UTF-8 (although not technically correct, this doesn’t break anything for binary files)
python -c $'import SimpleHTTPServer;\nmap = SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map;\nmap[""] = "text/plain";\nfor key, value in map.items():\n\tmap[key] = value + ";charset=UTF-8";\nSimpleHTTPServer.test();' "$port"
}
# Start a PHP server from a directory, optionally specifying the port
# (Requires PHP 5.4.0+.)
function phpserver() {
local port="${1:-4000}"
local ip=$(ipconfig getifaddr en1)
sleep 1 && open "http://${ip}:${port}/" &
php -S "${ip}:${port}"
}
# Get gzipped file size
function gz() {
echo "orig size (bytes): "
cat "$1" | wc -c
echo "gzipped size (bytes): "
gzip -c "$1" | wc -c
}
# Test if HTTP compression (RFC 2616 + SDCH) is enabled for a given URL.
# Send a fake UA string for sites that sniff it instead of using the Accept-Encoding header. (Looking at you, ajax.googleapis.com!)
function httpcompression() {
encoding="$(curl -LIs -H 'User-Agent: Mozilla/5 Gecko' -H 'Accept-Encoding: gzip,deflate,compress,sdch' "$1" | grep '^Content-Encoding:')" && echo "$1 is encoded using ${encoding#* }" || echo "$1 is not using any encoding"
}
# Syntax-highlight JSON strings or files
# Usage: `json '{"foo":42}'` or `echo '{"foo":42}' | json`
function json() {
if [ -t 0 ]; then # argument
python -mjson.tool <<< "$*" | pygmentize -l javascript
else # pipe
python -mjson.tool | pygmentize -l javascript
fi
}
# All the dig info
function digga() {
dig +nocmd "$1" any +multiline +noall +answer
}
# Escape UTF-8 characters into their 3-byte format
function escape() {
printf "\\\x%s" $(printf "$@" | xxd -p -c1 -u)
echo # newline
}
# Decode \x{ABCD}-style Unicode escape sequences
function unidecode() {
perl -e "binmode(STDOUT, ':utf8'); print \"$@\""
echo # newline
}
# Get a character’s Unicode code point
function codepoint() {
perl -e "use utf8; print sprintf('U+%04X', ord(\"$@\"))"
echo # newline
}
# Manually remove a downloaded app or file from the quarantine
function unquarantine() {
for attribute in com.apple.metadata:kMDItemDownloadedDate com.apple.metadata:kMDItemWhereFroms com.apple.quarantine; do
xattr -r -d "$attribute" "$@"
done
}
# Usage: mount_ssh [your_username@]some.server.com:/path/to/mount "[Your Server Name]"
function mount_ssh() {
# Ensure SSHFS is installed before trying to run any commands.
if ! type sshfs >/dev/null 2>&1; then
echo "SSHFS is required to run this command."
echo "If you have Homebrew installed, please run \"brew install sshfs\""
return
fi
local connectionPattern="((.*)@)?(.*)(:(.*))?"
local remoteHost
local friendlyName
if [[ -n "${1}" && ${1} =~ ${connectionPattern} ]]; then
remoteHost=${BASH_REMATCH[3]}
friendlyName=`expr "${remoteHost}" : '\([0-9A-Za-z_-]\{1,30\}\)'`
else
echo "A host name is required"
return
fi
# Auto requesting sudo access as it is required for the creation and alteration of the Network folder.
sudo -v
local directoryName=/Network/${friendlyName}
local volumeName="${friendlyName} (SSHFS)"
if [[ -n "${2}" ]]; then
local volumeName=${2}
fi
if [ -d ${directoryName} ]; then
read -p "\"${directoryName}\" already exists. Would you like to remount it? [y/N]: " yn
case $yn in
[Yy]* )
cd
sudo umount -f ${directoryName};;
* )
echo "Exiting...";
return;;
esac
fi
# An ugly chain of dependant commands. If the command previous to each fails, something has gone
# wrong and we need to stop.
sudo mkdir -p ${directoryName} &&
sudo chown ${USER} ${directoryName} &&
sudo chmod 0755 ${directoryName} &&
sshfs ${1} ${directoryName} -o follow_symlinks,compression=yes,volname="${volumeName}" &&
cd ${directoryName} &&
echo "${remoteHost} mounted to ${directoryName}, named \"${volumeName}\"" && return
echo "${remoteHost} failed to mount."
}
# Search file(s) for keywords.
function grok() {
# Display usage if no parameters given
if [[ -z "$@" ]]; then
echo -e " ${0##*/} <string> <file/path> - search recursively for keyword in files"
exit
fi
if [ -z "$2" ]; then
loc=$(pwd)
else
loc="$2"
fi
echo " Searching..."
grep -ilr "$@" "$loc" 2>&1 | grep -v "No such" | grep -v "Permission denied" | sed "s:^\.::g" | sed "s:$(pwd)::g" | sort | uniq
}
# Kill any program that's running on a specified port
function killport() {
if [ -z "$1" ]; then
echo "Usage: killport <port>"
return 1
fi
PORT=$1
PIDS=$(lsof -ti :$PORT)
if [ -z "$PIDS" ]; then
echo "No process is using port $PORT"
else
echo "Killing process(es) $PIDS on port $PORT"
# Safely handle multiple PIDs by splitting on spaces or newlines
echo "$PIDS" | xargs kill -9
fi
}