Released as 20201022 ('SamuelPaty')
[parallel.git] / src / env_parallel.sh
blob94bb9768d93ae872189fb96ce1373920a8547233
1 #!/usr/bin/env sh
3 # This file must be sourced in sh:
5 # . `which env_parallel.sh`
7 # after which 'env_parallel' works
10 # Copyright (C) 2016-2020 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 env_parallel() {
29 # env_parallel.sh
31 _names_of_ALIASES() {
32 # alias fails on Unixware 5
33 for _i in `alias 2>/dev/null | perl -ne 's/^alias //;s/^(\S+)=.*/$1/ && print' 2>/dev/null`; do
34 # Check if this name really is an alias
35 # or just part of a multiline alias definition
36 if alias $_i >/dev/null 2>/dev/null; then
37 echo $_i
39 done
41 _bodies_of_ALIASES() {
42 # alias may return:
43 # myalias='definition' (GNU/Linux ash)
44 # alias myalias='definition' (FreeBSD ash)
45 # so remove 'alias ' from first line
46 for _i in "$@"; do
47 echo 'alias '"`alias $_i | perl -pe '1..1 and s/^alias //'`"
48 done
50 _names_of_maybe_FUNCTIONS() {
51 set | perl -ne '/^([A-Z_0-9]+)\s*\(\)\s*\{?$/i and print "$1\n"'
53 _names_of_FUNCTIONS() {
54 # myfunc is a function
55 LANG=C type `_names_of_maybe_FUNCTIONS` |
56 perl -ne '/^(\S+) is a function$/ and not $seen{$1}++ and print "$1\n"'
58 _bodies_of_FUNCTIONS() {
59 LANG=C type "$@" | perl -ne '/^(\S+) is a function$/ or print'
61 _names_of_VARIABLES() {
62 # This may screw up if variables contain \n and =
63 set | perl -ne 's/^(\S+?)=.*/$1/ and print;'
65 _bodies_of_VARIABLES() {
66 # Crappy typeset -p
67 for _i in "$@"
69 perl -e 'print @ARGV' "$_i="
70 eval echo \"\$$_i\" | perl -e '$/=undef; $a=<>; chop($a); print $a' |
71 perl -pe 's/[\002-\011\013-\032\\\#\?\`\(\)\{\}\[\]\^\*\<\=\>\~\|\; \"\!\$\&\202-\377]/\\$&/go;'"s/'/\\\'/g; s/[\n]/'\\n'/go;";
72 echo
73 done
75 _ignore_HARDCODED() {
76 # These names cannot be detected
77 echo '(_|TIMEOUT)'
79 _ignore_READONLY() {
80 readonly | perl -e '@r = map {
81 chomp;
82 # sh on UnixWare: readonly TIMEOUT
83 # ash: readonly var='val'
84 # ksh: var='val'
85 s/^(readonly )?([^= ]*)(=.*|)$/$2/ or
86 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
87 # zsh: typeset -r var='val'
88 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
89 $_ } <>;
90 $vars = join "|",map { quotemeta $_ } @r;
91 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
94 _remove_bad_NAMES() {
95 # Do not transfer vars and funcs from env_parallel
96 _ignore_RO="`_ignore_READONLY`"
97 _ignore_HARD="`_ignore_HARDCODED`"
98 # Macos-grep does not like long patterns
99 # Old Solaris grep does not support -E
100 # Perl Version of:
101 # grep -Ev '^(...)$' |
102 perl -ne '/^(
103 PARALLEL_ENV|
104 PARALLEL_TMP|
105 _alias_NAMES|
106 _bodies_of_ALIASES|
107 _bodies_of_FUNCTIONS|
108 _bodies_of_VARIABLES|
109 _error_PAR|
110 _function_NAMES|
111 _get_ignored_VARS|
112 _grep_REGEXP|
113 _ignore_HARD|
114 _ignore_HARDCODED|
115 _ignore_READONLY|
116 _ignore_RO|
117 _ignore_UNDERSCORE|
118 _list_alias_BODIES|
119 _list_function_BODIES|
120 _list_variable_VALUES|
121 _make_grep_REGEXP|
122 _names_of_ALIASES|
123 _names_of_FUNCTIONS|
124 _names_of_VARIABLES|
125 _names_of_maybe_FUNCTIONS|
126 _parallel_exit_CODE|
127 _prefix_PARALLEL_ENV|
128 _prefix_PARALLEL_ENV|
129 _remove_bad_NAMES|
130 _remove_readonly|
131 _variable_NAMES|
132 _warning_PAR|
133 _which_PAR)$/x and next;
134 # Filter names matching --env
135 /^'"$_grep_REGEXP"'$/ or next;
136 /^'"$_ignore_UNDERSCORE"'$/ and next;
137 # Remove readonly variables
138 /^'"$_ignore_RO"'$/ and next;
139 /^'"$_ignore_HARD"'$/ and next;
140 print;'
142 _get_ignored_VARS() {
143 perl -e '
144 for(@ARGV){
145 $next_is_env and push @envvar, split/,/, $_;
146 $next_is_env=/^--env$/;
148 if(grep { /^_$/ } @envvar) {
149 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
150 print STDERR "parallel: Error: ",
151 "Run \"parallel --record-env\" in a clean environment first.\n";
152 } else {
153 chomp(@ignored_vars = <IN>);
156 if($ENV{PARALLEL_IGNORED_NAMES}) {
157 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
158 chomp @ignored_vars;
160 $vars = join "|",map { quotemeta $_ } @ignored_vars;
161 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
162 ' -- "$@"
165 # Get the --env variables if set
166 # --env _ should be ignored
167 # and convert a b c to (a|b|c)
168 # If --env not set: Match everything (.*)
169 _make_grep_REGEXP() {
170 perl -e '
171 for(@ARGV){
172 /^_$/ and $next_is_env = 0;
173 $next_is_env and push @envvar, split/,/, $_;
174 $next_is_env = /^--env$/;
176 $vars = join "|",map { quotemeta $_ } @envvar;
177 print $vars ? "($vars)" : "(.*)";
178 ' -- "$@"
180 _which_PAR() {
181 # type returns:
182 # ll is an alias for ls -l (in ash)
183 # bash is a tracked alias for /bin/bash
184 # true is a shell builtin (in bash)
185 # myfunc is a function (in bash)
186 # myfunc is a shell function (in zsh)
187 # which is /usr/bin/which (in sh, bash)
188 # which is hashed (/usr/bin/which)
189 # gi is aliased to `grep -i' (in bash)
190 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
191 # Return 0 if found, 1 otherwise
192 LANG=C type "$@" |
193 perl -pe '$exit += (s/ is an alias for .*// ||
194 s/ is aliased to .*// ||
195 s/ is a function// ||
196 s/ is a shell function// ||
197 s/ is a shell builtin// ||
198 s/.* is hashed .(\S+).$/$1/ ||
199 s/.* is (a tracked alias for )?//);
200 END { exit not $exit }'
202 _warning_PAR() {
203 echo "env_parallel: Warning: $@" >&2
205 _error_PAR() {
206 echo "env_parallel: Error: $@" >&2
209 if _which_PAR parallel >/dev/null; then
210 true parallel found in path
211 else
212 _error_PAR 'parallel must be in $PATH.'
213 return 255
216 # Grep regexp for vars given by --env
217 _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
218 unset _make_grep_REGEXP
220 # Deal with --env _
221 _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
222 unset _get_ignored_VARS
224 # --record-env
225 if perl -e 'exit grep { /^--record-env$/ } @ARGV' -- "$@"; then
226 true skip
227 else
228 (_names_of_ALIASES;
229 _names_of_FUNCTIONS;
230 _names_of_VARIABLES) |
231 cat > $HOME/.parallel/ignored_vars
232 return 0
235 # --session
236 if perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@"; then
237 true skip
238 else
239 # Insert ::: between each level of session
240 # so you can pop off the last ::: at --end-session
241 PARALLEL_IGNORED_NAMES="`echo \"$PARALLEL_IGNORED_NAMES\";
242 echo :::;
243 (_names_of_ALIASES;
244 _names_of_FUNCTIONS;
245 _names_of_VARIABLES) | perl -ne '
246 BEGIN{
247 map { $ignored_vars{$_}++ }
248 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
250 chomp;
251 for(split/\s+/) {
252 if(not $ignored_vars{$_}) {
253 print $_,\"\\n\";
257 export PARALLEL_IGNORED_NAMES
258 return 0
260 if perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@"; then
261 true skip
262 else
263 # Pop off last ::: from PARALLEL_IGNORED_NAMES
264 PARALLEL_IGNORED_NAMES="`perl -e '
265 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
266 print $ENV{PARALLEL_IGNORED_NAMES}
268 return 0
270 # Grep alias names
271 _alias_NAMES="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
272 _list_alias_BODIES="_bodies_of_ALIASES $_alias_NAMES"
273 if [ "$_alias_NAMES" = "" ] ; then
274 # no aliases selected
275 _list_alias_BODIES="true"
277 unset _alias_NAMES
279 # Grep function names
280 _function_NAMES="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
281 _list_function_BODIES="_bodies_of_FUNCTIONS $_function_NAMES"
282 if [ "$_function_NAMES" = "" ] ; then
283 # no functions selected
284 _list_function_BODIES="true"
286 unset _function_NAMES
288 # Grep variable names
289 _variable_NAMES="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
290 _list_variable_VALUES="_bodies_of_VARIABLES $_variable_NAMES"
291 if [ "$_variable_NAMES" = "" ] ; then
292 # no variables selected
293 _list_variable_VALUES="true"
295 unset _variable_NAMES
297 PARALLEL_ENV="`
298 $_list_alias_BODIES;
299 $_list_function_BODIES;
300 $_list_variable_VALUES;
302 export PARALLEL_ENV
303 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
304 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
305 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
306 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
307 unset _remove_bad_NAMES _grep_REGEXP
308 unset _prefix_PARALLEL_ENV
309 # Test if environment is too big
310 if `_which_PAR true` >/dev/null 2>/dev/null ; then
311 parallel "$@"
312 _parallel_exit_CODE=$?
313 # Clean up variables/functions
314 unset PARALLEL_ENV
315 unset _which_PAR _which_TRUE
316 unset _warning_PAR _error_PAR
317 # Unset _parallel_exit_CODE before return
318 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
319 else
320 unset PARALLEL_ENV;
321 _error_PAR "Your environment is too big."
322 _error_PAR "You can try 3 different approaches:"
323 _error_PAR "1. Run 'env_parallel --session' before you set"
324 _error_PAR " variables or define functions."
325 _error_PAR "2. Use --env and only mention the names to copy."
326 _error_PAR "3. Try running this in a clean environment once:"
327 _error_PAR " env_parallel --record-env"
328 _error_PAR " And then use '--env _'"
329 _error_PAR "For details see: man env_parallel"
330 return 255
334 parset() {
335 _parset_PARALLEL_PRG=parallel
336 _parset_main "$@"
339 env_parset() {
340 _parset_PARALLEL_PRG=env_parallel
341 _parset_main "$@"
344 _parset_main() {
345 # If $1 contains ',' or space:
346 # Split on , to get the destination variable names
347 # If $1 is a single destination variable name:
348 # Treat it as the name of an array
350 # # Create array named myvar
351 # parset myvar echo ::: {1..10}
352 # echo ${myvar[5]}
354 # # Put output into $var_a $var_b $var_c
355 # varnames=(var_a var_b var_c)
356 # parset "${varnames[*]}" echo ::: {1..3}
357 # echo $var_c
359 # # Put output into $var_a4 $var_b4 $var_c4
360 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
361 # echo $var_c4
363 _make_TEMP() {
364 # mktemp does not exist on some OS
365 perl -e 'use File::Temp qw(tempfile);
366 $ENV{"TMPDIR"} ||= "/tmp";
367 print((tempfile(DIR=>$ENV{"TMPDIR"}, TEMPLATE => "parXXXXX"))[1])'
370 _parset_NAME="$1"
371 if [ "$_parset_NAME" = "" ] ; then
372 echo parset: Error: No destination variable given. >&2
373 echo parset: Error: Try: >&2
374 echo parset: Error: ' ' parset myarray echo ::: foo bar >&2
375 return 255
377 if [ "$_parset_NAME" = "--help" ] ; then
378 echo parset: Error: Usage: >&2
379 echo parset: Error: ' ' parset varname GNU Parallel options and command >&2
380 echo
381 parallel --help
382 return 255
384 if [ "$_parset_NAME" = "--version" ] ; then
385 echo "parset 20201022 (GNU parallel `parallel --minversion 1`)"
386 echo "Copyright (C) 2007-2020 Ole Tange, http://ole.tange.dk and Free Software"
387 echo "Foundation, Inc."
388 echo "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>"
389 echo "This is free software: you are free to change and redistribute it."
390 echo "GNU parallel comes with no warranty."
391 echo
392 echo "Web site: https://www.gnu.org/software/parallel"
393 echo
394 echo "When using programs that use GNU Parallel to process data for publication"
395 echo "please cite as described in 'parallel --citation'."
396 echo
397 return 255
399 shift
400 echo "$_parset_NAME" |
401 perl -ne 'chomp;for (split /[, ]/) {
402 # Allow: var_32 var[3]
403 if(not /^[a-zA-Z_][a-zA-Z_0-9]*(\[\d+\])?$/) {
404 print STDERR "parset: Error: $_ is an invalid variable name.\n";
405 print STDERR "parset: Error: Variable names must be letter followed by letters or digits.\n";
406 print STDERR "parset: Error: Usage:\n";
407 print STDERR "parset: Error: parset varname GNU Parallel options and command\n";
408 $exitval = 255;
411 exit $exitval;
412 ' || return 255
413 _exit_FILE=`_make_TEMP`
414 if perl -e 'exit not grep /,| /, @ARGV' "$_parset_NAME" ; then
415 # $_parset_NAME contains , or space
416 # Split on , or space to get the names
417 eval "`
418 # Compute results into files
419 ($_parset_PARALLEL_PRG --files -k "$@"; echo $? > "$_exit_FILE") |
420 # var1= cat tmpfile1; rm tmpfile1
421 # var2= cat tmpfile2; rm tmpfile2
422 parallel -q echo {2}='\`cat {1}; rm {1}\`' :::: - :::+ \`
423 echo "$_parset_NAME" |
424 perl -pe 's/,/ /g'
427 else
428 # $_parset_NAME does not contain , or space
429 # => $_parset_NAME is the name of the array to put data into
430 # Supported in: bash zsh ksh mksh
431 # Arrays do not work in: sh ash dash
432 eval "$_parset_NAME=( $(
433 # Compute results into files. Save exit value
434 ($_parset_PARALLEL_PRG --files -k "$@"; echo $? > "$_exit_FILE") |
435 perl -pe 'chop;$_="\"\`cat $_; rm $_\`\" "'
436 ) )"
438 unset _parset_NAME _parset_PARALLEL_PRG _parallel_exit_CODE
439 # Unset _exit_FILE before return
440 eval "unset _exit_FILE; return \`cat $_exit_FILE; rm $_exit_FILE\`"