2017-06-01 121 views
0

我試圖將applescript與bash腳本結合起來,但由於某種原因它不工作。有人可以糾正我的語法錯誤嗎? (執行它說line 18: syntax error: unexpected end of file時,有沒有線18 ??)如何將蘋果腳本與bash腳本結合使用

2 #!/bin/sh 
    3 declare -a sites 
    4 sites=("www.google.com") 
    5 for site in "${sites[@]}"; do 
    6  if ! wget "$site" --timeout 1 -O - 2> /dev/null | grep "server down" then 
    7   echo "The site is down" 
    8 
    9 osascript <<EOF 
    10 tell application "Google Chrome" to tell the tab of its window 
    11  reload 
    12 end tell 
    13 EOF 
    14 
    15 
    16 fi 
    17 done 

所以其目的是尋找一個網站,用grep特定的消息如果屬實重裝谷歌瀏覽器。

更新: 所以我設法讓它實現一個功能的appscript,但它仍然吐出回聲連接即使網站已關閉。這是更新代碼:

2 #!/bin/sh 
3 
4 function ex_app_scpt() { 
5 `osascript <<-AppleScript 
6 tell application "Google Chrome" to tell the tab of its window 
7  reload 
8 end tell 
9 return quit 
10 AppleScript` 
11 } 
12 
13 declare -a sites 
14 
15 
16  if [ /usr/bin/wget "www.google.com" --timeout 1 -O - 2> /dev/null | grep "server down" ]; then 
17   echo "The site is down" 
18   ex_app_scpt 
19  else 
20   echo "connected" 
21  fi 

任何原因爲什麼?

+1

分號之前'then' –

+0

失蹤BTW,通過http://shellcheck.net/運行,這將可能已經沒有捲入人類發現它。 –

+0

如果我做'grep'服務器關閉';然後'它仍然錯誤'語法錯誤:文件的意外結束' – tranmaster

回答

1

錯誤消息可能是由於之前丟失的分號造成的。

解釋程序不會將其識別爲關鍵字,而是作爲參數。

隨着bash的錯誤信息更加清晰,因爲它在遇到關鍵字fi時失敗。

syntax error near unexpected token `fi' 
+0

斑點! –