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

@ -28,20 +28,77 @@
# Supports env of 127426 bytes # Supports env of 127426 bytes
env_parallel() { env_parallel() {
# env_parallel.bash # env_parallel.bash
PARALLEL_ENV="$(alias; local argv_ARRAY=()
typeset -p | local grep_ARRAY=()
grep -vFf <(readonly) | local _par_i
grep -v 'declare .. (GROUPS|FUNCNAME|DIRSTACK|_|PIPESTATUS|USERNAME|BASH_[A-Z_]+) '; local _par_ARR
typeset -f)";
# Copy shopt (so e.g. extended globbing works) # Get the --env variables if set
parallel_shopt="$(shopt 2>/dev/null | while test $# -gt 0; do
perl -pe 's:\s+off:;: and s/^/shopt -u /; key="$1"
s:\s+on:;: and s/^/shopt -s /;')"
export PARALLEL_ENV="$parallel_shopt$PARALLEL_ENV" case $key in
unset parallel_shopt; --env)
`which parallel` "$@"; argv_ARRAY+=("$1")
unset PARALLEL_ENV; 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) |
egrep -v '^(BASHOPTS|BASHPID|EUID|GROUPS|FUNCNAME|DIRSTACK|_|PIPESTATUS|PPID|SHELLOPTS|UID|USERNAME|BASH_[A-Z_]+)$')"
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)
export PARALLEL_ENV="$(
shopt 2>/dev/null |
perl -pe 's:\s+off:;: and s/^/shopt -u /;
s:\s+on:;: and s/^/shopt -s /;';
$_list_alias_BODIES;
$_list_variable_VALUES;
$_list_function_BODIES)";
`which parallel` "${argv_ARRAY[@]}";
unset PARALLEL_ENV;
} }
# Supports env of 127375 bytes # Supports env of 127375 bytes

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