3 # This file must be sourced in zsh:
5 # source =env_parallel.zsh
7 # after which 'env_parallel' works
10 # Copyright (C) 2016-2023 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-2023 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
36 print
-l ${(k)aliases}
38 _bodies_of_ALIASES
() {
41 echo 'alias '"$(alias $_i)"
44 _names_of_FUNCTIONS
() {
45 print
-l ${(k)functions}
47 _bodies_of_FUNCTIONS
() {
50 _names_of_VARIABLES
() {
51 print
-l ${(k)parameters}
53 _bodies_of_VARIABLES
() {
57 # These names cannot be detected
58 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)'
61 typeset
-pr | perl
-e '@r = map {
63 # sh on UnixWare: readonly TIMEOUT
64 # ash: readonly var='val
'
67 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
68 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
69 # zsh: typeset -r var='val
'
70 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
72 $vars = join "|",map { quotemeta $_ } @r;
73 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
77 # Do not transfer vars and funcs from env_parallel
78 # shellcheck disable=SC2006
79 _ignore_RO
="`_ignore_READONLY`"
80 # shellcheck disable=SC2006
81 _ignore_HARD
="`_ignore_HARDCODED`"
82 # To avoid depending on grep dialect, use Perl version of:
83 # grep -Ev '^(...)$' |
101 _list_function_BODIES|
102 _list_variable_VALUES|
107 _names_of_maybe_FUNCTIONS|
109 _prefix_PARALLEL_ENV|
110 _prefix_PARALLEL_ENV|
115 _which_PAR)$/x and next;
116 # Filter names matching --env
117 /^'"$_grep_REGEXP"'$/ or next;
118 /^'"$_ignore_UNDERSCORE"'$/ and next;
119 # Remove readonly variables
120 /^'"$_ignore_RO"'$/ and next;
121 /^'"$_ignore_HARD"'$/ and next;
124 _get_ignored_VARS
() {
127 $next_is_env and push @envvar, split/,/, $_;
128 $next_is_env=/^--env$/;
130 if(grep { /^_$/ } @envvar) {
131 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
132 print STDERR "parallel: Error: ",
133 "Run \"parallel --record-env\" in a clean environment first.\n";
135 chomp(@ignored_vars = <IN>);
138 if($ENV{PARALLEL_IGNORED_NAMES}) {
139 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
142 $vars = join "|",map { quotemeta $_ } @ignored_vars;
143 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
147 # Get the --env variables if set
148 # --env _ should be ignored
149 # and convert a b c to (a|b|c)
150 # If --env not set: Match everything (.*)
151 _make_grep_REGEXP
() {
154 /^_$/ and $next_is_env = 0;
155 $next_is_env and push @envvar, split/,/, $_;
156 $next_is_env = /^--env$/;
158 $vars = join "|",map { quotemeta $_ } @envvar;
159 print $vars ? "($vars)" : "(.*)";
164 # ll is an alias for ls -l (in ash)
165 # bash is a tracked alias for /bin/bash
166 # true is a shell builtin (in bash)
167 # myfunc is a function (in bash)
168 # myfunc is a shell function (in zsh)
169 # which is /usr/bin/which (in sh, bash)
170 # which is hashed (/usr/bin/which)
171 # gi is aliased to `grep -i' (in bash)
172 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
173 # Return 0 if found, 1 otherwise
175 perl
-pe '$exit += (s/ is an alias for .*// ||
176 s/ is aliased to .*// ||
177 s/ is a function// ||
178 s/ is a shell function// ||
179 s/ is a shell builtin// ||
180 s/.* is hashed .(\S+).$/$1/ ||
181 s/.* is (a tracked alias for )?//);
182 END { exit not $exit }'
185 echo "env_parallel: Warning: $*" >&2
188 echo "env_parallel: Error: $*" >&2
191 if _which_PAR parallel
>/dev
/null
; then
192 true parallel found
in path
194 # shellcheck disable=SC2016
195 _error_PAR
'parallel must be in $PATH.'
199 # Grep regexp for vars given by --env
200 # shellcheck disable=SC2006
201 _grep_REGEXP
="`_make_grep_REGEXP \"$@\"`"
202 unset _make_grep_REGEXP
205 # shellcheck disable=SC2006
206 _ignore_UNDERSCORE
="`_get_ignored_VARS \"$@\"`"
207 unset _get_ignored_VARS
210 if perl
-e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
215 _names_of_VARIABLES
) |
216 cat > "$HOME"/.parallel
/ignored_vars
221 if perl
-e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
224 # Insert ::: between each level of session
225 # so you can pop off the last ::: at --end-session
226 # shellcheck disable=SC2006
227 PARALLEL_IGNORED_NAMES
="`echo \"$PARALLEL_IGNORED_NAMES\";
231 _names_of_VARIABLES) | perl -ne '
233 map { $ignored_vars{$_}++ }
234 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
238 if(not $ignored_vars{$_}) {
243 export PARALLEL_IGNORED_NAMES
246 if perl
-e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
249 # Pop off last ::: from PARALLEL_IGNORED_NAMES
250 # shellcheck disable=SC2006
251 PARALLEL_IGNORED_NAMES
="`perl -e '
252 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
253 print $ENV{PARALLEL_IGNORED_NAMES}
258 # shellcheck disable=SC2006
259 _alias_NAMES
="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
260 _list_alias_BODIES
="_bodies_of_ALIASES $_alias_NAMES"
261 if [ "$_alias_NAMES" = "" ] ; then
262 # no aliases selected
263 _list_alias_BODIES
="true"
267 # Grep function names
268 # shellcheck disable=SC2006
269 _function_NAMES
="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
270 _list_function_BODIES
="_bodies_of_FUNCTIONS $_function_NAMES"
271 if [ "$_function_NAMES" = "" ] ; then
272 # no functions selected
273 _list_function_BODIES
="true"
275 unset _function_NAMES
277 # Grep variable names
278 # shellcheck disable=SC2006
279 _variable_NAMES
="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
280 _list_variable_VALUES
="_bodies_of_VARIABLES $_variable_NAMES"
281 if [ "$_variable_NAMES" = "" ] ; then
282 # no variables selected
283 _list_variable_VALUES
="true"
285 unset _variable_NAMES
287 # shellcheck disable=SC2006
289 eval $_list_alias_BODIES;
290 eval $_list_function_BODIES;
291 eval $_list_variable_VALUES;
294 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
295 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
296 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
297 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
298 unset _remove_bad_NAMES _grep_REGEXP
299 unset _prefix_PARALLEL_ENV
300 # Test if environment is too big by running 'true'
301 # shellcheck disable=SC2006,SC2092
302 if `_which_PAR true` >/dev
/null
2>/dev
/null
; then
304 _parallel_exit_CODE
=$?
305 # Clean up variables/functions
307 unset _which_PAR _which_TRUE
308 unset _warning_PAR _error_PAR
309 # Unset _parallel_exit_CODE before return
310 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
313 _error_PAR
"Your environment is too big."
314 _error_PAR
"You can try 3 different approaches:"
315 _error_PAR
"1. Run 'env_parallel --session' before you set"
316 _error_PAR
" variables or define functions."
317 _error_PAR
"2. Use --env and only mention the names to copy."
318 _error_PAR
"3. Try running this in a clean environment once:"
319 _error_PAR
" env_parallel --record-env"
320 _error_PAR
" And then use '--env _'"
321 _error_PAR
"For details see: man env_parallel"
327 _parset_PARALLEL_PRG
=parallel
332 _parset_PARALLEL_PRG
=env_parallel
337 # If $1 contains ',' or space:
338 # Split on , to get the destination variable names
339 # If $1 is a single destination variable name:
340 # Treat it as the name of an array
342 # # Create array named myvar
343 # parset myvar echo ::: {1..10}
346 # # Put output into $var_a $var_b $var_c
347 # varnames=(var_a var_b var_c)
348 # parset "${varnames[*]}" echo ::: {1..3}
351 # # Put output into $var_a4 $var_b4 $var_c4
352 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
356 if [ "$_parset_NAME" = "" ] ; then
357 echo parset
: Error
: No destination variable given.
>&2
358 echo parset
: Error
: Try
: >&2
359 echo parset
: Error
: ' ' parset myarray
echo ::: foo bar
>&2
362 if [ "$_parset_NAME" = "--help" ] ; then
363 echo parset
: Error
: Usage
: >&2
364 echo parset
: Error
: ' ' parset varname GNU Parallel options and
command >&2
369 if [ "$_parset_NAME" = "--version" ] ; then
370 # shellcheck disable=SC2006
371 echo "parset 20230522 (GNU parallel `parallel --minversion 1`)"
372 echo "Copyright (C) 2007-2023 Ole Tange, http://ole.tange.dk and Free Software"
373 echo "Foundation, Inc."
374 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
375 echo "This is free software: you are free to change and redistribute it."
376 echo "GNU parallel comes with no warranty."
378 echo "Web site: https://www.gnu.org/software/parallel"
380 echo "When using programs that use GNU Parallel to process data for publication"
381 echo "please cite as described in 'parallel --citation'."
387 # Bash: declare -A myassoc=( )
388 # Zsh: typeset -A myassoc=( )
389 # Ksh: typeset -A myassoc=( )
390 # shellcheck disable=SC2039,SC2169
391 if (typeset
-p "$_parset_NAME" 2>/dev
/null
; echo) |
392 perl
-ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
393 # This is an associative array
394 # shellcheck disable=SC2006
395 eval "`$_parset_PARALLEL_PRG -k --_parset assoc,"$_parset_NAME" "$@
"`"
396 # The eval returns the function!
398 # This is a normal array or a list of variable names
399 # shellcheck disable=SC2006
400 eval "`$_parset_PARALLEL_PRG -k --_parset var,"$_parset_NAME" "$@
"`"
401 # The eval returns the function!