1

I ran my shell script on rpi 3. But I got the following error.

script.sh: 51: script.sh: Syntax error: "(" unexpected

my script lines is following.

51: lines_to_be_removed=("allow-hotplug wlan0" "iface wlan0 inet manual" "wpa-conf \/etc\/wpa_supplicant\/wpa_supplicant.conf")

52: remove_lines(){
53:    for i in "${lines_to_be_removed[@]}"
54:    do
55:        sudo sed -i "/$i/,1 d" /etc/network/interfaces
56:    done
57: }
Inian
  • 80,270
  • 14
  • 142
  • 161
Pavlov
  • 11
  • 2
  • 2
    Please take a look: http://www.shellcheck.net/ – Cyrus Dec 10 '17 at 17:53
  • 1
    Check lines before 51. – Cyrus Dec 10 '17 at 17:54
  • what do you mean? do you need to see lines before 51? – Pavlov Dec 10 '17 at 17:59
  • 4
    Another possibility is that the shell is not bash. Show how you're invoking the script and what the shebang line is. Also, @Cyrus' advice is excellent: copy and paste your complete code (but not the line numbers) into [shellcheck.net](http://www.shellcheck.net/). – John1024 Dec 10 '17 at 18:15
  • @Pavlov: yes, add some lines before number 51. – Cyrus Dec 10 '17 at 18:19
  • #!/bin/bash clear sleep 5s # Change value for Hotspot name NETWORK="RPI3-HOTsPOT" # Change value for Hotspot password PASSWORD="rpi3hotspot" sleep 10s echo echo "Running HotSpot Setup ..." echo echo "Checking for network connection ..." echo #Check for internet connection ping -c 1 www.google.com > /dev/null 2>&1 sleep 10s is_connected=$? if [ "$is_connected" = "0" ]; then echo "Checking for Updates ..." sudo apt-get update > /dev/null if ! { sudo apt-get update 2>&1 || echo E: update failed; } | grep -q '^[WE]:'; then – Pavlov Dec 10 '17 at 18:21
  • sudo pkill apt-get sudo apt-get update echo "Installing updates ..." echo if ! { sudo apt-get update 2>&1 || echo E: update failed; } | grep -q '^[WE]:'; then sudo apt-get upgrade -y else echo "Failed to update. Please restart and check connection" exit 1 fi else echo "Failed to get updates. Please restart and check connection" exit 1 fi – Pavlov Dec 10 '17 at 18:23
  • else echo "Please check network connection..." echo "Exiting ......" echo echo "Please Restart to run this Setup Again ..." echo exit 1 fi – Pavlov Dec 10 '17 at 18:23
  • Hello John, I added my script in shellcheck.net – Pavlov Dec 10 '17 at 18:26
  • 1
    Putting the code here in comments is obviously not very useful. If you don't want to add the code to the actual question (please don't include line numbers!) maybe put it somewhere like https://pastebin.com/ – tripleee Dec 10 '17 at 18:31
  • @Pavlov After you paste your code in shellcheck.net, wait a moment and then shellcheck will display a variety of errors (red) and warnings about your code. Follow shellcheck's suggestions and your code will be much improved. – John1024 Dec 10 '17 at 19:38
  • I don't understand the address range `/$i/,1`. The line that matches /$i/ to line 1? But I'm sure it's unrelated to the Unexpected '(' error. – Cole Tierney Dec 11 '17 at 02:06

1 Answers1

1

Not sure this'll solve the problem, but it may help simplify and streamline the solution. Try calling sed once:

remove_lines() {
    sed -i '
        /allow-hotplug wlan0/d
        /iface wlan0 inet manual/d
        /wpa-conf \/etc\/wpa_supplicant\/wpa_supplicant.conf/d
    ' /etc/network/interfaces
}
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35