1

My bash code is simply this. I am trying to learn docker but also a newbie with bash scripting. I type in something simple like google.com for the read command but it gives me curl: (3) URL using bad/illegal format or missing URL. Anyone know what I am doing wrong?

docker exec -it missingDependencies sh -c "echo 'Input Website:'; read website; echo 'Searching..'; sleep 1; curl http://$website;"

Eric Goto
  • 63
  • 8

1 Answers1

1

Curl will give that warning when invoked like this (without a domain):

curl http://

let's define an image that has curl.

$ cat Dockerfile 
  FROM ubuntu:latest
  RUN apt-get update
  RUN apt-get install -y curl

and assemble it:

docker build .  -t foobar

So now we can run your script.

$ docker run -it --rm foobar /bin/sh -c \
  "set -x; echo 'Input Website:'; read website; echo 'Searching..'; curl https://$website ;"

+ echo Input Website:
Input Website:
+ read website
google.com
+ echo Searching..
Searching..
+ curl https://

Solution

 docker run -it --rm foobar /bin/sh -c \
  "set -x; read -p 'which website?: ' website; echo 'Searching..'; curl https://\$website;"

+ read -p which website?:  website
which website?: google.com
+ echo Searching..
Searching..
+ curl https://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="https://www.google.com/">here</A>.
</BODY></HTML>

The problem is that when you run bash -c "echo whatever $website", the $website variable will be taken from your current environment and not from the docker environment (from read website). To counteract that the $website variable is interpolated, you could use single quotes like sh -c 'read foo; echo $foo', or escape the dollar sign: sh -c "read foo; echo \$foo"

  • Thank you so much. I will read through your response and get back to you when I understand it – Eric Goto Oct 29 '21 at 19:43
  • Ok, I've tried this: `C:\Users\Eric Goto> docker exec -it asdf /bin/sh -c 'read xd; curl https://${xd}'` and it works. I am now trying to get `'read xd; echo "Searching for ${xd}";' to work – Eric Goto Oct 29 '21 at 20:09
  • Just a follow up question, do you know of any resources that could direct me to that would explain a few of the things you mentioned in your response? Things like interpolate, \ and the difference between " and ' – Eric Goto Oct 29 '21 at 20:14
  • Ah ok I got the thing I was working on to work. `'read xd; echo "$xd" you typed that;'`. But in ubuntu I can do `read xd; echo You typed $xd` as well as `read xd; echo "You typed $xd"` and they both work. How come they both don't work through docker exec? `docker exec -it asdf /bin/sh -c 'read xd; echo "$xd you typed that";'` Doesn't work `docker exec -it asdf /bin/sh -c 'read xd; echo "$xd" you typed that;'` Works – Eric Goto Oct 29 '21 at 20:30
  • https://stackoverflow.com/questions/6697753/difference-between-single-and-double-quotes-in-bash https://en.wikipedia.org/wiki/String_interpolation – Eric Goto Oct 29 '21 at 23:36