-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash_functions
190 lines (167 loc) · 5.02 KB
/
bash_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
#!/usr/bin/env bash
function set-jdk() {
# setjdk from http://www.jayway.com/2014/01/15/how-to-switch-jdk-version-on-mac-os-x-maverick/
if [ $# -ne 0 ]; then
remove-from-path '/System/Library/Frameworks/JavaVM.framework/Home/bin'
if [ -n "${JAVA_HOME+x}" ]; then
remove-from-path $JAVA_HOME
fi
export JAVA_HOME=`/usr/libexec/java_home -v $@`
export PATH=$JAVA_HOME/bin:$PATH
fi
}
function remove-from-path() {
export PATH=$(echo $PATH | sed -E -e "s;:$1;;" -e "s;$1:?;;")
}
function link-gradle() {
# Wrapper script
ln -s $HOME/code/stash/NEBULA/wrapper/gradlew gradlew
# Directory
ln -s $HOME/code/stash/NEBULA/wrapper/gradle gradle
}
function cd-to-finder() {
target=`osascript -e 'tell application "Finder" to if (count of Finder windows) > 0 then get POSIX path of (target of front Finder window as text)'`
if [ "$target" != "" ]; then
cd "$target"; pwd
else
echo 'No Finder window found' >&2
fi
}
function pickfrom() {
# picks random line from file
# pickfrom /usr/share/dict/words'
local file=$1
[ -z "$file" ] && reference $FUNCNAME && return
length=$(cat $file | wc -l)
n=$(expr $RANDOM \* $length \/ 32768 + 1)
head -n $n $file | tail -1
}
function pass() {
# generates random password from dictionary words
# pass 4 (default)
local i pass length=${1:-4}
pass=$(echo $(for i in $(eval echo "{1..$length}"); do pickfrom /usr/share/dict/words; done))
echo "With spaces (easier to memorize): $pass"
echo "Without (use this as the pass): $(echo $pass | tr -d ' ')"
}
function mkcd() {
# make a directory and cd into it
# mkcd /tmp/some_directory
mkdir -p "$*"
cd "$*"
}
function usage() {
# disk usage per directory
# usage (~/tmp)
if [ $(uname) = "Darwin" ]; then
if [ -n $1 ]; then
du -hd $1
else
du -hd 1
fi
elif [ $(uname) = "Linux" ]; then
if [ -n $1 ]; then
du -h --max-depth=1 $1
else
du -h --max-depth=1
fi
fi
}
function command_exists() {
# checks for existence of a command
# command_exists ls && echo "yay!"
type "$1" &> /dev/null ;
}
function extract() {
if [ $# -ne 1 ]
then
echo "Error: No file specified."
return 1
fi
if [ -f $1 ] ; then
case $1 in
*.7z) 7z x $1 ;;
*.Z) uncompress $1 ;;
*.bz2) bunzip2 $1 ;;
*.deb) ar -x $1 ;;
*.gz) gunzip $1 ;;
*.rar) unrar x $1 ;;
*.tar) tar xvf $1 ;;
*.tar.bz2) tar xvjf $1 ;;
*.tar.gz) tar xvzf $1 ;;
*.tbz2) tar xvjf $1 ;;
*.tgz) tar xvzf $1 ;;
*.zip) unzip $1 ;;
*.war) unzip $1 ;;
*.jar) unzip $1 ;;
*) echo "'$1' cannot be extracted via extract" ;;
esac
else
echo "'$1' is not a valid file"
fi
}
function extract-and-remove() {
if [ $# -ne 1 ]
then
echo "Error: No file specified."
return 1
fi
if [ -f $1 ] ; then
extract $1 && rm $1
else
echo "'$1' is not a valid file"
fi
}
function local-ignore() {
# adds file or path to git exclude file
# local-ignore private.txt
echo "$1" >> .git/info/exclude
}
function mainfest() {
# extracts the specified JAR/WAR file's MANIFEST file and prints it to stdout
# manifest ~/stupid.jar
unzip -c $1 META-INF/MANIFEST.MF
}
function tabname() {
# renames the current terminal tab title
printf "\e]1;$1\a"
}
function winname() {
# renames the current terminal window title
printf "\e]2;$1\a"
}
function pyedit() {
# opens python module
# pyedit kragle
xpyc=`python -c "import sys; stdout = sys.stdout; sys.stdout = sys.stderr; import $1; stdout.write($1.__file__)"`
if [ "$xpyc" == "" ]; then
echo "Python module $1 not found"
return -1
elif [[ $xpyc == *__init__.py* ]]; then
xpydir=`dirname $xpyc`;
echo "$EDITOR $xpydir";
$EDITOR "$xpydir";
else
echo "$EDITOR ${xpyc%.*}.py";
$EDITOR "${xpyc%.*}.py";
fi
}
function start-hadoop-minicluster() {
# All ports from http://docs.hortonworks.com/HDPDocuments/HDP2/HDP-2.1.5/bk_reference/content/reference_chap2.html
export HADOOP_RESOURCE_MANAGER_PORT=8025
export HADOOP_JOBTRACKER_PORT=10020
export HADOOP_NAMENODE_PORT=9000
HADOOP_VERSION=2.4.1
local HADOOP_DIR=~/Applications/hadoop-$HADOOP_VERSION
if [ -d $HADOOP_DIR ]; then
local HADOOP_MINICLUSTER_JAR=$(find $HADOOP_DIR -name "hadoop-mapreduce-client-jobclient-*-tests.jar")
local HADOOP_TEST_CLASSPATH=$(find $HADOOP_DIR -name "*-tests.jar" | grep -v $HADOOP_MINICLUSTER_JAR | paste -sd ":" -)
if [ -f $HADOOP_MINICLUSTER_JAR ]; then
HADOOP_CLASSPATH=$HADOOP_TEST_CLASSPATH $HADOOP_DIR/bin/hadoop jar $HADOOP_MINICLUSTER_JAR minicluster -rmport $HADOOP_RESOURCE_MANAGER_PORT -jhsport $HADOOP_JOBTRACKER_PORT -nnport $HADOOP_NAMENODE_PORT
else
echo "Unable to find minicluster jar for hadoop version $HADOOP_VERSION"
fi
else
echo "Unable to find hadoop directory for hadoop version $HADOOP_VERSION"
fi
}