0

I've been dealing with this for about 3 days. I tried most of the combinations of double and single quotes, looked at most of the stackoverflow questions and answers and still no luck :( Below is the simplified version of my problem.

> line="008256042a1b sh -c \"mysqldump databasename\""
> echo $line
008256042a1b sh -c "mysqldump databasename"
> docker exec $line
databasename": 1: databasename": Syntax error: Unterminated quoted string
hannele
  • 53
  • 4
Onur Demir
  • 708
  • 2
  • 13
  • 35
  • 1
    You need to know on which container you want to run that instruction...it should be something like `docker run busybox bin/sh -c $line` – Hackerman May 06 '21 at 21:23
  • @Hackerman this states exactly which container to run the command in, and it uses `exec` to do it without creating a new container but using the environment to pass the command. The OP is trying to run tasks in an existing container and has provided a [mcve]. I don't see how your comment is relevant. – Software Engineer May 06 '21 at 22:12
  • 1
    This is [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050). – Charles Duffy May 06 '21 at 22:59
  • @Hackerman, using `$line` that way is buggy for the reasons described in the BashFAQ #50 link above. – Charles Duffy May 06 '21 at 23:02
  • @SE it was just a comment, not an answer...just like CD comment...comments should give hints about how to get to the actual answer...if you didn't get it that way, it's your problem...SMH – Hackerman May 07 '21 at 04:28

2 Answers2

3

The problem is that you tried to put a command in $line, it's like you want to run:

sh -c \"mysqldump Database\"
Database": 1: Syntax error: Unterminated quoted string

This should work :

line=(008256042a1b sh -c "mysqldump databasename")
echo "${line[@]}"
docker exec "${line[@]}"

"mysqldump databasename" is a single string.

\"mysqldump databasename\" is actually two strings.

"${line[@]}" allows the proper expansion of array items.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Philippe
  • 20,025
  • 2
  • 23
  • 32
  • 1
    For more details, I recommend reading http://mywiki.wooledge.org/BashFAQ/050. – chepner May 06 '21 at 22:15
  • 1
    @SoftwareEngineer Run this, you'll see it's working `docker run --name sleepy ubuntu sleep infinity & line=(sleepy sh -c "echo mysqldump databasename"); sleep 1; docker exec "${line[@]}"' – Philippe May 06 '21 at 22:33
  • @Philippe Yeah, that works, thanks. I'd misread (and mistyped) your solution as `$()` (subshell) rather than just `()` (array). But, I suppose the clue is in the `@`. Perhaps you could add a word or two explaining the solution a little more (the array bit anyway). – Software Engineer May 06 '21 at 22:52
  • I up voted because it's a working solution. But, I'm using the app and it's a little buggy and is giving me a strange error so who knows :) – Software Engineer May 06 '21 at 23:15
-1

Can you substitude the "docker exec" also ?
Then it simply becomes

line="008256042a1b sh -c \"mysqldump databasename\""
OR
line="008256042a1b sh -c 'mysqldump databasename'"
OR even
line="008256042a1b mysqldump databasename"

THEN
eval "docker exec ${line}"

edit: this can only be used when you trust what's in $line. Other wise, it is a security issue as mentionned in comments.

wcxyiany
  • 19
  • 3
  • This _works_, but it's not at all a good idea. [BashFAQ #48](https://mywiki.wooledge.org/BashFAQ/048) applies, which is why the approaches described in [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050) are better. – Charles Duffy May 06 '21 at 23:00
  • Yes, I understand that it is vulnerable to injection, but I assumed the input is trusted (which I hope it is since it would allow running anything on any container !). If it is, it's way simpler ... – wcxyiany May 07 '21 at 11:01
  • Often an array will be assembled by code that's piecing together trusted and untrusted bits, so you can have the first field be trusted, the second field be untrusted, etc; but for evaling a string, anything untrusted at all and it can run arbitrary code on not just containers but the host too. – Charles Duffy May 07 '21 at 14:04