如何在 .bat 脚本中将引号应用于 URL?

如何在 .bat 脚本中将引号应用于 URL?

CMD 中有效的 curl 命令的一般形式是:

curl-o最新.dump-L”https://s3.amazonaws.com/hkpgbackups/[电子邮件保护]/xxxx.dump?AWSAccessKeyId=AKIAJSCBEZJRDOTGNGZQ&Expires=1411489500&签名=%2F5zwQNZNN0H2XSR4wSqQ%2FFExBdI%3D

但是 URL 是动态生成的,所以我必须将其放入 .bat 脚本中。好的,没问题,这是我的 .bat 脚本,理论上应该可以运行。

@echo off

for /f "delims=" %%a in ('heroku pgbackups:url') do @set this_url=%%a
ECHO %this_url%
curl -v -o latest.dump -L %this_url%

但是输出失败了,因为正如您所预料的那样,第一个“&”出现了。 的回显this_url显示了带引号的完整字符串。我该如何附加或强制用引号封装此字符串?

我已尝试但它返回相同类型的错误:

cmd /k ""curl -v -o latest.dump -l" "('heroku pgbackups:url')""

有人可能会问为什么不在 Unix 或 CYGWIN 中执行此操作?我先这样做了。该命令在我的 OS X 个人计算机上运行良好。那么它应该在 CYGWIN 中运行吗?好吧,我遇到了相同类型的错误。

我尝试使用该uri_escape库合并 Perl 脚本并将其插入。但它传递的 URL 无法被 curl 识别。

强制将输出放在单引号内会使 URL 混乱,因为它开始是正确的,但结束的单引号位于 URL 的最后几个字符内,这使得它无法进行卷曲。

heroku pgbackups:urlCMD控制台中的输出是:

"https://s3.amazonaws.com/hkpgbackups/[email protected]/b080.dump?AWSAccess
KeyId=AKIAJSCBEZJRDOTGNGZQ&Expires=1411657655&Signature=MqzqBitAN7MADiLAsdAN4ZQk
RH0%3D"

上面.bat文件的输出是:

https://s3.amazonaws.com/hkpgbackups/[email protected]/b080.dump?AWSAccessK
eyId=AKIAJSCBEZJRDOTGNGZQ
'Expires' is not recognized as an internal or external command,
operable program or batch file.
'Signature' is not recognized as an internal or external command,
operable program or batch file.
* About to connect() to s3.amazonaws.com port 443 (#0)
*   Trying 205.251.243.66...   % Total    % Received % Xferd  Average Speed   Ti
me    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0co
nnected
* Connected to s3.amazonaws.com (205.251.243.66) port 443 (#0)
* SSLv3, TLS handshake, Client hello (1):
} [data not shown]
* SSLv3, TLS handshake, Server hello (2):
{ [data not shown]
* SSLv3, TLS handshake, CERT (11):
{ [data not shown]
* SSLv3, TLS handshake, Server finished (14):
{ [data not shown]
* SSLv3, TLS handshake, Client key exchange (16):
} [data not shown]
* SSLv3, TLS change cipher, Client hello (1):
} [data not shown]
* SSLv3, TLS handshake, Finished (20):
} [data not shown]
* SSLv3, TLS change cipher, Client hello (1):
{ [data not shown]
* SSLv3, TLS handshake, Finished (20):
{ [data not shown]
* SSL connection using AES128-SHA
* Server certificate:
*        subject: C=US; ST=Washington; L=Seattle; O=Amazon.com Inc.; CN=s3.amazo
naws.com
*        start date: 2014-04-12 00:00:00 GMT
*        expire date: 2015-04-13 23:59:59 GMT
*        subjectAltName: s3.amazonaws.com matched
*        issuer: C=US; O=VeriSign, Inc.; OU=VeriSign Trust Network; OU=Terms of
use at https://www.verisign.com/rpa (c)10; CN=VeriSign Class 3 Secure Server CA
- G3
*        SSL certificate verify result: unable to get local issuer certificate (
20), continuing anyway.
> GET /hkpgbackups/[email protected]/b080.dump?AWSAccessKeyId=AKIAJSCBEZJRD
OTGNGZQ HTTP/1.1
> User-Agent: curl/7.21.7 (amd64-pc-win32) libcurl/7.21.7 OpenSSL/0.9.8r zlib/1.
2.5
> Host: s3.amazonaws.com
> Accept: */*
>
< HTTP/1.1 403 Forbidden
< x-amz-request-id: 4566C5AB208C1248
< x-amz-id-2: zF/zr+fbR/pE7nvF7vvUmOdZQeMzjSBI6SPLKH14LGAI5JAb2xyoLhuuDGBDhqcq
< Content-Type: application/xml
< Transfer-Encoding: chunked
< Date: Thu, 25 Sep 2014 14:51:46 GMT
< Server: AmazonS3
<
{ [data not shown]
100   231    0   231    0     0    592      0 --:--:-- --:--:-- --:--:--   704
* Connection #0 to host s3.amazonaws.com left intact
* Closing connection #0
* SSLv3, TLS alert, Client hello (1):
} [data not shown]
'Expires' is not recognized as an internal or external command,
operable program or batch file.
'Signature' is not recognized as an internal or external command,
operable program or batch file.

403 错误表明 URL 被混淆了,否则它就通过了,否则我会收到 400 错误。由于脚本中的某种原因,“&”被重定向,因为它们在脚本中被解释。

如果你是我,你会如何运行此命令并动态传递此 URL,以便它不会被 CMD、bash 等解释?谢谢,sam

答案1

查看您最初发布的脚本:

@echo off

for /f "delims=" %%a in ('heroku pgbackups:url') do @set this_url=%%a
ECHO %this_url%
curl -v -o latest.dump -L %this_url%

如果输出ECHO %this_url%与您发布的完全一样,并且带有引号,则您现有的批处理脚本应该可以工作。

如果的值%this_url%没有引号,那么您应该在 curl 命令中添加引号:

curl -v -o latest.dump -L "%this_url%"

您可以修改该命令以使其正常工作,无论是否%this_url%包含引号,方法是删除任何现有引号并明确添加您自己的引号:

curl -v -o latest.dump -L "%this_url:"=%"

您列出的另一个命令在我看来完全错误:

cmd /k ""curl -v -o latest.dump -l" "('heroku pgbackups:url')""

外部引号是不需要的,周围的引号也不需要"curl -v -o latest.dump -l"。您可以只在 exe 文件周围加上引号"curl",但除非您包含 curl 的路径并且该路径包含空格或有毒字符,否则不需要这样做。

我根本就不明白其"('heroku pgbackups:url')"语法。

相关内容