3 # This file must be sourced in bash:
5 # source `which env_parallel.bash`
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
34 _bodies_of_ALIASES
() {
37 if [ $
(alias $_i |
wc -l) == 1 ] ; then
38 true Alias is a single line. Good.
40 _warning_PAR
"Alias '$_i' contains newline."
41 _warning_PAR
"Make sure the command has at least one newline after '$_i'."
42 _warning_PAR
"See BUGS in 'man env_parallel'."
47 _names_of_FUNCTIONS
() {
50 _bodies_of_FUNCTIONS
() {
53 _names_of_VARIABLES
() {
56 _bodies_of_VARIABLES
() {
60 # These names cannot be detected
61 echo '(_|TIMEOUT|GROUPS|FUNCNAME|DIRSTACK|PIPESTATUS|USERNAME|BASHPID|BASH_[A-Z_]+)'
64 readonly | perl
-e '@r = map {
66 # sh on UnixWare: readonly TIMEOUT
67 # ash: readonly var='val
'
69 s/^(readonly )?([^= ]*)(=.*|)$/$2/ or
70 # bash: declare -ar BASH_VERSINFO=([0]="4" [1]="4")
71 # zsh: typeset -r var='val
'
72 s/^\S+\s+\S+\s+(\S[^=]*)(=.*|$)/$1/;
74 $vars = join "|",map { quotemeta $_ } @r;
75 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
79 # Do not transfer vars and funcs from env_parallel
80 _ignore_RO
="`_ignore_READONLY`"
81 _ignore_HARD
="`_ignore_HARDCODED`"
82 # Macos-grep does not like long patterns
83 # Old Solaris grep does not support -E
85 # grep -Ev '^(...)$' |
103 _list_function_BODIES|
104 _list_variable_VALUES|
109 _names_of_maybe_FUNCTIONS|
111 _prefix_PARALLEL_ENV|
112 _prefix_PARALLEL_ENV|
117 _which_PAR)$/x and next;
118 # Filter names matching --env
119 /^'"$_grep_REGEXP"'$/ or next;
120 /^'"$_ignore_UNDERSCORE"'$/ and next;
121 # Remove readonly variables
122 /^'"$_ignore_RO"'$/ and next;
123 /^'"$_ignore_HARD"'$/ and next;
126 _prefix_PARALLEL_ENV
() {
128 perl
-pe 's:\s+off:;: and s/^/shopt -u /;
129 s:\s+on:;: and s/^/shopt -s /;
131 echo 'shopt -s expand_aliases &>/dev/null';
134 _get_ignored_VARS
() {
137 $next_is_env and push @envvar, split/,/, $_;
138 $next_is_env=/^--env$/;
140 if(grep { /^_$/ } @envvar) {
141 if(not open(IN, "<", "$ENV{HOME}/.parallel/ignored_vars")) {
142 print STDERR "parallel: Error: ",
143 "Run \"parallel --record-env\" in a clean environment first.\n";
145 chomp(@ignored_vars = <IN>);
148 if($ENV{PARALLEL_IGNORED_NAMES}) {
149 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
152 $vars = join "|",map { quotemeta $_ } @ignored_vars;
153 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
157 # Get the --env variables if set
158 # --env _ should be ignored
159 # and convert a b c to (a|b|c)
160 # If --env not set: Match everything (.*)
161 _make_grep_REGEXP
() {
164 /^_$/ and $next_is_env = 0;
165 $next_is_env and push @envvar, split/,/, $_;
166 $next_is_env = /^--env$/;
168 $vars = join "|",map { quotemeta $_ } @envvar;
169 print $vars ? "($vars)" : "(.*)";
174 # ll is an alias for ls -l (in ash)
175 # bash is a tracked alias for /bin/bash
176 # true is a shell builtin (in bash)
177 # myfunc is a function (in bash)
178 # myfunc is a shell function (in zsh)
179 # which is /usr/bin/which (in sh, bash)
180 # which is hashed (/usr/bin/which)
181 # gi is aliased to `grep -i' (in bash)
182 # aliased to `alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
183 # Return 0 if found, 1 otherwise
185 perl
-pe '$exit += (s/ is an alias for .*// ||
186 s/ is aliased to .*// ||
187 s/ is a function// ||
188 s/ is a shell function// ||
189 s/ is a shell builtin// ||
190 s/.* is hashed .(\S+).$/$1/ ||
191 s/.* is (a tracked alias for )?//);
192 END { exit not $exit }'
195 echo "env_parallel: Warning: $@" >&2
198 echo "env_parallel: Error: $@" >&2
201 # Bash is broken in version 3.2.25 and 4.2.39
202 # The crazy '[ "`...`" == "" ]' is needed for the same reason
203 if [ "`_which_PAR parallel`" == "" ]; then
204 _error_PAR
'parallel must be in $PATH.'
208 # Grep regexp for vars given by --env
209 _grep_REGEXP
="`_make_grep_REGEXP \"$@\"`"
210 unset _make_grep_REGEXP
213 _ignore_UNDERSCORE
="`_get_ignored_VARS \"$@\"`"
214 unset _get_ignored_VARS
217 # Bash is broken in version 3.2.25 and 4.2.39
218 # The crazy '[ "`...`" == 0 ]' is needed for the same reason
219 if [ "`perl -e 'exit grep { /^--record-env$/ } @ARGV' -- "$@
"; echo $?`" == 0 ] ; then
224 _names_of_VARIABLES
) |
225 cat > $HOME/.parallel
/ignored_vars
230 # Bash is broken in version 3.2.25 and 4.2.39
231 # The crazy '[ "`...`" == 0 ]' is needed for the same reason
232 if [ "`perl -e 'exit grep { /^--session$/ } @ARGV' -- "$@
"; echo $?`" == 0 ] ; then
235 # Insert ::: between each level of session
236 # so you can pop off the last ::: at --end-session
237 PARALLEL_IGNORED_NAMES
="`echo \"$PARALLEL_IGNORED_NAMES\";
241 _names_of_VARIABLES) | perl -ne '
243 map { $ignored_vars{$_}++ }
244 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
248 if(not $ignored_vars{$_}) {
253 export PARALLEL_IGNORED_NAMES
256 # Bash is broken in version 3.2.25 and 4.2.39
257 # The crazy '[ "`...`" == 0 ]' is needed for the same reason
258 if [ "`perl -e 'exit grep { /^--end.?session$/ } @ARGV' -- "$@
"; echo $?`" == 0 ] ; then
261 # Pop off last ::: from PARALLEL_IGNORED_NAMES
262 PARALLEL_IGNORED_NAMES
="`perl -e '
263 $ENV{PARALLEL_IGNORED_NAMES} =~ s/(.*):::.*?$/$1/s;
264 print $ENV{PARALLEL_IGNORED_NAMES}
269 _alias_NAMES
="`_names_of_ALIASES | _remove_bad_NAMES | xargs echo`"
270 _list_alias_BODIES
="_bodies_of_ALIASES $_alias_NAMES"
271 if [ "$_alias_NAMES" = "" ] ; then
272 # no aliases selected
273 _list_alias_BODIES
="true"
277 # Grep function names
278 _function_NAMES
="`_names_of_FUNCTIONS | _remove_bad_NAMES | xargs echo`"
279 _list_function_BODIES
="_bodies_of_FUNCTIONS $_function_NAMES"
280 if [ "$_function_NAMES" = "" ] ; then
281 # no functions selected
282 _list_function_BODIES
="true"
284 unset _function_NAMES
286 # Grep variable names
287 _variable_NAMES
="`_names_of_VARIABLES | _remove_bad_NAMES | xargs echo`"
288 _list_variable_VALUES
="_bodies_of_VARIABLES $_variable_NAMES"
289 if [ "$_variable_NAMES" = "" ] ; then
290 # no variables selected
291 _list_variable_VALUES
="true"
293 unset _variable_NAMES
295 _which_TRUE
="`_which_PAR true`"
296 # Copy shopt (so e.g. extended globbing works)
297 # But force expand_aliases as aliases otherwise do not work
301 $_list_function_BODIES;
302 $_list_variable_VALUES;
305 unset _list_alias_BODIES _list_variable_VALUES _list_function_BODIES
306 unset _bodies_of_ALIASES _bodies_of_VARIABLES _bodies_of_FUNCTIONS
307 unset _names_of_ALIASES _names_of_VARIABLES _names_of_FUNCTIONS
308 unset _ignore_HARDCODED _ignore_READONLY _ignore_UNDERSCORE
309 unset _remove_bad_NAMES _grep_REGEXP
310 unset _prefix_PARALLEL_ENV
311 # Test if environment is too big
312 if [ "`_which_PAR true`" == "$_which_TRUE" ] ; then
314 _parallel_exit_CODE
=$?
315 # Clean up variables/functions
317 unset _which_PAR _which_TRUE
318 unset _warning_PAR _error_PAR
319 # Unset _parallel_exit_CODE before return
320 eval "unset _parallel_exit_CODE; return $_parallel_exit_CODE"
323 _error_PAR
"Your environment is too big."
324 _error_PAR
"You can try 3 different approaches:"
325 _error_PAR
"1. Run 'env_parallel --session' before you set"
326 _error_PAR
" variables or define functions."
327 _error_PAR
"2. Use --env and only mention the names to copy."
328 _error_PAR
"3. Try running this in a clean environment once:"
329 _error_PAR
" env_parallel --record-env"
330 _error_PAR
" And then use '--env _'"
331 _error_PAR
"For details see: man env_parallel"
337 _parset_PARALLEL_PRG
=parallel
342 _parset_PARALLEL_PRG
=env_parallel
347 # If $1 contains ',' or space:
348 # Split on , to get the destination variable names
349 # If $1 is a single destination variable name:
350 # Treat it as the name of an array
352 # # Create array named myvar
353 # parset myvar echo ::: {1..10}
356 # # Put output into $var_a $var_b $var_c
357 # varnames=(var_a var_b var_c)
358 # parset "${varnames[*]}" echo ::: {1..3}
361 # # Put output into $var_a4 $var_b4 $var_c4
362 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
366 # mktemp does not exist on some OS
367 perl
-e 'use File::Temp qw(tempfile);
368 $ENV{"TMPDIR"} ||= "/tmp";
369 print((tempfile(DIR=>$ENV{"TMPDIR"}, TEMPLATE => "parXXXXX"))[1])'
373 if [ "$_parset_NAME" = "" ] ; then
374 echo parset
: Error
: No destination variable given.
>&2
375 echo parset
: Error
: Try
: >&2
376 echo parset
: Error
: ' ' parset myarray
echo ::: foo bar
>&2
380 echo "$_parset_NAME" |
381 perl
-ne 'chomp;for (split /[, ]/) {
382 # Allow: var_32 var[3]
383 if(not /^[a-zA-Z_][a-zA-Z_0-9]*(\[\d+\])?$/) {
384 print STDERR "parset: Error: $_ is an invalid variable name.\n";
385 print STDERR "parset: Error: Variable names must be letter followed by letters or digits.\n";
391 _exit_FILE
=`_make_TEMP`
392 if perl
-e 'exit not grep /,| /, @ARGV' "$_parset_NAME" ; then
393 # $_parset_NAME contains , or space
394 # Split on , or space to get the names
396 # Compute results into files
397 ($_parset_PARALLEL_PRG --files -k "$@
"; echo $? > "$_exit_FILE") |
398 # var1=`cat tmpfile1; rm tmpfile1`
399 # var2=`cat tmpfile2; rm tmpfile2`
400 parallel --plain -q echo {2}='`cat {1}; rm {1}`' :::: - :::+ $(
401 echo "$_parset_NAME" | perl -pe 's/,/ /g'
406 # $_parset_NAME does not contain , or space
407 # => $_parset_NAME is the name of the array to put data into
408 # Supported in: bash zsh ksh mksh
409 # Arrays do not work in: sh ash dash
410 eval "$_parset_NAME=( $(
411 # Compute results into files. Save exit value
412 ($_parset_PARALLEL_PRG --files -k "$@
"; echo $? > "$_exit_FILE") |
413 perl -pe 'chop;$_="\"\
`cat $_; rm $_\`\" "'
416 unset _parset_NAME _parset_PARALLEL_PRG _parallel_exit_CODE
417 # Unset _exit_FILE before return
418 eval "unset _exit_FILE; return \`cat $_exit_FILE; rm $_exit_FILE\`"