3 # This file must be sourced in bash:
5 # source `which env_parallel.bash`
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
36 # No aliases will return false. This error should be ignored.
39 _bodies_of_ALIASES
() {
42 # shellcheck disable=SC2046
43 if [ $
(alias "$_i" |
wc -l) == 1 ] ; then
44 true Alias is a single line. Good.
46 _warning_PAR
"Alias '$_i' contains newline."
47 _warning_PAR
"Make sure the command has at least one newline after '$_i'."
48 _warning_PAR
"See BUGS in 'man env_parallel'."
53 _names_of_FUNCTIONS
() {
56 _bodies_of_FUNCTIONS
() {
59 _names_of_VARIABLES
() {
62 _bodies_of_VARIABLES
() {
66 # Copying $RANDOM will cause it not to be random
67 # The rest cannot be detected as read-only
68 echo '(RANDOM|_|TIMEOUT|GROUPS|FUNCNAME|DIRSTACK|PIPESTATUS|USERNAME|BASHPID|BASH_[A-Z_]+)'
71 # shellcheck disable=SC1078,SC1079,SC2026
72 readonly | perl
-e '@r = map {
74 # sh on UnixWare: readonly TIMEOUT
75 # ash: readonly var='val
'
78 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
79 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
80 # zsh: typeset -r var='val
'
81 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
83 $vars = join "|",map { quotemeta $_ } @r;
84 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
88 # Do not transfer vars and funcs from env_parallel
89 # shellcheck disable=SC2006
90 _ignore_RO
="`_ignore_READONLY`"
91 # shellcheck disable=SC2006
92 _ignore_HARD
="`_ignore_HARDCODED`"
93 # Macos-grep does not like long patterns
94 # Old Solaris grep does not support -E
96 # grep -Ev '^(...)$' |
102 _bodies_of_FUNCTIONS|
103 _bodies_of_VARIABLES|
114 _list_function_BODIES|
115 _list_variable_VALUES|
120 _names_of_maybe_FUNCTIONS|
122 _prefix_PARALLEL_ENV|
123 _prefix_PARALLEL_ENV|
128 _which_PAR)$/x and next;
129 # Filter names matching --env
130 /^'"$_grep_REGEXP"'$/ or next;
131 /^'"$_ignore_UNDERSCORE"'$/ and next;
132 # Remove readonly variables
133 /^'"$_ignore_RO"'$/ and next;
134 /^'"$_ignore_HARD"'$/ and next;
137 _prefix_PARALLEL_ENV
() {
139 perl
-pe 's:\s+off:;: and s/^/shopt -u /;
140 s:\s+on:;: and s/^/shopt -s /;
142 echo 'shopt -s expand_aliases &>/dev/null';
145 _get_ignored_VARS
() {
148 $next_is_env and push @envvar, split/,/, $_;
149 $next_is_env=/^--env$/;
151 if(grep { /^_$/ } @envvar) {
152 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
153 print STDERR "parallel: Error: ",
154 "Run \"parallel --record-env\" in a clean environment first.\n";
156 chomp(@ignored_vars = <IN>);
159 if($ENV{PARALLEL_IGNORED_NAMES}) {
160 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
163 $vars = join "|",map { quotemeta $_ } @ignored_vars;
164 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
168 # Get the --env variables if set
169 # --env _ should be ignored
170 # and convert a b c to (a|b|c)
171 # If --env not set: Match everything (.*)
172 _make_grep_REGEXP
() {
175 /^_$/ and $next_is_env = 0;
176 $next_is_env and push @envvar, split/,/, $_;
177 $next_is_env = /^--env$/;
179 $vars = join "|",map { quotemeta $_ } @envvar;
180 print $vars ? "($vars)" : "(.*)";
185 # ll is an alias for ls -l (in ash)
186 # bash is a tracked alias for /bin/bash
187 # true is a shell builtin (in bash)
188 # myfunc is a function (in bash)
189 # myfunc is a shell function (in zsh)
190 # which is /usr/bin/which (in sh, bash)
191 # which is hashed (/usr/bin/which)
192 # gi is aliased to `grep -i' (in bash)
193 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
194 # Return 0 if found, 1 otherwise
196 perl
-pe '$exit += (s/ is an alias for .*// ||
197 s/ is aliased to .*// ||
198 s/ is a function// ||
199 s/ is a shell function// ||
200 s/ is a shell builtin// ||
201 s/.* is hashed .(\S+).$/$1/ ||
202 s/.* is (a tracked alias for )?//);
203 END { exit not $exit }'
206 echo "env_parallel: Warning: $*" >&2
209 echo "env_parallel: Error: $*" >&2
212 # Bash is broken in version 3.2.25 and 4.2.39
213 # The crazy '[ "`...`" == "" ]' is needed for the same reason
214 if [ "`_which_PAR parallel`" == "" ]; then
215 # shellcheck disable=SC2016
216 _error_PAR
'parallel must be in $PATH.'
220 # Grep regexp for vars given by --env
221 # shellcheck disable=SC2006
222 _grep_REGEXP
="`_make_grep_REGEXP \"$@\"`"
223 unset _make_grep_REGEXP
226 # shellcheck disable=SC2006
227 _ignore_UNDERSCORE
="`_get_ignored_VARS \"$@\"`"
228 unset _get_ignored_VARS
231 # Bash is broken in version 3.2.25 and 4.2.39
232 # The crazy '[ "`...`" == 0 ]' is needed for the same reason
233 if [ "`perl -e 'exit grep { /^--record-env$/ } @ARGV' -- "$@
"; echo $?`" == 0 ] ; then
238 _names_of_VARIABLES
) |
239 cat > "$HOME"/.parallel
/ignored_vars
244 # Bash is broken in version 3.2.25 and 4.2.39
245 # The crazy '[ "`...`" == 0 ]' is needed for the same reason
246 if [ "`perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@
"; echo $?`" == 0 ] ; then
249 # Insert ::: between each level of session
250 # so you can pop off the last ::: at --end-session
251 # shellcheck disable=SC2006
252 PARALLEL_IGNORED_NAMES
="`echo \"$PARALLEL_IGNORED_NAMES\";
256 _names_of_VARIABLES) | perl -ne '
258 map { $ignored_vars{$_}++ }
259 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
263 if(not $ignored_vars{$_}) {
268 export PARALLEL_IGNORED_NAMES
271 # Bash is broken in version 3.2.25 and 4.2.39
272 # The crazy '[ "`...`" == 0 ]' is needed for the same reason
273 if [ "`perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@
"; echo $?`" == 0 ] ; then
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}
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"
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 _which_TRUE
="`_which_PAR true`"
315 # Copy shopt (so e.g. extended globbing works)
316 # But force expand_aliases as aliases otherwise do not work
320 $_list_function_BODIES;
321 $_list_variable_VALUES;
324 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
325 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
326 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
327 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
328 unset _remove_bad_NAMES _grep_REGEXP
329 unset _prefix_PARALLEL_ENV
330 # Test if environment is too big
331 if [ "`_which_PAR true`" == "$_which_TRUE" ] ; then
333 _parallel_exit_CODE
=$?
334 # Clean up variables/functions
336 unset _which_PAR _which_TRUE
337 unset _warning_PAR _error_PAR
338 # Unset _parallel_exit_CODE before return
339 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
342 _error_PAR
"Your environment is too big."
343 _error_PAR
"You can try 3 different approaches:"
344 _error_PAR
"1. Run 'env_parallel --session' before you set"
345 _error_PAR
" variables or define functions."
346 _error_PAR
"2. Use --env and only mention the names to copy."
347 _error_PAR
"3. Try running this in a clean environment once:"
348 _error_PAR
" env_parallel --record-env"
349 _error_PAR
" And then use '--env _'"
350 _error_PAR
"For details see: man env_parallel"
356 _parset_PARALLEL_PRG
=parallel
361 _parset_PARALLEL_PRG
=env_parallel
366 # If $1 contains ',' or space:
367 # Split on , to get the destination variable names
368 # If $1 is a single destination variable name:
369 # Treat it as the name of an array
371 # # Create array named myvar
372 # parset myvar echo ::: {1..10}
375 # # Put output into $var_a $var_b $var_c
376 # varnames=(var_a var_b var_c)
377 # parset "${varnames[*]}" echo ::: {1..3}
380 # # Put output into $var_a4 $var_b4 $var_c4
381 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
385 if [ "$_parset_NAME" = "" ] ; then
386 echo parset
: Error
: No destination variable given.
>&2
387 echo parset
: Error
: Try
: >&2
388 echo parset
: Error
: ' ' parset myarray
echo ::: foo bar
>&2
391 if [ "$_parset_NAME" = "--help" ] ; then
392 echo parset
: Error
: Usage
: >&2
393 echo parset
: Error
: ' ' parset varname GNU Parallel options and
command >&2
398 if [ "$_parset_NAME" = "--version" ] ; then
399 # shellcheck disable=SC2006
400 echo "parset 20221022 (GNU parallel `parallel --minversion 1`)"
401 echo "Copyright (C) 2007-2022 Ole Tange, http://ole.tange.dk and Free Software"
402 echo "Foundation, Inc."
403 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
404 echo "This is free software: you are free to change and redistribute it."
405 echo "GNU parallel comes with no warranty."
407 echo "Web site: https://www.gnu.org/software/parallel"
409 echo "When using programs that use GNU Parallel to process data for publication"
410 echo "please cite as described in 'parallel --citation'."
416 # Bash: declare -A myassoc=( )
417 # Zsh: typeset -A myassoc=( )
418 # Ksh: typeset -A myassoc=( )
419 # shellcheck disable=SC2039,SC2169
420 if (typeset
-p "$_parset_NAME" 2>/dev
/null
; echo) |
421 perl
-ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
422 # This is an associative array
423 # shellcheck disable=SC2006
424 eval "`$_parset_PARALLEL_PRG -k --_parset assoc,"$_parset_NAME" "$@
"`"
425 # The eval returns the function!
427 # This is a normal array or a list of variable names
428 # shellcheck disable=SC2006
429 eval "`$_parset_PARALLEL_PRG -k --_parset var,"$_parset_NAME" "$@
"`"
430 # The eval returns the function!