-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe-file
executable file
·54 lines (43 loc) · 1.48 KB
/
pipe-file
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
#!/usr/bin/env sh
# Usage: doc/api.md#pipe-file
# Create an alias
. "${SHELL_API_PATH}"/common/expand-aliases
alias pipe-file="pipe_file"
pipe_file(){
depth=""
placeholder=""
sudo_cmd=""
if [ -z "${1}" ]; then
echo "pipe-file: arguments are missing"
return 127
fi
if [ "${1}" = "-a" ] || [ "${1}" = "--auth" ]; then
sudo_cmd=" sudo"
shift $(( $# > 0 ? 1 : 0 ))
fi
if [ "$1" -ge 0 ] >/dev/null 2>&1; then
depth=" -maxdepth ${1}"
shift $(( $# > 0 ? 1 : 0 ))
fi
file_path=$(dirname "${1}")
file_pattern=$(basename "${1}")
shift $(( $# > 0 ? 1 : 0 ))
if [ -z "${file_path}" ]; then
echo "Path is missing"
return 127
fi
if [ -z "${file_pattern}" ]; then
echo "Pattern is missing"
return 127
fi
if [ -z "${1}" ]; then
echo "Command argument is missing"
return 127
fi
# The '-iname' option instructs 'find' to search for files based on a pattern.
# The '-print0' option instructs 'find' to properly handle filenames containing blanks or newlines.
# The '-0' option instructs 'xargs' to split input on null bytes instead of blanks or newlines.
# The '-I' option instructs 'xargs' to look for the '{}' placeholder and replace it with a filename.
if echo "${*}" | grep -q "{}" >/dev/null 2>&1; then placeholder=" -I{}"; fi
eval "find ${file_path} -iname ${file_pattern}${depth} -print0 | xargs -0${placeholder}${sudo_cmd} ${*}"
}