出力をカール応答の末尾に追加しないように、stdoutの別のファイル記述子に出力をリダイレクトするには、curl -wオプションをどのように使用できますか?

出力をカール応答の末尾に追加しないように、stdoutの別のファイル記述子に出力をリダイレクトするには、curl -wオプションをどのように使用できますか?

カール操作を使用して、ダウンロード速度と合計時間が必要ですcurl -w。走れば

curl example.com -w '%{speed_download} %{time_total}'

標準出力は

<!doctype html> <html> <head> <title>Example Domain</title> <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <style type="text/css"> body { background-color: #f0f0f2; margin: 0; padding: 0; font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; } div { width: 600px; margin: 5em auto; padding: 2em; background-color: #fdfdff; border-radius: 0.5em; box-shadow: 2px 3px 7px 2px rgba(0,0,0,0.02); } a:link, a:visited { color: #38488f; text-decoration: none; } @media (max-width: 700px) { div { margin: 0 auto; width: auto; } } </style> </head> <body> <div> <h1>Example Domain</h1> <p>This domain is for use in illustrative examples in documents. You may use this domain in literature without prior coordination or asking for permission.</p> <p><a href="https://www.iana.org/domains/example">More information...</a></p> </div> </body> </html> 2861.000 0.439478

speed_download と time_total が標準出力に追加されます。したがって、そのオプションを使用しない場合は、-w次のようにカール応答を抽出できます。

CURL_RESPONSE=$(curl example.com -w '%{speed_download} %{time_total}')
echo $CURL_RESPONSE | grep -oP ".*(?= ([0-9]+\.[0-9]+ [0-9]+\.[0-9]+$))"

そして同様にspeed_download得てtime_total

echo $CURL_RESPONSE | grep -oP "([0-9]+\.[0-9]+ [0-9]+\.[0-9]+$)"

-w '%{speed_download} %{time_total}'私はこのアプローチを避け、出力をSTDERRなどの他のファイル記述子にリダイレクトし、それを使用せずにSTDOUTを維持したいと思います。本書ではhttps://www.mit.edu/afs.new/sipb/user/ssen/src/curl-7.11.1/docs/curl.htmlそれは言う:

-w/--書く

ジョブが完了して成功した後に表示される内容を定義します。形式は、任意の数の変数と混在するプレーンテキストを含めることができる文字列です。文字列は "string" で指定でき、特定のファイルから読み込むには "@filename" で指定できます。

ファイルから文字列を書く形式を読み取ることができますが、-wを使用せずにカールの通常のSTDOUTがリダイレクトされる場所とは別に表示される内容を制御する方法はありますか?

ベストアンサー1

指示(man curl)によると:

-w、--書く

すべての変数は%{variable_name}で指定されており、通常の%を出力するには%%で書き留めます。 \n を使用して改行文字を出力し、\r を使用してキャリッジリターンを出力し、\t を使用してタブスペースを出力できます。出力は標準出力に書き込まれますが、%{stderr}を使用して標準エラーに切り替えることができます。

したがって、次のコマンドを使用してandをcurl取得できます。speed_downloadtime_total

curl example.com --silent -w '%{stderr} %{speed_download} %{time_total}' 1> /dev/null

別の出力(html)を取得するには、ファイル記述1子またはstdoutファイルをリダイレクトできます。

curl example.com --silent -w '%{stderr} %{speed_download} %{time_total}' 1> somefile

おすすめ記事