如何在shell脚本中处理引号

时间:2022-12-06 09:49:29

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

通过这个问题,我可以获得oracle jdk的wget url。我打算在脚本中使用它。

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

当我回显上面的命令时,它会显示ok并能够正确地下载文件。但是在运行脚本中的命令时,我得到了下面的命令。

--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.

Wget得到错误的URL。

How do I correct this?

我怎么改正这个?

3 个解决方案

#1


2  

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.

检查bash手册中单引号和双引号之间的差异。

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

编辑:实际上您在wget命令行中有一些错误。这是正确的线。

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

load-cookies选项将文件作为参数,而不是字符串。我们必须使用-header选项与Cookie: oraclelicense=accept-securebackup-cookie。在测试之后,我发现wget并不关心header字段中的空格。所以我们可以直接使用Cookie:oraclelicense=accept-securebackup-cookie

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

如果您使用—debug选项,您将看到正确的格式化请求:

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

#2


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"

#3


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:

另外,在命令中,需要在jdk_download_url前面加上$:

/usr/bin/wget $wget_opts $jdk_download_url

#1


2  

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.

检查bash手册中单引号和双引号之间的差异。

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

编辑:实际上您在wget命令行中有一些错误。这是正确的线。

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

load-cookies选项将文件作为参数,而不是字符串。我们必须使用-header选项与Cookie: oraclelicense=accept-securebackup-cookie。在测试之后,我发现wget并不关心header字段中的空格。所以我们可以直接使用Cookie:oraclelicense=accept-securebackup-cookie

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

如果您使用—debug选项,您将看到正确的格式化请求:

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

#2


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"

#3


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:

另外,在命令中,需要在jdk_download_url前面加上$:

/usr/bin/wget $wget_opts $jdk_download_url