Re: q// / qq// syntax or built-ins please ?

2017-03-24 Thread Eduardo Bustamante
Remember that bash's grammar is derived from the specification of the
POSIX shell 
(http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html).
Also, any addition to the bash grammar should be backwards compatible
(or enabled with a shopt, disabled by default), to avoid breaking
already working scripts.

Due to the complexity of the above limitations, I'd just use perl or
another language. It's not worth the effort. So don't feel discouraged
if you send patches and these are rejected.



Re: q// / qq// syntax or built-ins please ?

2017-03-24 Thread Vladimir Marek
While quoting can be difficult at time, one can make his life simpler

DELIM=\'
CHAR="[^$DELIM]"
REGEX="$CHAR*$DELIM($CHAR+)$DELIM$CHAR*"

$ echo "

Error: Cannot find file '/missing/file_1'.

Error: Cannot find file '/missing/file_2'.

" | while read line; do
if [[ $line =~ $REGEX ]]; then
echo "${BASH_REMATCH[1]}"
fi
done
/missing/file_1
/missing/file_2


Care has to be taken though, CHAR can't be for example

CHAR=ab
WORD="$CHAR*"

as that becomes 'ab*' which is not '(ab)*'

Personally, I would probably use perl.

-- 
Vlad


On Thu, Mar 23, 2017 at 08:40:35PM +, Jason Vas Dias wrote:
> Please , in some future versions of bash, could it provide
> support / help for avoiding "quoting hell", in such situations
> as :
> 
> $ echo "
> 
> Error: Cannot find file '/missing/file_1'.
> 
> Error: Cannot find file '/missing/file_2'.
> 
> "  |  while read line; do
>cmd='if [[ '$'"'"$line"$'"'' =~
> ^[^'"\\'"']*['"\\'"']([^'"\\'"']+)['"\\'"'][^'"\\'"']*$ ]]; then echo
> ${BASH_REMATCH[1]}; fi;';
>   echo "$cmd" | bash -;
> done
> 
> See what I have to do to match lines containing a non-empty
> single-quoted string ?
> ie. I just want to cut-and-paste such lines from the output of some
> application, and
> weed out the empty lines and print the  single-quoted string in lines
> containing them (only!), with a simple bash command. If you replace
> "echo $cmd | bash -" with
> 'eval "$cmd"' , it does not work, because the double-quotes which I
> had painstakingly inserted with '$'"''  get removed somehow by eval -
> why is this?
> ie, only if "$line" is empty, does bash evaluate the text:
>'if [[ "" = ... ]]; then ...'
> else, for the lines I want to match, it would evaluate eg. :
> + eval 'if [[ Error: Cannot find file '\''/missing_file_1'\''.\" =~
> ^[^\\\'\'']*[\\\'\'']([\\\'\'']+)[\\\'\''][^\\\'\'']*$ ]]; then echo
> ${BASH_REMATCH[1]}; fi;'
> + set +x
> ( nothing printed - the single quotes are stripped)
> + eval 'if [[ \"\" =~
> ^[^\\\'\'']*[\\\'\'']([\\\'\'']+)[\\\'\''][^\\\'\'']*$ ]]; then echo
> ${BASH_REMATCH[1]}; fi;'
> ++ [[ "" =~ ^[^\']*[\']([\']+)[\'][^\']*$ ]]
> + set +x
> +
> 
> I think bash needs some kind of "q/.../'" and 'qq/../' syntax / built-ins, or
> whatever syntax its author likes,  like PERL has, whereby the  single quote
> ("'") or double quote ('"') respectively are totally ignored within
> '/.../'  parameter
> strings, which should use a different quoting character than '"' or
> "'" to delineate
> them.
> 
> If the author won't develop these, I will & will send a patch.
> 
> Regards,
> Jason
> 



q// / qq// syntax or built-ins please ?

2017-03-23 Thread Jason Vas Dias
Please , in some future versions of bash, could it provide
support / help for avoiding "quoting hell", in such situations
as :

$ echo "

Error: Cannot find file '/missing/file_1'.

Error: Cannot find file '/missing/file_2'.

"  |  while read line; do
   cmd='if [[ '$'"'"$line"$'"'' =~
^[^'"\\'"']*['"\\'"']([^'"\\'"']+)['"\\'"'][^'"\\'"']*$ ]]; then echo
${BASH_REMATCH[1]}; fi;';
  echo "$cmd" | bash -;
done

See what I have to do to match lines containing a non-empty
single-quoted string ?
ie. I just want to cut-and-paste such lines from the output of some
application, and
weed out the empty lines and print the  single-quoted string in lines
containing them (only!), with a simple bash command. If you replace
"echo $cmd | bash -" with
'eval "$cmd"' , it does not work, because the double-quotes which I
had painstakingly inserted with '$'"''  get removed somehow by eval -
why is this?
ie, only if "$line" is empty, does bash evaluate the text:
   'if [[ "" = ... ]]; then ...'
else, for the lines I want to match, it would evaluate eg. :
+ eval 'if [[ Error: Cannot find file '\''/missing_file_1'\''.\" =~
^[^\\\'\'']*[\\\'\'']([\\\'\'']+)[\\\'\''][^\\\'\'']*$ ]]; then echo
${BASH_REMATCH[1]}; fi;'
+ set +x
( nothing printed - the single quotes are stripped)
+ eval 'if [[ \"\" =~
^[^\\\'\'']*[\\\'\'']([\\\'\'']+)[\\\'\''][^\\\'\'']*$ ]]; then echo
${BASH_REMATCH[1]}; fi;'
++ [[ "" =~ ^[^\']*[\']([\']+)[\'][^\']*$ ]]
+ set +x
+

I think bash needs some kind of "q/.../'" and 'qq/../' syntax / built-ins, or
whatever syntax its author likes,  like PERL has, whereby the  single quote
("'") or double quote ('"') respectively are totally ignored within
'/.../'  parameter
strings, which should use a different quoting character than '"' or
"'" to delineate
them.

If the author won't develop these, I will & will send a patch.

Regards,
Jason