Released as 20240522 ('Tbilisi')
[parallel.git] / src / env_parallel.mksh
blobd736f1b4d413ec9d379ac7614e2ca30bb5004eb5
1 #!/usr/bin/env ksh
3 # This file must be sourced in sh/ash/dash/bash/ksh/mksh/zsh:
5 # . env_parallel.sh
6 # source env_parallel.ash
7 # source env_parallel.dash
8 # source env_parallel.bash
9 # source env_parallel.ksh
10 # source env_parallel.mksh
11 # source env_parallel.zsh
13 # after which 'env_parallel' works
16 # Copyright (C) 2016-2024 Ole Tange, http://ole.tange.dk and Free
17 # Software Foundation, Inc.
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms of the GNU General Public License as published by
21 # the Free Software Foundation; either version 3 of the License, or
22 # (at your option) any later version.
24 # This program is distributed in the hope that it will be useful, but
25 # WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 # General Public License for more details.
29 # You should have received a copy of the GNU General Public License
30 # along with this program; if not, see <http://www.gnu.org/licenses/>
31 # or write to the Free Software Foundation, Inc., 51 Franklin St,
32 # Fifth Floor, Boston, MA 02110-1301 USA
34 # SPDX-FileCopyrightText: 2021-2024 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
35 # SPDX-License-Identifier: GPL-3.0-or-later
36 # shellcheck disable=SC2006
38 env_parallel() {
39 # env_parallel.{sh,ash,dash,bash,ksh,mksh,zsh}
41 # Check shell dialect
42 if [ -n "$BASH_VERSION" ]; then
43 _shell_DIALECT=bash
44 _eval_needed=false
45 _prefix_PARALLEL_ENV=_prefix_PARALLEL_ENV_bash
46 elif [ -n "$ZSH_VERSION" ]; then
47 _shell_DIALECT=zsh
48 _eval_needed=true
49 _prefix_PARALLEL_ENV=false
50 elif [ -n "$KSH_VERSION" ]; then
51 _shell_DIALECT=ksh
52 _eval_needed=false
53 _prefix_PARALLEL_ENV=false
54 else
55 # Dash/ash - can these be detected better?
56 _shell_DIALECT=sh
57 _eval_needed=false
58 _prefix_PARALLEL_ENV=false
60 _names_of_ALIASES() {
61 _names_of_ALIASES_$_shell_DIALECT
63 _names_of_ALIASES_sh() {
64 # alias fails on Unixware 5
65 for _i in `alias 2>/dev/null | perl -ne 's/^alias //;s/^(\S+)=.*/$1/ && print' 2>/dev/null`; do
66 # Check if this name really is an alias
67 # or just part of a multiline alias definition
68 if alias "$_i" >/dev/null 2>/dev/null; then
69 echo "$_i"
71 done
73 _names_of_ALIASES_bash() {
74 # No aliases will return false. This error should be ignored.
75 compgen -a || true
77 _names_of_ALIASES_ksh() {
78 alias | perl -pe 's/=.*//'
80 _names_of_ALIASES_zsh() {
81 print -l ${(k)aliases}
83 _bodies_of_ALIASES() {
84 _bodies_of_ALIASES_$_shell_DIALECT "$@"
86 _bodies_of_ALIASES_sh() {
87 # alias may return:
88 # myalias='definition' (GNU/Linux ash)
89 # alias myalias='definition' (FreeBSD ash)
90 # so remove 'alias ' from first line
91 for _i in "$@"; do
92 echo 'alias '"`alias "$_i" | perl -pe '1..1 and s/^alias //'`"
93 done
95 _bodies_of_ALIASES_bash() {
96 local _i
97 for _i in "$@"; do
98 # shellcheck disable=SC2046
99 if [ $(alias "$_i" | wc -l) == 1 ] ; then
100 true Alias is a single line. Good.
101 else
102 _warning_PAR "Alias '$_i' contains newline."
103 _warning_PAR "Make sure the command has at least one newline after '$_i'."
104 _warning_PAR "See BUGS in 'man env_parallel'."
106 done
107 alias "$@"
109 _bodies_of_ALIASES_ksh() {
110 alias "$@" | perl -pe 's/^/alias /;
111 sub warning { print STDERR "env_parallel: Warning: @_\n"; }
112 if(/^alias (\S+)=\$.*\\n/) {
113 warning("Alias \"$1\" contains newline.");
114 warning("Make sure the command has at least one newline after \"$1\".");
115 warning("See BUGS in \"man env_parallel\".");
119 _bodies_of_ALIASES_zsh() {
120 local _i
121 for _i in "$@"; do
122 echo 'alias '"$(alias $_i)"
123 done
125 _names_of_FUNCTIONS() {
126 _names_of_FUNCTIONS_$_shell_DIALECT
128 _names_of_FUNCTIONS_bash() {
129 compgen -A function
131 _names_of_maybe_FUNCTIONS() {
132 set | perl -ne '/^([A-Z_0-9]+)\s*\(\)\s*\{?$/i and print "$1\n"'
134 _names_of_FUNCTIONS_sh() {
135 # myfunc is a function
136 # shellcheck disable=SC2046
137 LANG=C type `_names_of_maybe_FUNCTIONS` |
138 perl -ne '/^(\S+) is a function$/ and not $seen{$1}++ and print "$1\n"'
140 _names_of_FUNCTIONS_ksh() {
141 # mksh = typeset +f
142 # ksh = typeset +p -f | perl -pe 's/\(\).*//'
143 typeset +f | perl -pe 's/\(\).*//; s/ .*//;'
145 _names_of_FUNCTIONS_zsh() {
146 print -l ${(k)functions}
148 _bodies_of_FUNCTIONS() {
149 _bodies_of_FUNCTIONS_$_shell_DIALECT "$@"
151 _bodies_of_FUNCTIONS_sh() {
152 LANG=C type "$@" | perl -ne '/^(\S+) is a function$/ or print'
154 _bodies_of_FUNCTIONS_bash() {
155 typeset -f "$@"
157 _bodies_of_FUNCTIONS_ksh() {
158 functions "$@"
160 _bodies_of_FUNCTIONS_zsh() {
161 typeset -f "$@"
163 _names_of_VARIABLES() {
164 _names_of_VARIABLES_$_shell_DIALECT
166 _names_of_VARIABLES_sh() {
167 # This may screw up if variables contain \n and =
168 set | perl -ne 's/^(\S+?)=.*/$1/ and print;'
170 _names_of_VARIABLES_bash() {
171 compgen -A variable
173 _names_of_VARIABLES_ksh() {
174 # mksh: typeset +p |
175 # perl -pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
176 # uniq
177 # ksh: typeset +p | perl -pe 's/^typeset .. //'
178 typeset +p |
179 perl -pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
180 uniq
182 _names_of_VARIABLES_zsh() {
183 print -l ${(k)parameters}
185 _bodies_of_VARIABLES() {
186 _bodies_of_VARIABLES_$_shell_DIALECT "$@"
188 _bodies_of_VARIABLES_sh() {
189 # Crappy typeset -p
190 for _i in "$@"
192 perl -e 'print @ARGV' "$_i="
193 eval echo "\"\$$_i\"" | perl -e '$/=undef; $a=<>; chop($a); print $a' |
194 perl -pe 's/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\202-\377]/\\$&/go;'"s/'/\\\'/g; s/[\n]/'\\n'/go;";
195 echo
196 done
198 _bodies_of_VARIABLES_bash() {
199 typeset -p "$@"
201 _bodies_of_VARIABLES_ksh() {
202 typeset -p "$@"
204 _bodies_of_VARIABLES_zsh() {
205 typeset -p "$@"
207 _ignore_HARDCODED() {
208 _ignore_HARDCODED_$_shell_DIALECT
210 _ignore_HARDCODED_sh() {
211 # These names cannot be detected
212 echo '(_|TIMEOUT|IFS)'
214 _ignore_HARDCODED_bash() {
215 # Copying $RANDOM will cause it not to be random
216 # The rest cannot be detected as read-only
217 echo '(RANDOM|_|TIMEOUT|GROUPS|FUNCNAME|DIRSTACK|PIPESTATUS|USERNAME|BASHPID|BASH_[A-Z_]+)'
219 _ignore_HARDCODED_ksh() {
220 # These names cannot be detected
221 echo '(_|TIMEOUT|IFS)'
223 _ignore_HARDCODED_zsh() {
224 # These names cannot be detected
225 echo '([-\?\#\!\$\*\@\_0]|zsh_eval_context|ZSH_EVAL_CONTEXT|LINENO|IFS|commands|functions|options|aliases|EUID|EGID|UID|GID|dis_patchars|patchars|terminfo|galiases|keymaps|parameters|jobdirs|dirstack|functrace|funcsourcetrace|zsh_scheduled_events|dis_aliases|dis_reswords|dis_saliases|modules|reswords|saliases|widgets|userdirs|historywords|nameddirs|termcap|dis_builtins|dis_functions|jobtexts|funcfiletrace|dis_galiases|builtins|history|jobstates|funcstack|run-help)'
227 _ignore_READONLY() {
228 _ignore_READONLY_$_shell_DIALECT
230 _parse_READONLY() {
231 # shellcheck disable=SC1078,SC1079,SC2026
232 perl -e '@r = map {
233 chomp;
234 # sh on UnixWare: readonly TIMEOUT
235 # ash: readonly var='val'
236 # ksh: var='val'
237 # mksh: PIPESTATUS[0]
238 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
239 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
240 # zsh: typeset -r var='val'
241 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
242 $_ } <>;
243 $vars = join "|",map { quotemeta $_ } @r;
244 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
247 _ignore_READONLY_sh() {
248 readonly | _parse_READONLY
250 _ignore_READONLY_bash() {
251 readonly | _parse_READONLY
253 _ignore_READONLY_ksh() {
254 readonly | _parse_READONLY
256 _ignore_READONLY_zsh() {
257 typeset -pr | _parse_READONLY
259 _remove_bad_NAMES() {
260 # Do not transfer vars and funcs from env_parallel
261 # shellcheck disable=SC2006
262 _ignore_RO="`_ignore_READONLY`"
263 # shellcheck disable=SC2006
264 _ignore_HARD="`_ignore_HARDCODED`"
265 # To avoid depending on grep dialect, use Perl version of:
266 # grep -Ev '^(...)$' |
267 perl -ne '/^(
268 PARALLEL_ENV|
269 PARALLEL_TMP|
270 _alias_NAMES|
271 _bodies_of_ALIASES|
272 _bodies_of_FUNCTIONS|
273 _bodies_of_VARIABLES|
274 _error_PAR|
275 _function_NAMES|
276 _get_ignored_VARS|
277 _grep_REGEXP|
278 _ignore_HARD|
279 _ignore_HARDCODED|
280 _ignore_READONLY|
281 _ignore_RO|
282 _ignore_UNDERSCORE|
283 _list_alias_BODIES|
284 _list_function_BODIES|
285 _list_variable_VALUES|
286 _make_grep_REGEXP|
287 _names_of_ALIASES|
288 _names_of_FUNCTIONS|
289 _names_of_VARIABLES|
290 _names_of_maybe_FUNCTIONS|
291 _parallel_exit_CODE|
292 _prefix_PARALLEL_ENV|
293 _prefix_PARALLEL_ENV|
294 _remove_bad_NAMES|
295 _remove_readonly|
296 _variable_NAMES|
297 _warning_PAR|
298 _which_PAR)$/x and next;
299 # Filter names matching --env
300 /^'"$_grep_REGEXP"'$/ or next;
301 /^'"$_ignore_UNDERSCORE"'$/ and next;
302 # Remove readonly variables
303 /^'"$_ignore_RO"'$/ and next;
304 /^'"$_ignore_HARD"'$/ and next;
305 print;'
307 _prefix_PARALLEL_ENV_bash() {
308 shopt 2>/dev/null |
309 perl -pe 's:\s+off:;: and s/^/shopt -u /;
310 s:\s+on:;: and s/^/shopt -s /;
311 s:;$:&>/dev/null;:';
312 echo 'shopt -s expand_aliases &>/dev/null';
315 _get_ignored_VARS() {
316 perl -e '
317 for(@ARGV){
318 $next_is_env and push @envvar, split/,/, $_;
319 $next_is_env=/^--env$/;
321 if(grep { /^_$/ } @envvar) {
322 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
323 print STDERR "parallel: Error: ",
324 "Run \"parallel --record-env\" in a clean environment first.\n";
325 } else {
326 chomp(@ignored_vars = <IN>);
329 if($ENV{PARALLEL_IGNORED_NAMES}) {
330 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
331 chomp @ignored_vars;
333 $vars = join "|",map { quotemeta $_ } @ignored_vars;
334 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
335 ' -- "$@"
338 # Get the --env variables if set
339 # --env _ should be ignored
340 # and convert a b c to (a|b|c)
341 # If --env not set: Match everything (.*)
342 _make_grep_REGEXP() {
343 perl -e '
344 for(@ARGV){
345 /^_$/ and $next_is_env = 0;
346 $next_is_env and push @envvar, split/,/, $_;
347 $next_is_env = /^--env$/;
349 $vars = join "|",map { quotemeta $_ } @envvar;
350 print $vars ? "($vars)" : "(.*)";
351 ' -- "$@"
353 _which_PAR() {
354 # type returns:
355 # ll is an alias for ls -l (in ash)
356 # bash is a tracked alias for /bin/bash
357 # true is a shell builtin (in bash)
358 # myfunc is a function (in bash)
359 # myfunc is a shell function (in zsh)
360 # which is /usr/bin/which (in sh, bash)
361 # which is hashed (/usr/bin/which)
362 # gi is aliased to `grep -i' (in bash)
363 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
364 # Return 0 if found, 1 otherwise
365 LANG=C type "$@" |
366 perl -pe '$exit += (s/ is an alias for .*// ||
367 s/ is aliased to .*// ||
368 s/ is a function// ||
369 s/ is a shell function// ||
370 s/ is a shell builtin// ||
371 s/.* is hashed .(\S+).$/$1/ ||
372 s/.* is (a tracked alias for )?//);
373 END { exit not $exit }'
375 _warning_PAR() {
376 echo "env_parallel: Warning: $*" >&2
378 _error_PAR() {
379 echo "env_parallel: Error: $*" >&2
382 if _which_PAR parallel >/dev/null; then
383 true parallel found in path
384 else
385 # shellcheck disable=SC2016
386 _error_PAR 'parallel must be in $PATH.'
387 return 255
390 # Grep regexp for vars given by --env
391 # shellcheck disable=SC2006
392 _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
393 unset _make_grep_REGEXP
395 # Deal with --env _
396 # shellcheck disable=SC2006
397 _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
398 unset _get_ignored_VARS
400 # --record-env
401 if perl -e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
402 true skip
403 else
404 (_names_of_ALIASES;
405 _names_of_FUNCTIONS;
406 _names_of_VARIABLES) |
407 cat > "$HOME"/.parallel/ignored_vars
408 return 0
411 # --session
412 if perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
413 true skip
414 else
415 # Insert ::: between each level of session
416 # so you can pop off the last ::: at --end-session
417 # shellcheck disable=SC2006
418 PARALLEL_IGNORED_NAMES="`echo \"$PARALLEL_IGNORED_NAMES\";
419 echo :::;
420 (_names_of_ALIASES;
421 _names_of_FUNCTIONS;
422 _names_of_VARIABLES) | perl -ne '
423 BEGIN{
424 map { $ignored_vars{$_}++ }
425 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
427 chomp;
428 for(split/\s+/) {
429 if(not $ignored_vars{$_}) {
430 print $_,\"\\n\";
434 export PARALLEL_IGNORED_NAMES
435 return 0
437 if perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
438 true skip
439 else
440 # Pop off last ::: from PARALLEL_IGNORED_NAMES
441 # shellcheck disable=SC2006
442 PARALLEL_IGNORED_NAMES="`perl -e '
443 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
444 print $ENV{PARALLEL_IGNORED_NAMES}
446 return 0
448 # Grep alias names
449 # shellcheck disable=SC2006
450 _alias_NAMES="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
451 _list_alias_BODIES="_bodies_of_ALIASES $_alias_NAMES"
452 if [ "$_alias_NAMES" = "" ] ; then
453 # no aliases selected
454 _list_alias_BODIES="true"
456 unset _alias_NAMES
458 # Grep function names
459 # shellcheck disable=SC2006
460 _function_NAMES="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
461 _list_function_BODIES="_bodies_of_FUNCTIONS $_function_NAMES"
462 if [ "$_function_NAMES" = "" ] ; then
463 # no functions selected
464 _list_function_BODIES="true"
466 unset _function_NAMES
468 # Grep variable names
469 # shellcheck disable=SC2006
470 _variable_NAMES="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
471 _list_variable_VALUES="_bodies_of_VARIABLES $_variable_NAMES"
472 if [ "$_variable_NAMES" = "" ] ; then
473 # no variables selected
474 _list_variable_VALUES="true"
476 unset _variable_NAMES
478 if $eval_needed ; then
479 # shellcheck disable=SC2006
480 PARALLEL_ENV="`
481 eval $_list_alias_BODIES;
482 eval $_list_function_BODIES;
483 eval $_list_variable_VALUES;
485 else
486 # shellcheck disable=SC2006
487 PARALLEL_ENV="`
488 $_prefix_PARALLEL_ENV;
489 $_list_alias_BODIES;
490 $_list_function_BODIES;
491 $_list_variable_VALUES;
494 export PARALLEL_ENV
495 # Free up some env space
496 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
497 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
498 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
499 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
500 unset _remove_bad_NAMES _grep_REGEXP
501 unset _prefix_PARALLEL_ENV
502 # Test if environment is too big by running 'true'
503 # shellcheck disable=SC2006,SC2092
504 if `_which_PAR true` >/dev/null 2>/dev/null ; then
505 parallel "$@"
506 _parallel_exit_CODE=$?
507 # Clean up variables/functions
508 unset PARALLEL_ENV
509 unset _which_PAR _which_TRUE
510 unset _warning_PAR _error_PAR
511 # Unset _parallel_exit_CODE before return
512 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
513 else
514 unset PARALLEL_ENV;
515 _error_PAR "Your environment is too big."
516 _error_PAR "You can try 3 different approaches:"
517 _error_PAR "1. Run 'env_parallel --session' before you set"
518 _error_PAR " variables or define functions."
519 _error_PAR "2. Use --env and only mention the names to copy."
520 _error_PAR "3. Try running this in a clean environment once:"
521 _error_PAR " env_parallel --record-env"
522 _error_PAR " And then use '--env _'"
523 _error_PAR "For details see: man env_parallel"
524 return 255
528 parset() {
529 _parset_PARALLEL_PRG=parallel
530 _parset_main "$@"
533 env_parset() {
534 _parset_PARALLEL_PRG=env_parallel
535 _parset_main "$@"
538 _parset_main() {
539 # If $1 contains ',' or space:
540 # Split on , to get the destination variable names
541 # If $1 is a single destination variable name:
542 # Treat it as the name of an array
544 # # Create array named myvar
545 # parset myvar echo ::: {1..10}
546 # echo ${myvar[5]}
548 # # Put output into $var_a $var_b $var_c
549 # varnames=(var_a var_b var_c)
550 # parset "${varnames[*]}" echo ::: {1..3}
551 # echo $var_c
553 # # Put output into $var_a4 $var_b4 $var_c4
554 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
555 # echo $var_c4
557 _parset_NAME="$1"
558 if [ "$_parset_NAME" = "" ] ; then
559 echo parset: Error: No destination variable given. >&2
560 echo parset: Error: Try: >&2
561 echo parset: Error: ' ' parset myarray echo ::: foo bar >&2
562 return 255
564 if [ "$_parset_NAME" = "--help" ] ; then
565 echo parset: Error: Usage: >&2
566 echo parset: Error: ' ' parset varname GNU Parallel options and command >&2
567 echo
568 parallel --help
569 return 255
571 if [ "$_parset_NAME" = "--version" ] ; then
572 # shellcheck disable=SC2006
573 echo "parset 20240522 (GNU parallel `parallel --minversion 1`)"
574 echo "Copyright (C) 2007-2024 Ole Tange, http://ole.tange.dk and Free Software"
575 echo "Foundation, Inc."
576 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
577 echo "This is free software: you are free to change and redistribute it."
578 echo "GNU parallel comes with no warranty."
579 echo
580 echo "Web site: https://www.gnu.org/software/parallel"
581 echo
582 echo "When using programs that use GNU Parallel to process data for publication"
583 echo "please cite as described in 'parallel --citation'."
584 echo
585 return 255
587 shift
589 # Bash: declare -A myassoc=( )
590 # Zsh: typeset -A myassoc=( )
591 # Ksh: typeset -A myassoc=( )
592 # shellcheck disable=SC2039,SC2169
593 if (typeset -p "$_parset_NAME" 2>/dev/null; echo) |
594 perl -ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
595 # This is an associative array
596 # shellcheck disable=SC2006
597 eval "`$_parset_PARALLEL_PRG -k --_parset assoc,"$_parset_NAME" "$@"`"
598 # The eval returns the function!
599 else
600 # This is a normal array or a list of variable names
601 # shellcheck disable=SC2006
602 eval "`$_parset_PARALLEL_PRG -k --_parset var,"$_parset_NAME" "$@"`"
603 # The eval returns the function!