1

From this question I am able to get a wget url for the oracle jdk. I intend to use it in a script vis

wget_opts="-c --no-check-certificate --no-cookies --header --load-cookies="Cookie: oraclelicense=accept-securebackup-cookie""
jdk_download_url="http://download.oracle.com/otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.tar.gz"

/usr/bin/wget $wget_opts $jdk_download_url

When I echo the above command it appears ok and is able to correctly download the file. But on running the command in the script I get the below

--2014-06-04 14:19:43--  http://oraclelicense=accept-securebackup-cookie%22/
Resolving oraclelicense=accept-securebackup-cookie"... failed: Name or service not known.
wget: unable to resolve host address “oraclelicense=accept-securebackup-cookie"”
--2014-06-04 14:20:03--  http://download.oracle.com/otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.tar.gz
Resolving download.oracle.com...

Wget gets the wrong URL.

How do I correct this?

Community
  • 1
  • 1
anthonyms
  • 950
  • 1
  • 10
  • 20

3 Answers3

5

Use an array:

wget_opts=( -c 
            --no-check-certificate 
            --no-cookies 
            --header 
            --load-cookies="Cookie: oraclelicense=accept-securebackup-cookie" 
          )
jdk_download_url="http://download.oracle.com/otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.tar.gz"

# use the exact quoting below
/usr/bin/wget "${wget_opts[@]}" "$jdk_download_url"
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • See http://mywiki.wooledge.org/BashFAQ/050#I.27m_constructing_a_command_based_on_information_that_is_only_known_at_run_time – glenn jackman Jun 04 '14 at 15:05
  • @glenn with that I get "line 30: -c --no-check-certificate --no-cookies --header --load-cookies="Cookie: oraclelicense=accept-securebackup-cookie": command not found" – anthonyms Jun 04 '14 at 15:09
  • What shell are you using to run this script? Use bash or ksh – glenn jackman Jun 04 '14 at 15:14
  • @glenn. I use bash. I have corrected a small error. The wget get run nicely but the header doesn't get picked since the download link redirects to an error page. The error page is coz of no correct header. – anthonyms Jun 04 '14 at 15:18
  • does it matter if I have wget=/usr/bin/wget if ! $wget "${wget_opts[@]}" "$jdk_download_url"; then . . . . . . – anthonyms Jun 04 '14 at 15:19
  • @glennjackman works fine. but went jml's answer as it is simpler – anthonyms Jun 05 '14 at 07:33
3

Try this:

wget_opts='-c --no-check-certificate --no-cookies --header --load-cookies="Cookie: oraclelicense=accept-securebackup-cookie"'
jdk_download_url="http://download.oracle.com/otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.tar.gz"

/usr/bin/wget $wget_opts $jdk_download_url

Check the difference between single and double quotes in the bash manual.

EDIT: In fact you have some mistakes in your wget command line. Here is the correct line.

OPTS="-c --no-check-certificate --no-cookies --header Cookie:oraclelicense=accept-securebackup-cookie"
URL="http://download.oracle.com/otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.tar.gz"

wget $OPTS $URL

The --load-cookies option take a file as argument and not a string. We have to use the --header option with Cookie: oraclelicense=accept-securebackup-cookie. After tests, I have seen that wget does not care about spaces in the header field. So we can use directly Cookie:oraclelicense=accept-securebackup-cookie

If you use the --debug option you will see the correct formatted request :

GET /otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.tar.gz HTTP/1.1
Range: bytes=5307-
User-Agent: Wget/1.15 (linux-gnu)
Accept: */*
Host: download.oracle.com
Connection: Keep-Alive
Cookie: oraclelicense=accept-securebackup-cookie
jmlemetayer
  • 4,774
  • 1
  • 32
  • 46
  • @jml No, it doesn't, at least not in bash 3.2.25. It gets expanded to this: `/usr/bin/wget -c --no-check-certificate --no-cookies --header '--load-cookies="Cookie:' 'oraclelicense=accept-securebackup-cookie"' http://download.oracle.com/otn-pub/java/jdk/7u55-b13/jdk-7u55-linux-x64.tar.gz` – tkocmathla Jun 04 '14 at 15:06
2

You need to escape the double quotes that are within other double quotes:

wget_opts="-c --no-check-certificate --no-cookies --header --load-cookies=\"Cookie: oraclelicense=accept-securebackup-cookie\""

Or enclose the string in single quotes if you don't need variable interpolation:

wget_opts='-c --no-check-certificate --no-cookies --header --load-cookies="Cookie: oraclelicense=accept-securebackup-cookie"'

Also, in your command, you need a $ in front of jdk_download_url:

/usr/bin/wget $wget_opts $jdk_download_url
tkocmathla
  • 901
  • 11
  • 24