/
file.sh
198 lines (183 loc) · 5.66 KB
/
file.sh
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
# @package: util/file
# Routines for common file operations
_bb_onfirstload "bb_util_file" || return
bb_load "util/list"
bb_load "util/env"
################################################################################
# Globals
################################################################################
################################################################################
# Functions
################################################################################
# function: bb_canonicalize [-v VAR] PATH
# Resolves . and .. in a given absolute path
# @arguments:
# - VAR: variable to store result (if not given, prints to stdout)
# - PATH: an absolute path
# @returns: 1 if PATH is invalid, 0 otherwise
function bb_canonicalize () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local path="$1"
[[ "${path:0:1}" == '/' ]] || return "$__bb_false"
local patharr=()
bb_split -V patharr '/' "$path"
local fixed=()
for dir in "${patharr[@]}"; do
case $dir in
.|'') ;;
..)
bb_pop fixed
;;
*)
fixed+=("$dir")
;;
esac
done
_bb_result "/$(bb_join '/' "${fixed[@]}")"
return "$__bb_true"
}
# function: bb_abspath [-v VAR] TARGET [FROM]
# Returns the absolute path from a relative one
# @arguments:
# - VAR: variable to store result (if not given, prints to stdout)
# - TARGET: target relative path (can be file or directory)
# - FROM: the absolute directory path from which the absolute path is formed
# (Defaults to $PWD)
function bb_abspath () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local target="$1"
local from="${2:-$PWD}"
local result
[[ "${target:0:1}" =~ [/~] ]] || target="$from/$target"
bb_canonicalize -v result "$target"
_bb_result "$result"
}
# function: bb_relpath [-v VAR] TARGET [FROM]
# Returns the relative path from a directory to the target
# @arguments:
# - VAR: variable to store result (if not given, prints to stdout)
# - TARGET: target absolute path (can be file or directory)
# - FROM: the absolute directory path from which the relative path is formed
# (Defaults to $PWD)
# @returns: 1 if either TARGET or FROM is invalid, 0 otherwise
function bb_relpath () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local target="$1"
local from="${2:-$PWD}"
local result=""
# Check inputs, must be absolute
[[ "${target:0:1}" == '/' ]] || return "$__bb_false"
[[ "${from:0:1}" == '/' ]] || return "$__bb_false"
# Strip any trailing slashes
target="${target%/}"
from="${from%/}"
# Canonicalize paths
bb_canonicalize -v target "$target"
bb_canonicalize -v from "$from"
# Handle trivial . case
if [[ "$target" == "$from" ]]; then
_bb_result "."
return "$__bb_true"
fi
# Find the common parent directory
local common="$from/"
while [[ "${target#$common}" == "${target}" ]]; do
common="${common%/}"
common="${common%/*}"
result+="${result:+/}.."
done
# Append
result+="${target#$common}"
_bb_result "$result"
return "$__bb_true"
}
# function: bb_prettypath PATH
# Prints a pretty version of the path
# @arguments:
# - PATH: a path
# @notes:
# Replaces home directory with ~
function bb_prettypath () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local result="$1"
result="${result/#$HOME/\~}"
_bb_result "$result"
}
# function: bb_countlines FILENAME ...
# Counts the number of lines in a list of files
# @arguments:
# - FILENAME: a valid filename
# @returns: 1 if any of the filenames are invalid, 0 otherwise
function bb_countlines () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
local f line
local -i ct=0
for f in "$@"; do
[[ -f "$f" ]] || return "$__bb_false"
while read -r line; do
if [[ -n $__bb_util_file_countlines_filter ]]; then
$__bb_util_file_countlines_filter "$line" || continue
fi
(( ct++ ))
done < "$f"
done
_bb_result "$ct"
return "$__bb_true"
}
# function: bb_countmatches PATTERN FILENAME ...
# Counts the number of matching lines in a list of files
# @arguments:
# - PATTERN: a valid bash regular expression
# - FILENAME: a valid filename
# @returns: 1 if any of the filenames are invalid, 0 otherwise
function bb_countmatches () {
_bb_glopts "$@"; set -- "${__bb_args[@]}"
__bb_util_file_grep_pattern="$1"
shift
_bb_util_file_grep () { [[ $1 =~ $__bb_util_file_grep_pattern ]]; }
__bb_util_file_countlines_filter=_bb_util_file_grep
local result
bb_countlines -v result "$@"
local rc=$?
unset __bb_util_file_countlines_filter
unset __bb_util_file_grep_pattern
unset -f _bb_util_file_grep
_bb_result "$result"
return $rc
}
# function: bb_extpush EXT FILENAME ...
# Adds the file extension EXT to all given files
# @arguments:
# - EXT: the file extension
# - FILENAME: a valid filename
function bb_extpush () {
local ext="$1"; shift;
bb_checkset ext || return
local f
for f in "$@"; do
mv "$f" "$f.$ext"
done
}
# function: bb_extpop FILENAME ...
# Removes the last file extension from the given files
# @arguments:
# - FILENAME: a valid filename
function bb_extpop () {
local f
for f in "$@"; do
local new="${f%.*}"
[[ "$f" == "$new" ]] || mv "$f" "$new"
done
}
# function: bb_hardcopy FILENAME ...
# Replaces symbolic links with deep copies
# @arguments:
# - FILENAME: a valid symbolic link
function bb_hardcopy () {
local f
for f in "$@"; do
[[ -L "$f" ]] || continue
local real="$(readlink -f "$f")"
unlink "$f" && cp -r "$real" "$f"
done
}