-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_functions
More file actions
141 lines (122 loc) · 3.56 KB
/
bash_functions
File metadata and controls
141 lines (122 loc) · 3.56 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
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
# Truncation code of $PWD
function truncate_pwd {
local maxlen=30
local trunc_sym='...'
local now_pwd=${PWD/$HOME/\~} # Some pattern replace
if [ ${#now_pwd} -gt $maxlen ]; then
local offset=$(( ${#now_pwd} - $maxlen ))
echo "${trunc_sym}${now_pwd:$offset:$maxlen}"
else
echo "$now_pwd"
fi
}
# Add to path add the begin or end of the PATH
# Returns: 1 or sets the new PATH
# Called like: add_to_path <directory> (pre|post)
function add_to_path {
local directory=$1
local location=$2
#Validates command line parameters
if [ -z "$directory" ]; then
echo "$O:$FUNCNAME: requires a directory to add" >&2
echo "e.g add_to_path /bin pre" >&2
return 1
fi
#Not relative paths
if [ $(echo $directory | grep '^/') ]; then
: echo "$O:$FUNCNAME: '$directory' is absolute" >&2
else
echo "$O:$FUNCNAME: '$directory' must be an absolute directory" >&2
return 1
fi
#Validates presence of the directory
if [ -d "$directory" ]; then
: echo "$O:$FUNCNAME: directory exist" >&2
else
echo "$O:$FUNCNAME: '$directory' does not exist --- aborting" >&2
return 1
fi
#Avoid to repeat
if [ $(contains "$PATH" "$directory") ]; then
echo "$O:$FUNCNAME: '$directory' already in \$PATH --- aborting" >&2
return 1
else
: echo "$O:$FUNCNAME: adding directory to path" >&2
fi
#position new dir
case $location in
pre* ) PATH="$directory:$PATH" ;;
post* ) PATH="$PATH:$directory" ;;
* ) PATH="$PATH:$directory" ;;
esac
#Clean
PATH=$(clean_path $PATH)
}
# Remove directory from path
# Returns: sets the new $PATH
# Called like: rm_from_path <directory>
function rm_from_path {
local directory=$1
#Remove instances
PATH=${PATH//$directory}
#Clean
PATH=$(clean_path $PATH)
}
# Remove leading/trailing or duplicate ':', remove duplicate entries
# Returns: echos the "cleaned up" path
# Called like: cleaned_path=$(clean_path $PATH)
function clean_path {
local path=$1
local newpath
local directory
# Validates we have something to work with
[ -z "$path" ] && return 1
#Remove duplicates
for directory in ${path//:/ }; do
contains "$newpath" "$directory" && newpath="${newpath}:${directory}"
done
#Remove any leading ':'
#Remove any trailing ':'
#Remove duplicates ':'
newpath=$(echo $newpath | sed 's/^:*//; s/:*$//; s/::/:/g')
#Return new path
echo "$newpath"
}
# Determine if the path contains a given directory
# Return 1 if target is contained within pattern, 0 otherwise
# Called like: contains $PATH $dir
function contains {
local pattern=":$1:"
local target=$2
#case sensitive
case $pattern in
*:$target:* ) return 1 ;;
* ) return 0 ;;
esac
}
# Plays an entire directory recursive with mplayer
function play_that {
local directory=$1
find "$directory" -iname *.[wmo][mpg][a3g] -exec mplayer {} +
}
# Compression
compress() { tar -czf "${1%/}.tar.gz" "${1%/}"; }
alias decompress="tar -xzf"
# SSH Port Forwarding Functions
fip() {
(( $# < 2 )) && echo "Usage: fip <host> <port1> [port2] ..." && return 1
local host="$1"
shift
for port in "$@"; do
ssh -f -N -L "$port:localhost:$port" "$host" && echo "Forwarding localhost:$port -> $host:$port"
done
}
dip() {
(( $# == 0 )) && echo "Usage: dip <port1> [port2] ..." && return 1
for port in "$@"; do
pkill -f "ssh.*-L $port:localhost:$port" && echo "Stopped forwarding port $port" || echo "No forwarding on port $port"
done
}
lip() {
pgrep -af "ssh.*-L [0-9]+:localhost:[0-9]+" || echo "No active forwards"
}