2014-10-02 109 views
0

相當noobie問題在這裏..我想。但我不能讓這個腳本工作。它將包含在幾天前我在這裏詢問的腳本中(BASH output column formatting)。基本上我想能夠刮一個網站的文本的一部分,並返回一個在線/離線答案。我很抱歉格式不正確和怪異的變量名稱。感謝您看看並幫助我!Bash IF語句

#!/bin/bash 
printf "" > /Users/USER12/Desktop/domainQueryString_output.txt 
domainCurlRequest="curl https://www.google.com/?gws_rd=ssl" 
ifStatementConditional="grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l" 
echo $($domainCurlRequest) >> /Users/USER12/Desktop/domainQueryString_output.txt 
    if [ $ifStatementConditional -eq 2 ] ; 
     then second_check="online" 
    else second_check="DOMAIN IS OFFLINE" 
    fi 
echo $second_check 

試圖運行此腳本

/Users/USER12/Desktop/domain_status8working.sh: line 6: [: too many arguments 

我試圖重寫另一種方式,但得到了同樣的錯誤,所以我的邏輯或語法或東西是關閉的,當我不斷收到以下錯誤。

再次感謝您的關注和幫助我!

+0

可能你只需要用'$()'來包裝你的curl語句而不是用雙引號 – rthbound 2014-10-02 21:07:12

回答

2

ifStatementConditional = 「grep的 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt |廁所-l」

這是一個字符串賦值。你可能想要反引號,或$()構造。否則,$ ifStatementConditional永遠等於2

+0

謝謝。正是我的問題是。謝謝! @CodeGnome – aLee788 2014-10-02 21:37:50

1
if [ $ifStatementConditional -eq 2 ] ; 

這被擴展爲:

if [ grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l -eq 2 ] ; 

這就解釋了你的錯誤。

我想你意思是:

#!/bin/bash 
curl "https://www.google.com/?gws_rd=ssl" > /Users/USER12/Desktop/domainQueryString_output.txt 
ifStatementConditional=$("grep 'google.com' /Users/USER12/Desktop/domainQueryString_output.txt | wc -l") 

    if [ $ifStatementConditional -eq 2 ] ; then 
     second_check="online" 
    else 
     second_check="DOMAIN IS OFFLINE" 
    fi 
echo $second_check 
  1. 不需要做printf "" > somefile.txt當你做後的捲曲,你附加到文件
  2. $()是捕獲子shell輸出。那裏錯過了什麼。
+0

感謝您指出我在那裏的冗餘printf語句。非常感激! @沒有找到數據 – aLee788 2014-10-02 21:39:47