env_parallel.bash: More read-only variables.
[parallel.git] / src / env_parallel.bash
blob27c05c179ce7aa647458881006643f8bf5bd5266
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-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.bash
31 _names_of_ALIASES() {
32 compgen -a
34 _bodies_of_ALIASES() {
35 local _i
36 for _i in $@; do
37 if [ $(alias $_i | wc -l) == 1 ] ; then
38 true Alias is a single line. Good.
39 else
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'."
44 done
45 alias "$@"
47 _names_of_FUNCTIONS() {
48 compgen -A function
50 _bodies_of_FUNCTIONS() {
51 typeset -f "$@"
53 _names_of_VARIABLES() {
54 compgen -A variable
56 _bodies_of_VARIABLES() {
57 typeset -p "$@"
59 _ignore_HARDCODED() {
60 # These names cannot be detected
61 echo '(_|TIMEOUT|GROUPS|FUNCNAME|DIRSTACK|PIPESTATUS|USERNAME|BASHPID|BASH_[A-Z_]+)'
63 _ignore_READONLY() {
64 readonly | perl -e '@r = map {
65 chomp;
66 # sh on UnixWare: readonly TIMEOUT
67 # ash: readonly var='val'
68 # ksh: 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/;
73 $_ } <>;
74 $vars = join "|",map { quotemeta $_ } @r;
75 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
78 _remove_bad_NAMES() {
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
84 # Perl Version of:
85 # grep -Ev '^(...)$' |
86 perl -ne '/^(
87 PARALLEL_ENV|
88 PARALLEL_TMP|
89 _alias_NAMES|
90 _bodies_of_ALIASES|
91 _bodies_of_FUNCTIONS|
92 _bodies_of_VARIABLES|
93 _error_PAR|
94 _function_NAMES|
95 _get_ignored_VARS|
96 _grep_REGEXP|
97 _ignore_HARD|
98 _ignore_HARDCODED|
99 _ignore_READONLY|
100 _ignore_RO|
101 _ignore_UNDERSCORE|
102 _list_alias_BODIES|
103 _list_function_BODIES|
104 _list_variable_VALUES|
105 _make_grep_REGEXP|
106 _names_of_ALIASES|
107 _names_of_FUNCTIONS|
108 _names_of_VARIABLES|
109 _names_of_maybe_FUNCTIONS|
110 _parallel_exit_CODE|
111 _prefix_PARALLEL_ENV|
112 _prefix_PARALLEL_ENV|
113 _remove_bad_NAMES|
114 _remove_readonly|
115 _variable_NAMES|
116 _warning_PAR|
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;
124 print;'
126 _prefix_PARALLEL_ENV() {
127 shopt 2>/dev/null |
128 perl -pe 's:\s+off:;: and s/^/shopt -u /;
129 s:\s+on:;: and s/^/shopt -s /;
130 s:;$:&>/dev/null;:';
131 echo 'shopt -s expand_aliases &>/dev/null';
134 _get_ignored_VARS() {
135 perl -e '
136 for(@ARGV){
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";
144 } else {
145 chomp(@ignored_vars = <IN>);
148 if($ENV{PARALLEL_IGNORED_NAMES}) {
149 push @ignored_vars, split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
150 chomp @ignored_vars;
152 $vars = join "|",map { quotemeta $_ } @ignored_vars;
153 print $vars ? "($vars)" : "(,,nO,,VaRs,,)";
154 ' -- "$@"
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() {
162 perl -e '
163 for(@ARGV){
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)" : "(.*)";
170 ' -- "$@"
172 _which_PAR() {
173 # type returns:
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
184 LANG=C type "$@" |
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 }'
194 _warning_PAR() {
195 echo "env_parallel: Warning: $@" >&2
197 _error_PAR() {
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.'
205 return 255
208 # Grep regexp for vars given by --env
209 _grep_REGEXP="`_make_grep_REGEXP \"$@\"`"
210 unset _make_grep_REGEXP
212 # Deal with --env _
213 _ignore_UNDERSCORE="`_get_ignored_VARS \"$@\"`"
214 unset _get_ignored_VARS
216 # --record-env
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
220 true skip
221 else
222 (_names_of_ALIASES;
223 _names_of_FUNCTIONS;
224 _names_of_VARIABLES) |
225 cat > $HOME/.parallel/ignored_vars
226 return 0
229 # --session
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
233 true skip
234 else
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\";
238 echo :::;
239 (_names_of_ALIASES;
240 _names_of_FUNCTIONS;
241 _names_of_VARIABLES) | perl -ne '
242 BEGIN{
243 map { $ignored_vars{$_}++ }
244 split/\s+/, $ENV{PARALLEL_IGNORED_NAMES};
246 chomp;
247 for(split/\s+/) {
248 if(not $ignored_vars{$_}) {
249 print $_,\"\\n\";
253 export PARALLEL_IGNORED_NAMES
254 return 0
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
259 true skip
260 else
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}
266 return 0
268 # Grep alias 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"
275 unset _alias_NAMES
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
298 PARALLEL_ENV="`
299 _prefix_PARALLEL_ENV
300 $_list_alias_BODIES;
301 $_list_function_BODIES;
302 $_list_variable_VALUES;
304 export PARALLEL_ENV
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
313 parallel "$@"
314 _parallel_exit_CODE=$?
315 # Clean up variables/functions
316 unset PARALLEL_ENV
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"
321 else
322 unset PARALLEL_ENV;
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"
332 return 255
336 parset() {
337 _parset_PARALLEL_PRG=parallel
338 _parset_main "$@"
341 env_parset() {
342 _parset_PARALLEL_PRG=env_parallel
343 _parset_main "$@"
346 _parset_main() {
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}
354 # echo ${myvar[5]}
356 # # Put output into $var_a $var_b $var_c
357 # varnames=(var_a var_b var_c)
358 # parset "${varnames[*]}" echo ::: {1..3}
359 # echo $var_c
361 # # Put output into $var_a4 $var_b4 $var_c4
362 # parset "var_a4 var_b4 var_c4" echo ::: {1..3}
363 # echo $var_c4
365 _make_TEMP() {
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])'
372 _parset_NAME="$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
377 return 255
379 shift
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";
386 $exitval = 255;
389 exit $exitval;
390 ' || return 255
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
395 eval "$(
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'
405 else
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 $_\`\" "'
414 ) )"
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\`"