parallel: Better error message if 'rsync' is not in $PATH.
[parallel.git] / src / env_parallel.dash
blob7925d1637c3e5bd468da710bb87fa2631f76926c
1 #!/usr/bin/env dash
3 # This file must be sourced in dash:
5 # . `which env_parallel.dash`
7 # after which 'env_parallel' works
10 # Copyright (C) 2016-2022 Ole Tange, http://ole.tange.dk and Free
11 # Software Foundation, Inc.
13 # This program is free software; you can redistribute it and/or modify
14 # it under the terms of the GNU General Public License as published by
15 # the Free Software Foundation; either version 3 of the License, or
16 # (at your option) any later version.
18 # This program is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, see <http://www.gnu.org/licenses/>
25 # or write to the Free Software Foundation, Inc., 51 Franklin St,
26 # Fifth Floor, Boston, MA 02110-1301 USA
28 # SPDX-FileCopyrightText: 2021-2022 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
29 # SPDX-License-Identifier: GPL-3.0-or-later
30 # shellcheck disable=SC2006
32 env_parallel() {
33 # based on env_parallel.sh
35 _names_of_ALIASES() {
36 # alias fails on Unixware 5
37 for _i in `alias 2>/dev/null | perl -ne 's/^alias //;s/^(\S+)=.*/$1/ && print' 2>/dev/null`; do
38 # Check if this name really is an alias
39 # or just part of a multiline alias definition
40 if alias "$_i" >/dev/null 2>/dev/null; then
41 echo "$_i"
43 done
45 _bodies_of_ALIASES() {
46 # alias may return:
47 # myalias='definition' (GNU/Linux ash)
48 # alias myalias='definition' (FreeBSD ash)
49 # so remove 'alias ' from first line
50 for _i in "$@"; do
51 echo 'alias '"`alias "$_i" | perl -pe '1..1 and s/^alias //'`"
52 done
54 _names_of_maybe_FUNCTIONS() {
55 set | perl -ne '/^([A-Z_0-9]+)\s*\(\)\s*\{?$/i and print "$1\n"'
57 _names_of_FUNCTIONS() {
58 # myfunc is a function
59 # shellcheck disable=SC2046
60 LANG=C type `_names_of_maybe_FUNCTIONS` |
61 perl -ne '/^(\S+) is a function$/ and not $seen{$1}++ and print "$1\n"'
63 _bodies_of_FUNCTIONS() {
64 LANG=C type "$@" | perl -ne '/^(\S+) is a function$/ or print'
66 _names_of_VARIABLES() {
67 # This may screw up if variables contain \n and =
68 set | perl -ne 's/^(\S+?)=.*/$1/ and print;'
70 _bodies_of_VARIABLES() {
71 # Crappy typeset -p
72 for _i in "$@"
74 perl -e 'print @ARGV' "$_i="
75 eval echo "\"\$$_i\"" | perl -e '$/=undef; $a=<>; chop($a); print $a' |
76 perl -pe 's/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\202-\377]/\\$&/go;'"s/'/\\\'/g; s/[\n]/'\\n'/go;";
77 echo
78 done
80 _ignore_HARDCODED() {
81 # These names cannot be detected
82 echo '(_|TIMEOUT|IFS)'
84 _ignore_READONLY() {
85 # shellcheck disable=SC1078,SC1079,SC2026
86 readonly | perl -e '@r = map {
87 chomp;
88 # sh on UnixWare: readonly TIMEOUT
89 # ash: readonly var='val'
90 # ksh: var='val'
91 # mksh: PIPESTATUS[0]
92 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
93 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
94 # zsh: typeset -r var='val'
95 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
96 $_ } <>;
97 $vars = join "|",map { quotemeta $_ } @r;
98 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
101 _remove_bad_NAMES() {
102 # Do not transfer vars and funcs from env_parallel
103 # shellcheck disable=SC2006
104 _ignore_RO="`_ignore_READONLY`"
105 # shellcheck disable=SC2006
106 _ignore_HARD="`_ignore_HARDCODED`"
107 # Macos-grep does not like long patterns
108 # Old Solaris grep does not support -E
109 # Perl Version of:
110 # grep -Ev '^(...)$' |
111 perl -ne '/^(
112 PARALLEL_ENV|
113 PARALLEL_TMP|
114 _alias_NAMES|
115 _bodies_of_ALIASES|
116 _bodies_of_FUNCTIONS|
117 _bodies_of_VARIABLES|
118 _error_PAR|
119 _function_NAMES|
120 _get_ignored_VARS|
121 _grep_REGEXP|
122 _ignore_HARD|
123 _ignore_HARDCODED|
124 _ignore_READONLY|
125 _ignore_RO|
126 _ignore_UNDERSCORE|
127 _list_alias_BODIES|
128 _list_function_BODIES|
129 _list_variable_VALUES|
130 _make_grep_REGEXP|
131 _names_of_ALIASES|
132 _names_of_FUNCTIONS|
133 _names_of_VARIABLES|
134 _names_of_maybe_FUNCTIONS|
135 _parallel_exit_CODE|
136 _prefix_PARALLEL_ENV|
137 _prefix_PARALLEL_ENV|
138 _remove_bad_NAMES|
139 _remove_readonly|
140 _variable_NAMES|
141 _warning_PAR|
142 _which_PAR)$/x and next;
143 # Filter names matching --env
144 /^'"$_grep_REGEXP"'$/ or next;
145 /^'"$_ignore_UNDERSCORE"'$/ and next;
146 # Remove readonly variables
147 /^'"$_ignore_RO"'$/ and next;
148 /^'"$_ignore_HARD"'$/ and next;
149 print;'
151 _get_ignored_VARS() {
152 perl -e '
153 for(@ARGV){
154 $next_is_env and push @envvar, split/,/, $_;
155 $next_is_env=/^--env$/;
157 if(grep { /^_$/ } @envvar) {
158 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
159 print STDERR "parallel: Error: ",
160 "Run \"parallel --record-env\" in a clean environment first.\n";
161 } else {
162 chomp(@ignored_vars = <IN>);
165 if($ENV{PARALLEL_IGNORED_NAMES}) {
166 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
167 chomp @ignored_vars;
169 $vars = join "|",map { quotemeta $_ } @ignored_vars;
170 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
171 ' -- "$@"
174 # Get the --env variables if set
175 # --env _ should be ignored
176 # and convert a b c to (a|b|c)
177 # If --env not set: Match everything (.*)
178 _make_grep_REGEXP() {
179 perl -e '
180 for(@ARGV){
181 /^_$/ and $next_is_env = 0;
182 $next_is_env and push @envvar, split/,/, $_;
183 $next_is_env = /^--env$/;
185 $vars = join "|",map { quotemeta $_ } @envvar;
186 print $vars ? "($vars)" : "(.*)";
187 ' -- "$@"
189 _which_PAR() {
190 # type returns:
191 # ll is an alias for ls -l (in ash)
192 # bash is a tracked alias for /bin/bash
193 # true is a shell builtin (in bash)
194 # myfunc is a function (in bash)
195 # myfunc is a shell function (in zsh)
196 # which is /usr/bin/which (in sh, bash)
197 # which is hashed (/usr/bin/which)
198 # gi is aliased to `grep -i' (in bash)
199 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
200 # Return 0 if found, 1 otherwise
201 LANG=C type "$@" |
202 perl -pe '$exit += (s/ is an alias for .*// ||
203 s/ is aliased to .*// ||
204 s/ is a function// ||
205 s/ is a shell function// ||
206 s/ is a shell builtin// ||
207 s/.* is hashed .(\S+).$/$1/ ||
208 s/.* is (a tracked alias for )?//);
209 END { exit not $exit }'
211 _warning_PAR() {
212 echo "env_parallel: Warning: $*" >&2
214 _error_PAR() {
215 echo "env_parallel: Error: $*" >&2
218 if _which_PAR parallel >/dev/null; then
219 true parallel found in path
220 else
221 # shellcheck disable=SC2016
222 _error_PAR 'parallel must be in $PATH.'
223 return 255
226 # Grep regexp for vars given by --env
227 # shellcheck disable=SC2006
228 _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
229 unset _make_grep_REGEXP
231 # Deal with --env _
232 # shellcheck disable=SC2006
233 _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
234 unset _get_ignored_VARS
236 # --record-env
237 if perl -e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
238 true skip
239 else
240 (_names_of_ALIASES;
241 _names_of_FUNCTIONS;
242 _names_of_VARIABLES) |
243 cat > "$HOME"/.parallel/ignored_vars
244 return 0
247 # --session
248 if perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
249 true skip
250 else
251 # Insert ::: between each level of session
252 # so you can pop off the last ::: at --end-session
253 # shellcheck disable=SC2006
254 PARALLEL_IGNORED_NAMES="`echo \"$PARALLEL_IGNORED_NAMES\";
255 echo :::;
256 (_names_of_ALIASES;
257 _names_of_FUNCTIONS;
258 _names_of_VARIABLES) | perl -ne '
259 BEGIN{
260 map { $ignored_vars{$_}++ }
261 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
263 chomp;
264 for(split/\s+/) {
265 if(not $ignored_vars{$_}) {
266 print $_,\"\\n\";
270 export PARALLEL_IGNORED_NAMES
271 return 0
273 if perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
274 true skip
275 else
276 # Pop off last ::: from PARALLEL_IGNORED_NAMES
277 # shellcheck disable=SC2006
278 PARALLEL_IGNORED_NAMES="`perl -e '
279 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
280 print $ENV{PARALLEL_IGNORED_NAMES}
282 return 0
284 # Grep alias names
285 # shellcheck disable=SC2006
286 _alias_NAMES="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
287 _list_alias_BODIES="_bodies_of_ALIASES $_alias_NAMES"
288 if [ "$_alias_NAMES" = "" ] ; then
289 # no aliases selected
290 _list_alias_BODIES="true"
292 unset _alias_NAMES
294 # Grep function names
295 # shellcheck disable=SC2006
296 _function_NAMES="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
297 _list_function_BODIES="_bodies_of_FUNCTIONS $_function_NAMES"
298 if [ "$_function_NAMES" = "" ] ; then
299 # no functions selected
300 _list_function_BODIES="true"
302 unset _function_NAMES
304 # Grep variable names
305 # shellcheck disable=SC2006
306 _variable_NAMES="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
307 _list_variable_VALUES="_bodies_of_VARIABLES $_variable_NAMES"
308 if [ "$_variable_NAMES" = "" ] ; then
309 # no variables selected
310 _list_variable_VALUES="true"
312 unset _variable_NAMES
314 # shellcheck disable=SC2006
315 PARALLEL_ENV="`
316 $_list_alias_BODIES;
317 $_list_function_BODIES;
318 $_list_variable_VALUES;
320 export PARALLEL_ENV
321 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
322 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
323 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
324 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
325 unset _remove_bad_NAMES _grep_REGEXP
326 unset _prefix_PARALLEL_ENV
327 # Test if environment is too big by running 'true'
328 # shellcheck disable=SC2006,SC2092
329 if `_which_PAR true` >/dev/null 2>/dev/null ; then
330 parallel "$@"
331 _parallel_exit_CODE=$?
332 # Clean up variables/functions
333 unset PARALLEL_ENV
334 unset _which_PAR _which_TRUE
335 unset _warning_PAR _error_PAR
336 # Unset _parallel_exit_CODE before return
337 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
338 else
339 unset PARALLEL_ENV;
340 _error_PAR "Your environment is too big."
341 _error_PAR "You can try 3 different approaches:"
342 _error_PAR "1. Run 'env_parallel --session' before you set"
343 _error_PAR " variables or define functions."
344 _error_PAR "2. Use --env and only mention the names to copy."
345 _error_PAR "3. Try running this in a clean environment once:"
346 _error_PAR " env_parallel --record-env"
347 _error_PAR " And then use '--env _'"
348 _error_PAR "For details see: man env_parallel"
349 return 255
353 parset() {
354 _parset_PARALLEL_PRG=parallel
355 _parset_main "$@"
358 env_parset() {
359 _parset_PARALLEL_PRG=env_parallel
360 _parset_main "$@"
363 _parset_main() {
364 # If $1 contains ',' or space:
365 # Split on , to get the destination variable names
366 # If $1 is a single destination variable name:
367 # Treat it as the name of an array
369 # # Create array named myvar
370 # parset myvar echo ::: {1..10}
371 # echo ${myvar[5]}
373 # # Put output into $var_a $var_b $var_c
374 # varnames=(var_a var_b var_c)
375 # parset "${varnames[*]}" echo ::: {1..3}
376 # echo $var_c
378 # # Put output into $var_a4 $var_b4 $var_c4
379 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
380 # echo $var_c4
382 _parset_NAME="$1"
383 if [ "$_parset_NAME" = "" ] ; then
384 echo parset: Error: No destination variable given. >&2
385 echo parset: Error: Try: >&2
386 echo parset: Error: ' ' parset myarray echo ::: foo bar >&2
387 return 255
389 if [ "$_parset_NAME" = "--help" ] ; then
390 echo parset: Error: Usage: >&2
391 echo parset: Error: ' ' parset varname GNU Parallel options and command >&2
392 echo
393 parallel --help
394 return 255
396 if [ "$_parset_NAME" = "--version" ] ; then
397 # shellcheck disable=SC2006
398 echo "parset 20220723 (GNU parallel `parallel --minversion 1`)"
399 echo "Copyright (C) 2007-2022 Ole Tange, http://ole.tange.dk and Free Software"
400 echo "Foundation, Inc."
401 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
402 echo "This is free software: you are free to change and redistribute it."
403 echo "GNU parallel comes with no warranty."
404 echo
405 echo "Web site: https://www.gnu.org/software/parallel"
406 echo
407 echo "When using programs that use GNU Parallel to process data for publication"
408 echo "please cite as described in 'parallel --citation'."
409 echo
410 return 255
412 shift
414 # Bash: declare -A myassoc=( )
415 # Zsh: typeset -A myassoc=( )
416 # Ksh: typeset -A myassoc=( )
417 # shellcheck disable=SC2039,SC2169
418 if (typeset -p "$_parset_NAME" 2>/dev/null; echo) |
419 perl -ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
420 # This is an associative array
421 # shellcheck disable=SC2006
422 eval "`$_parset_PARALLEL_PRG -k --_parset assoc,"$_parset_NAME" "$@"`"
423 # The eval returns the function!
424 else
425 # This is a normal array or a list of variable names
426 # shellcheck disable=SC2006
427 eval "`$_parset_PARALLEL_PRG -k --_parset var,"$_parset_NAME" "$@"`"
428 # The eval returns the function!