3 # This file must be sourced in ksh:
5 # source `which env_parallel.ksh`
7 # after which 'env_parallel' works
10 # Copyright (C) 2016-2021 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 Ole Tange, http://ole.tange.dk and Free Software and Foundation, Inc.
29 # SPDX-License-Identifier: GPL-3.0-or-later
35 alias | perl
-pe 's/=.*//'
37 _bodies_of_ALIASES
() {
38 alias "$@" | perl
-pe 's/^/alias /;
39 sub warning { print STDERR "env_parallel: Warning: @_\n"; }
40 if(/^alias (\S+)=\$.*\\n/) {
41 warning("Alias \"$1\" contains newline.");
42 warning("Make sure the command has at least one newline after \"$1\".");
43 warning("See BUGS in \"man env_parallel\".");
47 _names_of_maybe_FUNCTIONS
() {
50 _names_of_FUNCTIONS
() {
51 typeset
+p
-f | perl
-pe 's/\(\).*//'
53 _bodies_of_FUNCTIONS
() {
56 _names_of_VARIABLES
() {
57 typeset
+p | perl
-pe 's/^typeset .. //'
59 _bodies_of_VARIABLES
() {
63 # These names cannot be detected
64 echo '(_|TIMEOUT|IFS)'
67 readonly | perl
-e '@r = map {
69 # sh on UnixWare: readonly TIMEOUT
70 # ash: readonly var='val
'
73 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
74 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
75 # zsh: typeset -r var='val
'
76 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
78 $vars = join "|",map { quotemeta $_ } @r;
79 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
83 # Do not transfer vars and funcs from env_parallel
84 _ignore_RO
="`_ignore_READONLY`"
85 _ignore_HARD
="`_ignore_HARDCODED`"
86 # Macos-grep does not like long patterns
87 # Old Solaris grep does not support -E
89 # grep -Ev '^(...)$' |
107 _list_function_BODIES|
108 _list_variable_VALUES|
113 _names_of_maybe_FUNCTIONS|
115 _prefix_PARALLEL_ENV|
116 _prefix_PARALLEL_ENV|
121 _which_PAR)$/x and next;
122 # Filter names matching --env
123 /^'"$_grep_REGEXP"'$/ or next;
124 /^'"$_ignore_UNDERSCORE"'$/ and next;
125 # Remove readonly variables
126 /^'"$_ignore_RO"'$/ and next;
127 /^'"$_ignore_HARD"'$/ and next;
130 _get_ignored_VARS
() {
133 $next_is_env and push @envvar, split/,/, $_;
134 $next_is_env=/^--env$/;
136 if(grep { /^_$/ } @envvar) {
137 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
138 print STDERR "parallel: Error: ",
139 "Run \"parallel --record-env\" in a clean environment first.\n";
141 chomp(@ignored_vars = <IN>);
144 if($ENV{PARALLEL_IGNORED_NAMES}) {
145 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
148 $vars = join "|",map { quotemeta $_ } @ignored_vars;
149 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
153 # Get the --env variables if set
154 # --env _ should be ignored
155 # and convert a b c to (a|b|c)
156 # If --env not set: Match everything (.*)
157 _make_grep_REGEXP
() {
160 /^_$/ and $next_is_env = 0;
161 $next_is_env and push @envvar, split/,/, $_;
162 $next_is_env = /^--env$/;
164 $vars = join "|",map { quotemeta $_ } @envvar;
165 print $vars ? "($vars)" : "(.*)";
170 # ll is an alias for ls -l (in ash)
171 # bash is a tracked alias for /bin/bash
172 # true is a shell builtin (in bash)
173 # myfunc is a function (in bash)
174 # myfunc is a shell function (in zsh)
175 # which is /usr/bin/which (in sh, bash)
176 # which is hashed (/usr/bin/which)
177 # gi is aliased to `grep -i' (in bash)
178 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
179 # Return 0 if found, 1 otherwise
181 perl
-pe '$exit += (s/ is an alias for .*// ||
182 s/ is aliased to .*// ||
183 s/ is a function// ||
184 s/ is a shell function// ||
185 s/ is a shell builtin// ||
186 s/.* is hashed .(\S+).$/$1/ ||
187 s/.* is (a tracked alias for )?//);
188 END { exit not $exit }'
191 echo "env_parallel: Warning: $@" >&2
194 echo "env_parallel: Error: $@" >&2
197 if _which_PAR parallel
>/dev
/null
; then
198 true parallel found
in path
200 _error_PAR
'parallel must be in $PATH.'
204 # Grep regexp for vars given by --env
205 _grep_REGEXP
="`_make_grep_REGEXP \"$@\"`"
206 unset _make_grep_REGEXP
209 _ignore_UNDERSCORE
="`_get_ignored_VARS \"$@\"`"
210 unset _get_ignored_VARS
213 if perl
-e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
218 _names_of_VARIABLES
) |
219 cat > "$HOME"/.parallel
/ignored_vars
224 if perl
-e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
227 # Insert ::: between each level of session
228 # so you can pop off the last ::: at --end-session
229 PARALLEL_IGNORED_NAMES
="`echo \"$PARALLEL_IGNORED_NAMES\";
233 _names_of_VARIABLES) | perl -ne '
235 map { $ignored_vars{$_}++ }
236 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
240 if(not $ignored_vars{$_}) {
245 export PARALLEL_IGNORED_NAMES
248 if perl
-e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
251 # Pop off last ::: from PARALLEL_IGNORED_NAMES
252 PARALLEL_IGNORED_NAMES
="`perl -e '
253 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
254 print $ENV{PARALLEL_IGNORED_NAMES}
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 _function_NAMES
="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
269 _list_function_BODIES
="_bodies_of_FUNCTIONS $_function_NAMES"
270 if [ "$_function_NAMES" = "" ] ; then
271 # no functions selected
272 _list_function_BODIES
="true"
274 unset _function_NAMES
276 # Grep variable names
277 _variable_NAMES
="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
278 _list_variable_VALUES
="_bodies_of_VARIABLES $_variable_NAMES"
279 if [ "$_variable_NAMES" = "" ] ; then
280 # no variables selected
281 _list_variable_VALUES
="true"
283 unset _variable_NAMES
287 $_list_function_BODIES;
288 $_list_variable_VALUES;
291 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
292 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
293 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
294 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
295 unset _remove_bad_NAMES _grep_REGEXP
296 unset _prefix_PARALLEL_ENV
297 # Test if environment is too big
298 if `_which_PAR true` >/dev
/null
2>/dev
/null
; then
300 _parallel_exit_CODE
=$?
301 # Clean up variables/functions
303 unset _which_PAR _which_TRUE
304 unset _warning_PAR _error_PAR
305 # Unset _parallel_exit_CODE before return
306 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
309 _error_PAR
"Your environment is too big."
310 _error_PAR
"You can try 3 different approaches:"
311 _error_PAR
"1. Run 'env_parallel --session' before you set"
312 _error_PAR
" variables or define functions."
313 _error_PAR
"2. Use --env and only mention the names to copy."
314 _error_PAR
"3. Try running this in a clean environment once:"
315 _error_PAR
" env_parallel --record-env"
316 _error_PAR
" And then use '--env _'"
317 _error_PAR
"For details see: man env_parallel"
323 _parset_PARALLEL_PRG
=parallel
328 _parset_PARALLEL_PRG
=env_parallel
333 # If $1 contains ',' or space:
334 # Split on , to get the destination variable names
335 # If $1 is a single destination variable name:
336 # Treat it as the name of an array
338 # # Create array named myvar
339 # parset myvar echo ::: {1..10}
342 # # Put output into $var_a $var_b $var_c
343 # varnames=(var_a var_b var_c)
344 # parset "${varnames[*]}" echo ::: {1..3}
347 # # Put output into $var_a4 $var_b4 $var_c4
348 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
352 if [ "$_parset_NAME" = "" ] ; then
353 echo parset
: Error
: No destination variable given.
>&2
354 echo parset
: Error
: Try
: >&2
355 echo parset
: Error
: ' ' parset myarray
echo ::: foo bar
>&2
358 if [ "$_parset_NAME" = "--help" ] ; then
359 echo parset
: Error
: Usage
: >&2
360 echo parset
: Error
: ' ' parset varname GNU Parallel options and
command >&2
365 if [ "$_parset_NAME" = "--version" ] ; then
366 echo "parset 20210922 (GNU parallel `parallel --minversion 1`)"
367 echo "Copyright (C) 2007-2021 Ole Tange, http://ole.tange.dk and Free Software"
368 echo "Foundation, Inc."
369 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
370 echo "This is free software: you are free to change and redistribute it."
371 echo "GNU parallel comes with no warranty."
373 echo "Web site: https://www.gnu.org/software/parallel"
375 echo "When using programs that use GNU Parallel to process data for publication"
376 echo "please cite as described in 'parallel --citation'."
382 # Bash: declare -A myassoc=( )
383 # Zsh: typeset -A myassoc=( )
384 # Ksh: typeset -A myassoc=( )
385 if (typeset
-p "$_parset_NAME" 2>/dev
/null
; echo) |
386 perl
-ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
387 # This is an associative array
388 eval "`$_parset_PARALLEL_PRG -k --parset assoc,"$_parset_NAME" "$@
"`"
389 # The eval returns the function!
391 # This is a normal array or a list of variable names
392 eval "`$_parset_PARALLEL_PRG -k --parset var,"$_parset_NAME" "$@
"`"
393 # The eval returns the function!