-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwalk-run
executable file
·110 lines (99 loc) · 2.63 KB
/
walk-run
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
#!/usr/bin/env bash
#
# Requirements:
# - parallel: https://www.gnu.org/software/parallel/
# - walk: included in the bash-stdops project
# - nproc: https://savannah.gnu.org/git/?group=coreutils
# - sed
#
# Author: James Cherti
# URL: https://github.com/jamescherti/bash-stdops
#
# Description:
# ------------
# Recursively execute a command on all files listed by the `rg --files` command.
#
# For example, to recursively `cat` all text files in `/etc`, use the following command:
# ```
# walk-run /etc cat {}
# ```
#
# (`{}` is replaced with the path to each file.)
#
# License:
# --------
# Copyright (C) 2012-2025 James Cherti
#
# Distributed under terms of the GNU General Public License version 3.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
if [[ $RGFILES_RUN_CONFIRM = "" ]]; then
RGFILES_RUN_CONFIRM="1"
fi
set -euf -o pipefail
if [[ $# -lt 2 ]]; then
echo "Recursively execute a command on all files listed by " \
"the 'rg --files' command."
echo
echo "Usage: $0 <directory> <command> <arg1> [args...]" >&2
echo
exit 1
fi
display_quoted_args() {
local first=1
local arg
for arg in "$@"; do
escaped_arg="${arg//\"/\\\"}"
if [[ $first -ne 0 ]]; then
first=0
else
printf " "
fi
if [[ "$escaped_arg" =~ \ ]]; then
# Quote and escape the argument if it contains spaces
printf "\"%s\"" "$escaped_arg"
else
# Display the argument without quoting if it does not contain spaces
printf "%s" "$escaped_arg"
fi
done
}
#
# Directory
#
DIR="$1"
shift
if ! [[ -d "$DIR" ]]; then
echo "Error: The specified directory does not exist: $DIR" >&2
exit 1
fi
#
# RUN
#
RGFILES_CMD=(walk "$DIR")
PARALLEL_CMD=(qparallel "$@")
if [[ $RGFILES_RUN_CONFIRM -ne 0 ]]; then
# display_quoted_args "[DEBUG]" "${RGFILES_CMD[@]}" "|" "${PARALLEL_CMD[@]}"
echo -n "[COMMAND] "
display_quoted_args "$@"
echo
echo
read -r -p "Execute against all the files in '$DIR'? [y,n] " ANSWER
if [[ "$ANSWER" != "y" ]]; then
echo "Interrupted." >&2
exit 1
fi
fi
"${RGFILES_CMD[@]}" | "${PARALLEL_CMD[@]}"