env_parallel.bash: Fixed for bash #47483: env_parallel export --env only.

This commit is contained in:
Ole Tange 2016-06-29 03:24:14 +02:00
parent 85dfb3e96a
commit 8637d30bca
2 changed files with 94 additions and 26 deletions

View file

@ -29,18 +29,75 @@
env_parallel() { env_parallel() {
# env_parallel.bash # env_parallel.bash
PARALLEL_ENV="$(alias; local argv_ARRAY=()
typeset -p | local grep_ARRAY=()
local _par_i
local _par_ARR
# Get the --env variables if set
while test $# -gt 0; do
key="$1"
case $key in
--env)
argv_ARRAY+=("$1")
shift
# split --env on ,
IFS=',' read -ra _par_ARR <<< "$1"
for _par_i in "${_par_ARR[@]}"; do
grep_ARRAY+=("$_par_i")
done
;;
esac
argv_ARRAY+=("$1")
shift # past argument or value
done
# This converts a b c to (a|b|c)
local grep_REGEXP="$(perl -e 'print "(".(join "|",map { quotemeta $_ } @ARGV).")"' "${grep_ARRAY[@]}")"
if [[ "$grep_REGEXP" = "()" ]] ; then
# --env not set: Match everything
grep_REGEXP="(.*)"
fi
# Grep alias names
local _alias_NAMES="$(compgen -a |
egrep "^${grep_REGEXP}\$")"
local _list_alias_BODIES="alias $_alias_NAMES"
if [[ "$_alias_NAMES" = "" ]] ; then
# no aliases selected
_list_alias_BODIES="true"
fi
# Grep function names
local _function_NAMES="$(compgen -A function |
egrep "^${grep_REGEXP}\$")"
local _list_function_BODIES="typeset -f $_function_NAMES"
if [[ "$_function_NAMES" = "" ]] ; then
# no functions selected
_list_function_BODIES="true"
fi
# Grep variable names
local _variable_NAMES="$(compgen -A variable |
egrep "^${grep_REGEXP}\$" |
grep -vFf <(readonly) | grep -vFf <(readonly) |
grep -v 'declare .. (GROUPS|FUNCNAME|DIRSTACK|_|PIPESTATUS|USERNAME|BASH_[A-Z_]+) '; egrep -v '^(BASHOPTS|BASHPID|EUID|GROUPS|FUNCNAME|DIRSTACK|_|PIPESTATUS|PPID|SHELLOPTS|UID|USERNAME|BASH_[A-Z_]+)$')"
typeset -f)"; local _list_variable_VALUES="typeset -p $_variable_NAMES"
if [[ "$_variable_NAMES" = "" ]] ; then
# no variables selected
_list_variable_VALUES="true"
fi
# Copy shopt (so e.g. extended globbing works) # Copy shopt (so e.g. extended globbing works)
parallel_shopt="$(shopt 2>/dev/null | export PARALLEL_ENV="$(
shopt 2>/dev/null |
perl -pe 's:\s+off:;: and s/^/shopt -u /; perl -pe 's:\s+off:;: and s/^/shopt -u /;
s:\s+on:;: and s/^/shopt -s /;')" s:\s+on:;: and s/^/shopt -s /;';
export PARALLEL_ENV="$parallel_shopt$PARALLEL_ENV" $_list_alias_BODIES;
unset parallel_shopt; $_list_variable_VALUES;
`which parallel` "$@"; $_list_function_BODIES)";
`which parallel` "${argv_ARRAY[@]}";
unset PARALLEL_ENV; unset PARALLEL_ENV;
} }

View file

@ -44,6 +44,9 @@ Same as GNU B<parallel>.
=head2 Bash =head2 Bash
B<--env> is supported to export only the variable, alias, function, or
array with the given name. Multiple B<--env>s can be given.
Installation Installation
Put this in $HOME/.bashrc: Put this in $HOME/.bashrc:
@ -58,27 +61,35 @@ E.g. by doing:
=item aliases =item aliases
alias myecho=echo alias myecho='echo aliases'
env_parallel myecho ::: test env_parallel myecho ::: work
env_parallel -S server myecho ::: test env_parallel -S server myecho ::: work
env_parallel --env myecho myecho ::: work
env_parallel --env myecho -S server myecho ::: work
=item functions =item functions
myfunc() { echo $*; } myfunc() { echo functions $*; }
env_parallel myfunc ::: test env_parallel myfunc ::: work
env_parallel -S server myfunc ::: test env_parallel -S server myfunc ::: work
env_parallel --env myfunc myfunc ::: work
env_parallel --env myfunc -S server myfunc ::: work
=item variables =item variables
myvar=test myvar=variables
env_parallel echo '$myvar' ::: test env_parallel echo '$myvar' ::: work
env_parallel -S server echo '$myvar' ::: test env_parallel -S server echo '$myvar' ::: work
env_parallel --env myvar echo '$myvar' ::: work
env_parallel --env myvar -S server echo '$myvar' ::: work
=item arrays =item arrays
myarray=(foo bar baz) myarray=(arrays work, too)
env_parallel echo '${myarray[{}]}' ::: 0 1 2 env_parallel -k echo '${myarray[{}]}' ::: 0 1 2
env_parallel -S server echo '${myarray[{}]}' ::: 0 1 2 env_parallel -k -S server echo '${myarray[{}]}' ::: 0 1 2
env_parallel -k --env myarray echo '${myarray[{}]}' ::: 0 1 2
env_parallel -k --env myarray -S server echo '${myarray[{}]}' ::: 0 1 2
=back =back