-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path.bash_functions.Darwin
144 lines (142 loc) · 4.11 KB
/
.bash_functions.Darwin
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
# vim: ft=bash noet:
function applescript2app {
for file
do
osacompile -o "/Applications/${file/.applescript/.app}" "$file"
done
}
function dmgInstall {
local sudo defaultDst
local defaultDst=not_yet_defined
[ $isAdmin = true ] && defaultDst=/ || defaultDst="$HOME"
test "$debug" -gt 2 && set -x
for dmgFilePath
do
if \ls $dmgFilePath >/dev/null
then
mountDev=$(hdiutil mount $dmgFilePath | awk '/dev.disk.*Volumes/{print$1}')
mountDir="$(command mount | \grep $mountDev | awk '{$1=$2="";sub(" [(].*","");sub("^ ","");print}')"
test "$debug" = 1 && echo "=> mountDir = $mountDir"
if [ -d "$mountDir" ]
then
if \ls "${mountDir}/"*.pkg >/dev/null
then
$sudo installer -verboseR -pkg "${mountDir}/"*.pkg -target $defaultDst
else
$sudo \cp -pvr "${mountDir}/"*.app /Applications/
fi
sync
hdiutil unmount $mountDir
fi
fi
done
unset dmgFilePath
set +x
}
function lanIP {
ethName=$1
if [ -n "$ethName" ]
then
# printf "$ethName: "
\ip -4 addr show dev $ethName | awk '/inet /{print$2}'
else
\ip -4 addr show | awk '{if(/(UP|UNKNOWN)/){interface=$2;found=1}else if(/DOWN/)found=0;if(found==1 && /inet /)print interface" "$2}'
fi | column -t
}
function locate {
local locate="$(type -P glocate)"
groups 2>/dev/null | \egrep -wq "sudo|admin" && locateOptions="-e" || locateOptions="--database $HOME/.local/lib/mlocate/mlocate.db -e"
echo "$@" | \grep -q "\-[a-z]*r" && time $locate $locateOptions "$@" || time $locate $locateOptions -ir "${@}"
}
function mac@ {
ethName=$1
if [ -n "$ethName" ]
then
printf "$ethName: "
\ip addr show dev $ethName | awk '/ether/{print$2}'
else
\ip addr show | awk '{if(/(UP|UNKNOWN)/){interface=$1;found=1}else if(/DOWN/)found=0;if(found==1 && /ether/)print interface" "$2}'
fi
}
function mount {
local arg=$1
if [ $# = 0 ];then
command mount -v
else
arg=$(echo $arg | sed "s|/dev/||")
test -n "$arg" && diskutil list | grep -q $arg && [ -b /dev/$arg ] && diskutil mount $arg
fi
}
function netstat {
local mac_netstat_options="AaLnWgisf:p:I:w:c:mr"
local OPTSTRING="ltu$mac_netstat_options"
local args=("$@")
local option
local wasInWhile=false
while getopts $OPTSTRING option; do
wasInWhile=true
# echo $option | \egrep -q "l|t|u" && args=("$(echo "${args[@]}" | sed -E "s/$option|-$option | -$option//g")")
echo $option | \egrep -q "l|t|u" && args=( "${args[@]/$option\|-$option \| -$option/}" )
case "$option" in
l) args=("${args[@]}" -a) ;; #Show listening ports
t) args=("${args[@]}" -p tcp) ;;
u) args=("${args[@]}" -p udp) ;;
"") echo "=> ERROR : the <option> variable is empty.">&2; return 1;;
'?') echo "=> ERROR : the <option> variable is <$?>." >&2; return 2;;
esac
done
if $wasInWhile
then
command netstat "${args[@]}"
else
echo "=> ERROR : There was a problem with the <getopts> builtin." >&2
return 1;
fi
}
function pkgExpand {
for pkg
do
pkgutil --expand "$pkg" tmp/
done
}
function pkgInstall {
local defaultDst=not_yet_defined
[ $isAdmin = true ] && defaultDst=/ || defaultDst="$HOME"
test "$debug" -gt 2 && set -x
for pkg
do
$sudo installer -verboseR -pkg "$pkg" -target $defaultDst
done
set +x
}
function pkgList {
for pkg
do
lsbom $(pkgutil --bom "$pkg")
done
}
function restart {
test $1 && { stop $1; start $1; }
}
function route {
if echo $@ | \egrep -wq "add|flush|delete|change|get|monitor" ;then
command route $@
else
command netstat -f inet -r $@ | \egrep -v /32 | \egrep "default|0.0.0.0|S"
fi
}
function start {
local serviceName=$1
test $serviceName && sudo launchctl load $macOSServiceDIR/$serviceName.plist && status $serviceName
}
function status {
local serviceName="$1"
test $serviceName && sudo launchctl list | \egrep "$serviceName"
}
function stop {
local serviceName=$1
test $serviceName && sudo launchctl unload $macOSServiceDIR/$serviceName.plist && status $serviceName
}
function topflines {
command $find "$@" $findPrunePathsExpression -xdev -type f -size +10M -exec gls -l --block-size=M --time-style=+"%Y-%m-%d %T" {} \; 2>/dev/null | sort -nrk5 | column -t | head -n $(($LINES-4)) | awk '{total+=$5;print}END{if(total) print"=> total = "total" MiB"}'
}