0

I am trying to combine applescript with bash script but its not working for some reason. Can someone correct my syntax error please? (when executing its saying line 18: syntax error: unexpected end of file there is no line 18??)

  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

So the aim is to look for a site, grep a specific message if true reload google chrome.

UPDATE: So I manage to get it to implement the appscript with a function but its keep spiting out echo connected even if the site is down. This is updated code:

 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

Any reason why?

5
  • 1
    a semicolon is missing before then Commented Jun 1, 2017 at 15:27
  • BTW, running this through shellcheck.net would probably have spotted it without getting humans involved. Commented Jun 1, 2017 at 15:35
  • If i do 'grep "server down"; then' it still errors with 'syntax error: unexpected end of file' Commented Jun 1, 2017 at 15:43
  • you must not use brackets here the command is wget, also the 2>/dev/null redirection masks errors Commented Jun 1, 2017 at 15:45
  • when [] removed it still moved to echo "connected". Could i have a example on your answer? Commented Jun 1, 2017 at 15:54

1 Answer 1

1

The error message may be due to the semicolon missing before then.

The interpreter doesn't recognize then as a keyword but as an argument.

With bash the error message is more clear because when it fails encountering fi keyword.

 syntax error near unexpected token `fi'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.