From 8637d30bca70953a0a6c2ceee9983ff488ca4747 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Wed, 29 Jun 2016 03:24:14 +0200 Subject: [PATCH] env_parallel.bash: Fixed for bash #47483: env_parallel export --env only. --- src/env_parallel.bash | 85 ++++++++++++++++++++++++++++++++++++------- src/env_parallel.pod | 35 ++++++++++++------ 2 files changed, 94 insertions(+), 26 deletions(-) diff --git a/src/env_parallel.bash b/src/env_parallel.bash index ce735156..b994b3d8 100755 --- a/src/env_parallel.bash +++ b/src/env_parallel.bash @@ -28,20 +28,77 @@ # Supports env of 127426 bytes env_parallel() { - # env_parallel.bash - PARALLEL_ENV="$(alias; - typeset -p | - grep -vFf <(readonly) | - grep -v 'declare .. (GROUPS|FUNCNAME|DIRSTACK|_|PIPESTATUS|USERNAME|BASH_[A-Z_]+) '; - typeset -f)"; - # Copy shopt (so e.g. extended globbing works) - parallel_shopt="$(shopt 2>/dev/null | - perl -pe 's:\s+off:;: and s/^/shopt -u /; - s:\s+on:;: and s/^/shopt -s /;')" - export PARALLEL_ENV="$parallel_shopt$PARALLEL_ENV" - unset parallel_shopt; - `which parallel` "$@"; - unset PARALLEL_ENV; + # env_parallel.bash + local argv_ARRAY=() + 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) | + 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 diff --git a/src/env_parallel.pod b/src/env_parallel.pod index e321b4fa..ec2bfadd 100644 --- a/src/env_parallel.pod +++ b/src/env_parallel.pod @@ -44,6 +44,9 @@ Same as GNU B. =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 Put this in $HOME/.bashrc: @@ -58,27 +61,35 @@ E.g. by doing: =item aliases - alias myecho=echo - env_parallel myecho ::: test - env_parallel -S server myecho ::: test + alias myecho='echo aliases' + env_parallel myecho ::: work + env_parallel -S server myecho ::: work + env_parallel --env myecho myecho ::: work + env_parallel --env myecho -S server myecho ::: work =item functions - myfunc() { echo $*; } - env_parallel myfunc ::: test - env_parallel -S server myfunc ::: test + myfunc() { echo functions $*; } + env_parallel myfunc ::: work + env_parallel -S server myfunc ::: work + env_parallel --env myfunc myfunc ::: work + env_parallel --env myfunc -S server myfunc ::: work =item variables - myvar=test - env_parallel echo '$myvar' ::: test - env_parallel -S server echo '$myvar' ::: test + myvar=variables + env_parallel echo '$myvar' ::: work + 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 - myarray=(foo bar baz) - env_parallel echo '${myarray[{}]}' ::: 0 1 2 - env_parallel -S server echo '${myarray[{}]}' ::: 0 1 2 + myarray=(arrays work, too) + env_parallel -k 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