Released as 20221022 ('Nord Stream')
[parallel.git] / src / env_parallel.bash
blob757b2f19e300ac5b977b90dd52cfbc9e7418bfc8
1 #!/usr/bin/env bash
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
32 env_parallel() {
33 # env_parallel.bash
35 _names_of_ALIASES() {
36 # No aliases will return false. This error should be ignored.
37 compgen -a || true
39 _bodies_of_ALIASES() {
40 local _i
41 for _i in "$@"; do
42 # shellcheck disable=SC2046
43 if [ $(alias "$_i" | wc -l) == 1 ] ; then
44 true Alias is a single line. Good.
45 else
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'."
50 done
51 alias "$@"
53 _names_of_FUNCTIONS() {
54 compgen -A function
56 _bodies_of_FUNCTIONS() {
57 typeset -f "$@"
59 _names_of_VARIABLES() {
60 compgen -A variable
62 _bodies_of_VARIABLES() {
63 typeset -p "$@"
65 _ignore_HARDCODED() {
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_]+)'
70 _ignore_READONLY() {
71 # shellcheck disable=SC1078,SC1079,SC2026
72 readonly | perl -e '@r = map {
73 chomp;
74 # sh on UnixWare: readonly TIMEOUT
75 # ash: readonly var='val'
76 # ksh: var='val'
77 # mksh: PIPESTATUS[0]
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/;
82 $_ } <>;
83 $vars = join "|",map { quotemeta $_ } @r;
84 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
87 _remove_bad_NAMES() {
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
95 # Perl Version of:
96 # grep -Ev '^(...)$' |
97 perl -ne '/^(
98 PARALLEL_ENV|
99 PARALLEL_TMP|
100 _alias_NAMES|
101 _bodies_of_ALIASES|
102 _bodies_of_FUNCTIONS|
103 _bodies_of_VARIABLES|
104 _error_PAR|
105 _function_NAMES|
106 _get_ignored_VARS|
107 _grep_REGEXP|
108 _ignore_HARD|
109 _ignore_HARDCODED|
110 _ignore_READONLY|
111 _ignore_RO|
112 _ignore_UNDERSCORE|
113 _list_alias_BODIES|
114 _list_function_BODIES|
115 _list_variable_VALUES|
116 _make_grep_REGEXP|
117 _names_of_ALIASES|
118 _names_of_FUNCTIONS|
119 _names_of_VARIABLES|
120 _names_of_maybe_FUNCTIONS|
121 _parallel_exit_CODE|
122 _prefix_PARALLEL_ENV|
123 _prefix_PARALLEL_ENV|
124 _remove_bad_NAMES|
125 _remove_readonly|
126 _variable_NAMES|
127 _warning_PAR|
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;
135 print;'
137 _prefix_PARALLEL_ENV() {
138 shopt 2>/dev/null |
139 perl -pe 's:\s+off:;: and s/^/shopt -u /;
140 s:\s+on:;: and s/^/shopt -s /;
141 s:;$:&>/dev/null;:';
142 echo 'shopt -s expand_aliases &>/dev/null';
145 _get_ignored_VARS() {
146 perl -e '
147 for(@ARGV){
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";
155 } else {
156 chomp(@ignored_vars = <IN>);
159 if($ENV{PARALLEL_IGNORED_NAMES}) {
160 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
161 chomp @ignored_vars;
163 $vars = join "|",map { quotemeta $_ } @ignored_vars;
164 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
165 ' -- "$@"
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() {
173 perl -e '
174 for(@ARGV){
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)" : "(.*)";
181 ' -- "$@"
183 _which_PAR() {
184 # type returns:
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
195 LANG=C type "$@" |
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 }'
205 _warning_PAR() {
206 echo "env_parallel: Warning: $*" >&2
208 _error_PAR() {
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.'
217 return 255
220 # Grep regexp for vars given by --env
221 # shellcheck disable=SC2006
222 _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
223 unset _make_grep_REGEXP
225 # Deal with --env _
226 # shellcheck disable=SC2006
227 _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
228 unset _get_ignored_VARS
230 # --record-env
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
234 true skip
235 else
236 (_names_of_ALIASES;
237 _names_of_FUNCTIONS;
238 _names_of_VARIABLES) |
239 cat > "$HOME"/.parallel/ignored_vars
240 return 0
243 # --session
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
247 true skip
248 else
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\";
253 echo :::;
254 (_names_of_ALIASES;
255 _names_of_FUNCTIONS;
256 _names_of_VARIABLES) | perl -ne '
257 BEGIN{
258 map { $ignored_vars{$_}++ }
259 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
261 chomp;
262 for(split/\s+/) {
263 if(not $ignored_vars{$_}) {
264 print $_,\"\\n\";
268 export PARALLEL_IGNORED_NAMES
269 return 0
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
274 true skip
275 else
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}
282 return 0
284 # Grep alias 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"
292 unset _alias_NAMES
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
317 PARALLEL_ENV="`
318 _prefix_PARALLEL_ENV
319 $_list_alias_BODIES;
320 $_list_function_BODIES;
321 $_list_variable_VALUES;
323 export PARALLEL_ENV
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
332 parallel "$@"
333 _parallel_exit_CODE=$?
334 # Clean up variables/functions
335 unset PARALLEL_ENV
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"
340 else
341 unset PARALLEL_ENV;
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"
351 return 255
355 parset() {
356 _parset_PARALLEL_PRG=parallel
357 _parset_main "$@"
360 env_parset() {
361 _parset_PARALLEL_PRG=env_parallel
362 _parset_main "$@"
365 _parset_main() {
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}
373 # echo ${myvar[5]}
375 # # Put output into $var_a $var_b $var_c
376 # varnames=(var_a var_b var_c)
377 # parset "${varnames[*]}" echo ::: {1..3}
378 # echo $var_c
380 # # Put output into $var_a4 $var_b4 $var_c4
381 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
382 # echo $var_c4
384 _parset_NAME="$1"
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
389 return 255
391 if [ "$_parset_NAME" = "--help" ] ; then
392 echo parset: Error: Usage: >&2
393 echo parset: Error: ' ' parset varname GNU Parallel options and command >&2
394 echo
395 parallel --help
396 return 255
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."
406 echo
407 echo "Web site: https://www.gnu.org/software/parallel"
408 echo
409 echo "When using programs that use GNU Parallel to process data for publication"
410 echo "please cite as described in 'parallel --citation'."
411 echo
412 return 255
414 shift
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!
426 else
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!