症状
curlコマンドで以下URLを叩いたところ、バッチファイルから起動した場合とコマンドプロンプト直打ちの場合とで結果異なる。
bad_pattern.cmd
curl -v "https://www.google.com/search?q=test%252sample"
バッチファイルから実行した結果
curl -v "https://www.google.com/search?q=test%%252sample" |find "GET"
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Host www.google.com:443 was resolved.
* IPv6: (none)
* Connected to www.google.com (142.250.198.4) port 443
* schannel: disabled automatic use of client certificate
* ALPN: curl offers http/1.1
* ALPN: server accepted http/1.1
* using HTTP/1.x
+ > GET /search?q=test%252sample HTTP/1.1
コマンドを直に実行した場合
>bad_pattern.cmd
>curl -v "https://www.google.com/search?q=test52sample"
* Host www.google.com:443 was resolved.
* IPv6: (none)
* IPv4: 142.250.198.4
* Trying 142.250.198.4:443...
* Connected to www.google.com (142.250.198.4) port 443
* schannel: disabled automatic use of client certificate
* ALPN: curl offers http/1.1
* ALPN: server accepted http/1.1
* using HTTP/1.x
+ > GET /search?q=test52sample HTTP/1.1
原因
バッチファイル内では「%2」はコマンドライン引数であるため%2=""となり求めるURLにならない
対処方法
バッチファイルを以下のように修正する。
- curl -v "https://www.google.com/search?q=test%252sample"
+ curl -v "https://www.google.com/search?q=test%%252sample"
一言
これだけで2時間ぐらい潰れました。
メモ帳で編集していたのが長引いた原因でした。
VSCODE使っていれば、シンタックスハイライトでもうちょっと早く気づけたと思います。
参考
引数の指定とバッチファイル内で引数を参照する方法
https://www.javadrive.jp/command/bat/index6.html