3 # This file must be sourced in mksh:
5 # source `which env_parallel.mksh`
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
() {
53 _bodies_of_FUNCTIONS
() {
56 _names_of_VARIABLES
() {
58 perl
-pe 's/^(type)?set( [-+][a-zA-Z0-9]*)* //; s/(\[\d+\])?=.*//' |
61 _bodies_of_VARIABLES
() {
65 # These names cannot be detected
69 readonly | perl
-e '@r = map {
71 # sh on UnixWare: readonly TIMEOUT
72 # ash: readonly var='val
'
75 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
76 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
77 # zsh: typeset -r var='val
'
78 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
80 $vars = join "|",map { quotemeta $_ } @r;
81 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
85 # Do not transfer vars and funcs from env_parallel
86 _ignore_RO
="`_ignore_READONLY`"
87 _ignore_HARD
="`_ignore_HARDCODED`"
88 # Macos-grep does not like long patterns
89 # Old Solaris grep does not support -E
91 # grep -Ev '^(...)$' |
109 _list_function_BODIES|
110 _list_variable_VALUES|
115 _names_of_maybe_FUNCTIONS|
117 _prefix_PARALLEL_ENV|
118 _prefix_PARALLEL_ENV|
123 _which_PAR)$/x and next;
124 # Filter names matching --env
125 /^'"$_grep_REGEXP"'$/ or next;
126 /^'"$_ignore_UNDERSCORE"'$/ and next;
127 # Remove readonly variables
128 /^'"$_ignore_RO"'$/ and next;
129 /^'"$_ignore_HARD"'$/ and next;
132 _get_ignored_VARS
() {
135 $next_is_env and push @envvar, split/,/, $_;
136 $next_is_env=/^--env$/;
138 if(grep { /^_$/ } @envvar) {
139 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
140 print STDERR "parallel: Error: ",
141 "Run \"parallel --record-env\" in a clean environment first.\n";
143 chomp(@ignored_vars = <IN>);
146 if($ENV{PARALLEL_IGNORED_NAMES}) {
147 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
150 $vars = join "|",map { quotemeta $_ } @ignored_vars;
151 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
155 # Get the --env variables if set
156 # --env _ should be ignored
157 # and convert a b c to (a|b|c)
158 # If --env not set: Match everything (.*)
159 _make_grep_REGEXP
() {
162 /^_$/ and $next_is_env = 0;
163 $next_is_env and push @envvar, split/,/, $_;
164 $next_is_env = /^--env$/;
166 $vars = join "|",map { quotemeta $_ } @envvar;
167 print $vars ? "($vars)" : "(.*)";
172 # ll is an alias for ls -l (in ash)
173 # bash is a tracked alias for /bin/bash
174 # true is a shell builtin (in bash)
175 # myfunc is a function (in bash)
176 # myfunc is a shell function (in zsh)
177 # which is /usr/bin/which (in sh, bash)
178 # which is hashed (/usr/bin/which)
179 # gi is aliased to `grep -i' (in bash)
180 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
181 # Return 0 if found, 1 otherwise
183 perl
-pe '$exit += (s/ is an alias for .*// ||
184 s/ is aliased to .*// ||
185 s/ is a function// ||
186 s/ is a shell function// ||
187 s/ is a shell builtin// ||
188 s/.* is hashed .(\S+).$/$1/ ||
189 s/.* is (a tracked alias for )?//);
190 END { exit not $exit }'
193 echo "env_parallel: Warning: $@" >&2
196 echo "env_parallel: Error: $@" >&2
199 if _which_PAR parallel
>/dev
/null
; then
200 true parallel found
in path
202 _error_PAR
'parallel must be in $PATH.'
206 # Grep regexp for vars given by --env
207 _grep_REGEXP
="`_make_grep_REGEXP \"$@\"`"
208 unset _make_grep_REGEXP
211 _ignore_UNDERSCORE
="`_get_ignored_VARS \"$@\"`"
212 unset _get_ignored_VARS
215 if perl
-e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
220 _names_of_VARIABLES
) |
221 cat > "$HOME"/.parallel
/ignored_vars
226 if perl
-e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
229 # Insert ::: between each level of session
230 # so you can pop off the last ::: at --end-session
231 PARALLEL_IGNORED_NAMES
="`echo \"$PARALLEL_IGNORED_NAMES\";
235 _names_of_VARIABLES) | perl -ne '
237 map { $ignored_vars{$_}++ }
238 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
242 if(not $ignored_vars{$_}) {
247 export PARALLEL_IGNORED_NAMES
250 if perl
-e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
253 # Pop off last ::: from PARALLEL_IGNORED_NAMES
254 PARALLEL_IGNORED_NAMES
="`perl -e '
255 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
256 print $ENV{PARALLEL_IGNORED_NAMES}
261 _alias_NAMES
="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
262 _list_alias_BODIES
="_bodies_of_ALIASES $_alias_NAMES"
263 if [ "$_alias_NAMES" = "" ] ; then
264 # no aliases selected
265 _list_alias_BODIES
="true"
269 # Grep function names
270 _function_NAMES
="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
271 _list_function_BODIES
="_bodies_of_FUNCTIONS $_function_NAMES"
272 if [ "$_function_NAMES" = "" ] ; then
273 # no functions selected
274 _list_function_BODIES
="true"
276 unset _function_NAMES
278 # Grep variable names
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
289 $_list_function_BODIES;
290 $_list_variable_VALUES;
293 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
294 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
295 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
296 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
297 unset _remove_bad_NAMES _grep_REGEXP
298 unset _prefix_PARALLEL_ENV
299 # Test if environment is too big
300 if `_which_PAR true` >/dev
/null
2>/dev
/null
; then
302 _parallel_exit_CODE
=$?
303 # Clean up variables/functions
305 unset _which_PAR _which_TRUE
306 unset _warning_PAR _error_PAR
307 # Unset _parallel_exit_CODE before return
308 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
311 _error_PAR
"Your environment is too big."
312 _error_PAR
"You can try 3 different approaches:"
313 _error_PAR
"1. Run 'env_parallel --session' before you set"
314 _error_PAR
" variables or define functions."
315 _error_PAR
"2. Use --env and only mention the names to copy."
316 _error_PAR
"3. Try running this in a clean environment once:"
317 _error_PAR
" env_parallel --record-env"
318 _error_PAR
" And then use '--env _'"
319 _error_PAR
"For details see: man env_parallel"
325 _parset_PARALLEL_PRG
=parallel
330 _parset_PARALLEL_PRG
=env_parallel
335 # If $1 contains ',' or space:
336 # Split on , to get the destination variable names
337 # If $1 is a single destination variable name:
338 # Treat it as the name of an array
340 # # Create array named myvar
341 # parset myvar echo ::: {1..10}
344 # # Put output into $var_a $var_b $var_c
345 # varnames=(var_a var_b var_c)
346 # parset "${varnames[*]}" echo ::: {1..3}
349 # # Put output into $var_a4 $var_b4 $var_c4
350 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
354 if [ "$_parset_NAME" = "" ] ; then
355 echo parset
: Error
: No destination variable given.
>&2
356 echo parset
: Error
: Try
: >&2
357 echo parset
: Error
: ' ' parset myarray
echo ::: foo bar
>&2
360 if [ "$_parset_NAME" = "--help" ] ; then
361 echo parset
: Error
: Usage
: >&2
362 echo parset
: Error
: ' ' parset varname GNU Parallel options and
command >&2
367 if [ "$_parset_NAME" = "--version" ] ; then
368 echo "parset 20210922 (GNU parallel `parallel --minversion 1`)"
369 echo "Copyright (C) 2007-2021 Ole Tange, http://ole.tange.dk and Free Software"
370 echo "Foundation, Inc."
371 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
372 echo "This is free software: you are free to change and redistribute it."
373 echo "GNU parallel comes with no warranty."
375 echo "Web site: https://www.gnu.org/software/parallel"
377 echo "When using programs that use GNU Parallel to process data for publication"
378 echo "please cite as described in 'parallel --citation'."
384 # Bash: declare -A myassoc=( )
385 # Zsh: typeset -A myassoc=( )
386 # Ksh: typeset -A myassoc=( )
387 if (typeset
-p "$_parset_NAME" 2>/dev
/null
; echo) |
388 perl
-ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
389 # This is an associative array
390 eval "`$_parset_PARALLEL_PRG -k --parset assoc,"$_parset_NAME" "$@
"`"
391 # The eval returns the function!
393 # This is a normal array or a list of variable names
394 eval "`$_parset_PARALLEL_PRG -k --parset var,"$_parset_NAME" "$@
"`"
395 # The eval returns the function!