Released as 20231122 ('GrindavĂ­k')
[parallel.git] / src / env_parallel.ash
blob47faf062dae26ab870c78f11d8126e8212167b59
1 #!/usr/bin/env ash
3 # This file must be sourced in ash:
5 # . env_parallel.ash
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 shell=dash
32 env_parallel() {
33 # based on env_parallel.sh
35 _names_of_ALIASES() {
36 # alias fails on Unixware 5
37 for _i in `alias 2>/dev/null | perl -ne 's/^alias //;s/^(\S+)=.*/$1/ && print' 2>/dev/null`; do
38 # Check if this name really is an alias
39 # or just part of a multiline alias definition
40 if alias "$_i" >/dev/null 2>/dev/null; then
41 echo "$_i"
43 done
45 _bodies_of_ALIASES() {
46 # alias may return:
47 # myalias='definition' (GNU/Linux ash)
48 # alias myalias='definition' (FreeBSD ash)
49 # so remove 'alias ' from first line
50 for _i in "$@"; do
51 echo 'alias '"`alias "$_i" | perl -pe '1..1 and s/^alias //'`"
52 done
54 _names_of_maybe_FUNCTIONS() {
55 set | perl -ne '/^([A-Z_0-9]+)\s*\(\)\s*\{?$/i and print "$1\n"'
57 _names_of_FUNCTIONS() {
58 # myfunc is a function
59 # shellcheck disable=SC2046
60 LANG=C type `_names_of_maybe_FUNCTIONS` |
61 perl -ne '/^(\S+) is a function$/ and not $seen{$1}++ and print "$1\n"'
63 _bodies_of_FUNCTIONS() {
64 LANG=C type "$@" | perl -ne '/^(\S+) is a function$/ or print'
66 _names_of_VARIABLES() {
67 # This may screw up if variables contain \n and =
68 set | perl -ne 's/^(\S+?)=.*/$1/ and print;'
70 _bodies_of_VARIABLES() {
71 # Crappy typeset -p
72 for _i in "$@"
74 perl -e 'print @ARGV' "$_i="
75 eval echo "\"\$$_i\"" | perl -e '$/=undef; $a=<>; chop($a); print $a' |
76 perl -pe 's/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\202-\377]/\\$&/go;'"s/'/\\\'/g; s/[\n]/'\\n'/go;";
77 echo
78 done
80 _ignore_HARDCODED() {
81 # These names cannot be detected
82 echo '(_|TIMEOUT|IFS)'
84 _ignore_READONLY() {
85 # shellcheck disable=SC1078,SC1079,SC2026
86 readonly | perl -e '@r = map {
87 chomp;
88 # sh on UnixWare: readonly TIMEOUT
89 # ash: readonly var='val'
90 # ksh: var='val'
91 # mksh: PIPESTATUS[0]
92 s/^(readonly )?([^=\[ ]*?)(\[\d+\])?(=.*|)$/$2/ or
93 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
94 # zsh: typeset -r var='val'
95 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
96 $_ } <>;
97 $vars = join "|",map { quotemeta $_ } @r;
98 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
101 _remove_bad_NAMES() {
102 # Do not transfer vars and funcs from env_parallel
103 # shellcheck disable=SC2006
104 _ignore_RO="`_ignore_READONLY`"
105 # shellcheck disable=SC2006
106 _ignore_HARD="`_ignore_HARDCODED`"
107 # To avoid depending on grep dialect, use Perl version of:
108 # grep -Ev '^(...)$' |
109 perl -ne '/^(
110 PARALLEL_ENV|
111 PARALLEL_TMP|
112 _alias_NAMES|
113 _bodies_of_ALIASES|
114 _bodies_of_FUNCTIONS|
115 _bodies_of_VARIABLES|
116 _error_PAR|
117 _function_NAMES|
118 _get_ignored_VARS|
119 _grep_REGEXP|
120 _ignore_HARD|
121 _ignore_HARDCODED|
122 _ignore_READONLY|
123 _ignore_RO|
124 _ignore_UNDERSCORE|
125 _list_alias_BODIES|
126 _list_function_BODIES|
127 _list_variable_VALUES|
128 _make_grep_REGEXP|
129 _names_of_ALIASES|
130 _names_of_FUNCTIONS|
131 _names_of_VARIABLES|
132 _names_of_maybe_FUNCTIONS|
133 _parallel_exit_CODE|
134 _prefix_PARALLEL_ENV|
135 _prefix_PARALLEL_ENV|
136 _remove_bad_NAMES|
137 _remove_readonly|
138 _variable_NAMES|
139 _warning_PAR|
140 _which_PAR)$/x and next;
141 # Filter names matching --env
142 /^'"$_grep_REGEXP"'$/ or next;
143 /^'"$_ignore_UNDERSCORE"'$/ and next;
144 # Remove readonly variables
145 /^'"$_ignore_RO"'$/ and next;
146 /^'"$_ignore_HARD"'$/ and next;
147 print;'
149 _get_ignored_VARS() {
150 perl -e '
151 for(@ARGV){
152 $next_is_env and push @envvar, split/,/, $_;
153 $next_is_env=/^--env$/;
155 if(grep { /^_$/ } @envvar) {
156 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
157 print STDERR "parallel: Error: ",
158 "Run \"parallel --record-env\" in a clean environment first.\n";
159 } else {
160 chomp(@ignored_vars = <IN>);
163 if($ENV{PARALLEL_IGNORED_NAMES}) {
164 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
165 chomp @ignored_vars;
167 $vars = join "|",map { quotemeta $_ } @ignored_vars;
168 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
169 ' -- "$@"
172 # Get the --env variables if set
173 # --env _ should be ignored
174 # and convert a b c to (a|b|c)
175 # If --env not set: Match everything (.*)
176 _make_grep_REGEXP() {
177 perl -e '
178 for(@ARGV){
179 /^_$/ and $next_is_env = 0;
180 $next_is_env and push @envvar, split/,/, $_;
181 $next_is_env = /^--env$/;
183 $vars = join "|",map { quotemeta $_ } @envvar;
184 print $vars ? "($vars)" : "(.*)";
185 ' -- "$@"
187 _which_PAR() {
188 # type returns:
189 # ll is an alias for ls -l (in ash)
190 # bash is a tracked alias for /bin/bash
191 # true is a shell builtin (in bash)
192 # myfunc is a function (in bash)
193 # myfunc is a shell function (in zsh)
194 # which is /usr/bin/which (in sh, bash)
195 # which is hashed (/usr/bin/which)
196 # gi is aliased to `grep -i' (in bash)
197 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
198 # Return 0 if found, 1 otherwise
199 LANG=C type "$@" |
200 perl -pe '$exit += (s/ is an alias for .*// ||
201 s/ is aliased to .*// ||
202 s/ is a function// ||
203 s/ is a shell function// ||
204 s/ is a shell builtin// ||
205 s/.* is hashed .(\S+).$/$1/ ||
206 s/.* is (a tracked alias for )?//);
207 END { exit not $exit }'
209 _warning_PAR() {
210 echo "env_parallel: Warning: $*" >&2
212 _error_PAR() {
213 echo "env_parallel: Error: $*" >&2
216 if _which_PAR parallel >/dev/null; then
217 true parallel found in path
218 else
219 # shellcheck disable=SC2016
220 _error_PAR 'parallel must be in $PATH.'
221 return 255
224 # Grep regexp for vars given by --env
225 # shellcheck disable=SC2006
226 _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
227 unset _make_grep_REGEXP
229 # Deal with --env _
230 # shellcheck disable=SC2006
231 _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
232 unset _get_ignored_VARS
234 # --record-env
235 if perl -e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
236 true skip
237 else
238 (_names_of_ALIASES;
239 _names_of_FUNCTIONS;
240 _names_of_VARIABLES) |
241 cat > "$HOME"/.parallel/ignored_vars
242 return 0
245 # --session
246 if perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@"; 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 if perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
272 true skip
273 else
274 # Pop off last ::: from PARALLEL_IGNORED_NAMES
275 # shellcheck disable=SC2006
276 PARALLEL_IGNORED_NAMES="`perl -e '
277 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
278 print $ENV{PARALLEL_IGNORED_NAMES}
280 return 0
282 # Grep alias names
283 # shellcheck disable=SC2006
284 _alias_NAMES="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
285 _list_alias_BODIES="_bodies_of_ALIASES $_alias_NAMES"
286 if [ "$_alias_NAMES" = "" ] ; then
287 # no aliases selected
288 _list_alias_BODIES="true"
290 unset _alias_NAMES
292 # Grep function names
293 # shellcheck disable=SC2006
294 _function_NAMES="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
295 _list_function_BODIES="_bodies_of_FUNCTIONS $_function_NAMES"
296 if [ "$_function_NAMES" = "" ] ; then
297 # no functions selected
298 _list_function_BODIES="true"
300 unset _function_NAMES
302 # Grep variable names
303 # shellcheck disable=SC2006
304 _variable_NAMES="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
305 _list_variable_VALUES="_bodies_of_VARIABLES $_variable_NAMES"
306 if [ "$_variable_NAMES" = "" ] ; then
307 # no variables selected
308 _list_variable_VALUES="true"
310 unset _variable_NAMES
312 # shellcheck disable=SC2006
313 PARALLEL_ENV="`
314 $_list_alias_BODIES;
315 $_list_function_BODIES;
316 $_list_variable_VALUES;
318 export PARALLEL_ENV
319 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
320 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
321 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
322 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
323 unset _remove_bad_NAMES _grep_REGEXP
324 unset _prefix_PARALLEL_ENV
325 # Test if environment is too big by running 'true'
326 # shellcheck disable=SC2006,SC2092
327 if `_which_PAR true` >/dev/null 2>/dev/null ; then
328 parallel "$@"
329 _parallel_exit_CODE=$?
330 # Clean up variables/functions
331 unset PARALLEL_ENV
332 unset _which_PAR _which_TRUE
333 unset _warning_PAR _error_PAR
334 # Unset _parallel_exit_CODE before return
335 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
336 else
337 unset PARALLEL_ENV;
338 _error_PAR "Your environment is too big."
339 _error_PAR "You can try 3 different approaches:"
340 _error_PAR "1. Run 'env_parallel --session' before you set"
341 _error_PAR " variables or define functions."
342 _error_PAR "2. Use --env and only mention the names to copy."
343 _error_PAR "3. Try running this in a clean environment once:"
344 _error_PAR " env_parallel --record-env"
345 _error_PAR " And then use '--env _'"
346 _error_PAR "For details see: man env_parallel"
347 return 255
351 parset() {
352 _parset_PARALLEL_PRG=parallel
353 _parset_main "$@"
356 env_parset() {
357 _parset_PARALLEL_PRG=env_parallel
358 _parset_main "$@"
361 _parset_main() {
362 # If $1 contains ',' or space:
363 # Split on , to get the destination variable names
364 # If $1 is a single destination variable name:
365 # Treat it as the name of an array
367 # # Create array named myvar
368 # parset myvar echo ::: {1..10}
369 # echo ${myvar[5]}
371 # # Put output into $var_a $var_b $var_c
372 # varnames=(var_a var_b var_c)
373 # parset "${varnames[*]}" echo ::: {1..3}
374 # echo $var_c
376 # # Put output into $var_a4 $var_b4 $var_c4
377 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
378 # echo $var_c4
380 _parset_NAME="$1"
381 if [ "$_parset_NAME" = "" ] ; then
382 echo parset: Error: No destination variable given. >&2
383 echo parset: Error: Try: >&2
384 echo parset: Error: ' ' parset myarray echo ::: foo bar >&2
385 return 255
387 if [ "$_parset_NAME" = "--help" ] ; then
388 echo parset: Error: Usage: >&2
389 echo parset: Error: ' ' parset varname GNU Parallel options and command >&2
390 echo
391 parallel --help
392 return 255
394 if [ "$_parset_NAME" = "--version" ] ; then
395 # shellcheck disable=SC2006
396 echo "parset 20231122 (GNU parallel `parallel --minversion 1`)"
397 echo "Copyright (C) 2007-2023 Ole Tange, http://ole.tange.dk and Free Software"
398 echo "Foundation, Inc."
399 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
400 echo "This is free software: you are free to change and redistribute it."
401 echo "GNU parallel comes with no warranty."
402 echo
403 echo "Web site: https://www.gnu.org/software/parallel"
404 echo
405 echo "When using programs that use GNU Parallel to process data for publication"
406 echo "please cite as described in 'parallel --citation'."
407 echo
408 return 255
410 shift
412 # Bash: declare -A myassoc=( )
413 # Zsh: typeset -A myassoc=( )
414 # Ksh: typeset -A myassoc=( )
415 # shellcheck disable=SC2039,SC2169
416 if (typeset -p "$_parset_NAME" 2>/dev/null; echo) |
417 perl -ne 'exit not (/^declare[^=]+-A|^typeset[^=]+-A/)' ; then
418 # This is an associative array
419 # shellcheck disable=SC2006
420 eval "`$_parset_PARALLEL_PRG -k --_parset assoc,"$_parset_NAME" "$@"`"
421 # The eval returns the function!
422 else
423 # This is a normal array or a list of variable names
424 # shellcheck disable=SC2006
425 eval "`$_parset_PARALLEL_PRG -k --_parset var,"$_parset_NAME" "$@"`"
426 # The eval returns the function!