Re: bash shows an error message about unpaired quotes, but they are paired

2018-03-27 Thread Greg Wooledge
On Mon, Mar 26, 2018 at 09:54:21PM +0200, f.de.kru...@gmail.com wrote:
> #!/bin/bash
> 
> run () {
>echo "Running: ${*}"
>eval ${*}

If you're going to do it this way, that should be   eval "$*"  with the
quotes.

I strongly recommend NOT attempting this.  If anything, this way would
be slightly better:

run() {
echo "Running $*"
"$@"
...
}
run my command "with normally quoted" "${arguments[@]}"

That one allows arguments that have whitespace, but not redirections.
The "$*" variant allows redirections but not whitespace (without
additional quoting).

However, I consider both of them quite bad.  See
https://mywiki.wooledge.org/BashFaq/050

Especially 
https://mywiki.wooledge.org/BashFAQ/050#I_want_a_log_of_my_script.27s_actions

On Mon, Mar 26, 2018 at 04:05:02PM -0600, Eduardo Bustamante wrote:
> On Mon, Mar 26, 2018 at 1:54 PM,   wrote:
> > run 'sed -i "6a\find /srv/cowrie/log/ -mtime +7 -name \'cowrie.*\' -delete" 
> > cowrietest'
> 
> That's not how you escape single quotes within single quotes. Read:
> https://mywiki.wooledge.org/Quotes#Types_of_quoting
> 
> The proper way would be: run 'sed -i "6a\find /srv/cowrie/log/ -mtime
> +7 -name '\''cowrie.*'\''...

Or with $'...' quoting.  But still, I would scrap this altogether.
If I had to do it (forced at gunpoint or being paid to do it), then I
would use the "$@" variant which permits arbitrary arguments, because
I consider that far more important than being able to "submit and log"
redirections or pipelines.

P.S. what in the hell is that sed command even DOING?!  It looks like
you are writing a shell script that writes a shell command (wrapped in
extra quotes for submission to this rather hideous run function) which
invokes GNU sed to edit-in-place (BLAAARGGGH!) a file which may or
may not be a shell script already, and sed is attempting to add a shell
command to this file, and it's a nontrivial shell command with its
own layers of quoting.  Dear gods!  What an abomination.  You've got
like 4 different layers of commands wrapped inside each other.

Stop it!



Re: bash shows an error message about unpaired quotes, but they are paired

2018-03-27 Thread Chet Ramey
On 3/26/18 3:54 PM, f.de.kru...@gmail.com wrote:

> run 'sed -i "6a\find /srv/cowrie/log/ -mtime +7 -name \'cowrie.*\' -delete" 
> cowrietest'

Just so you understand how the quoting works here: you can't escape single
quotes with backslashes inside a single-quoted string. The \'cowrite.*\'
ends the first single-quoted string and inserts a single quote into the
command, since the backslash acts as an escape character in an unquoted
context. The double quote after -delete starts a new double-quoted string,
which is not terminated when the shell reaches the end of the script.

> ./aa.sh: line 16: unexpected EOF while looking for matching `"'
> ./aa.sh: line 17: syntax error: unexpected end of file


-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/