From 21d3b7d9be5db2acd0d0f70e9777bb8a4f4060f0 Mon Sep 17 00:00:00 2001 From: Ole Tange Date: Thu, 29 Jun 2017 04:21:44 +0200 Subject: [PATCH] env_parallel.bash: parset: Error handling if destination variable set wrongly. --- src/env_parallel.bash | 16 ++++++++++++++++ src/parset.pod | 9 +++++++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/env_parallel.bash b/src/env_parallel.bash index fce855c9..ca0b0b3c 100755 --- a/src/env_parallel.bash +++ b/src/env_parallel.bash @@ -203,6 +203,22 @@ parset() { _parset_name="$1" shift + if [ "$_parset_name" == "" ] ; then + echo parset: Error: No destination variable given. >&2 + echo parset: Error: Try: >&2 + echo parset: Error: ' ' parset myvar echo ::: foo >&2 + return 255 + fi + echo "$_parset_name" | + perl -ne 'chomp;for (split /[, ]/) { + if(not /^[a-zA-Z_][a-zA-Z_0-9]*$/) { + print STDERR "parset: Error: $_ is an invalid variable name.\n"; + print STDERR "parset: Error: Variable names must be letter followed by letters or digits.\n"; + $exitval = 255; + } + } + exit $exitval; + ' || return 255 if echo "$_parset_name" | grep -E ',| ' >/dev/null ; then # $1 contains , or space # Split on , or space to get the names diff --git a/src/parset.pod b/src/parset.pod index a051e831..8d3bb8c4 100644 --- a/src/parset.pod +++ b/src/parset.pod @@ -20,7 +20,7 @@ B into shell variables. The B function is defined as part of B. If I is a single variable name, this will be treated as -the destination variable and made to an array. +the destination variable and made into an array. If I contains multiple names separated by ',' or space, the names will be the destination variables. @@ -70,7 +70,12 @@ You cannot pipe into B, but must use a tempfile: parset res echo :::: parallel_input echo "${res[0]}" echo "${res[9]}" - + +or Bash process substitution: + + parset res echo :::: <(seq 100) + echo "${res[0]}" + echo "${res[99]}" =head3 Installation