2017-02-28 15 views
1

私はコマンドラインでcurlコマンドで直接変数cookieの値を使用しています。スクリプト内では機能しません。次のエラー:curlはコマンドラインで動作しますが、シェルスクリプト内では動作しません

#!/bin/bash 

cookie=`tail -1000 cat.txt | grep -v "wm-ueug" | grep -v "JRECookie" | grep "JSESSIONID.*_ga=GA" | tail -1 | sed -r 's/.{26}//' | sed 's/.$//'` 

`curl "http://example.com/monitor?method=monitor&refresh=true&count=4&start=1&dateFrom=2017-02-21&dateTo=2017-02-28&runId=&hidden_status=&dojo.preventCache=1488290723103" -H "Host: example.com" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20400202 Firefox/50.0" -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" -H "Accept-Language: en-US,en;q=0.5" --compressed -H "Content-Type: application/x-www-form-urlencoded" -H "X-Requested-With: XMLHttpRequest" -H "Referer: http://example.com/monitor" -H "Cookie: $cookie" -H "Connection: keep-alive"` 

更新:削除されたバックティック - 現在はエラーは表示されず、出力はありません。

# Not `curl ...` 
curl ... 

しかし、あなたはそれをより読みやすく、理解しやすくするためにcurlコマンドを壊すすることもできます。作成する

+0

おそらく、あなたは 'curl'コマンドの回りに' backticks 'を失うべきです。 –

+1

バッククエストは単にコマンドを実行するためのものではありません。それらは、別の式の値として使用するコマンドの出力をキャプチャする*ためのものです。 – chepner

+0

カールの周りのバックティックを削除しました - 今はエラーは出ませんが、まだ出力はありません。注:コマンドラインで動作しています。 – Koshur

答えて

1

主な変更点は、コマンドsubsitutionのcurl外を実行することです。

#!/bin/bash 

cookie=$(tail -1000 cat.txt | 
     grep -v "wm-ueug" | 
     grep -v "JRECookie" | 
     grep "JSESSIONID.*_ga=GA" | 
     tail -1 | sed -r 's/.{26}//' | sed 's/.$//') 

# Parameters can be passed to curl via the -d option, rather 
# than as a query string in the URL. 
url="http://example.com/monitor" 
parameters=(
    -d method=monitor 
    -d refresh=true 
    -d count=4 
    -d start=1 
    -d dateFrom=2017-02-21 
    -d dateTo=2017-02-28 
    -d runId=hidden_status 
    -d dojo.preventCache=1488290723103 
) 

headers=(
    -H "Host: example.com" 
    -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20400202 Firefox/50.0" 
    -H "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 
    -H "Accept-Language: en-US,en;q=0.5" 
    -H "Content-Type: application/x-www-form-urlencoded" 
    -H "X-Requested-With: XMLHttpRequest" 
    -H "Referer: http://example.com/monitor" 
    -H "Cookie: $cookie" 
    -H "Connection: keep-alive" 
) 
curl --compressed "${parameters[@]}" "${headers[@]}" "$url" 
+0

今はエラーは表示されませんが出力はありません。 – Koshur

+1

@Koshurは '何が起きているのかを見るためにcurlコマンドに 'v'フラグを立てます。サーバがリダイレクトで応答し、 'curl'にそれらに従うように要求していない場合、空の応答が予想されますが、それは単なる可能性です。 – Aaron

関連する問題