1

Here is my shell script:

if [ $# != 2 ] ; then
    echo "$0 <SSID> <passphrase>"
    exit
fi
wpa_cli -iwlan0 disconnect
wpa_cli -iwlan0 remove_network all
wpa_cli -iwlan0 add_network
wpa_cli -iwlan0 set_network 0 mode 0
wpa_cli -iwlan0 set_network 0 ssid \"$1\"
wpa_cli -iwlan0 set_network 0 auth_alg OPEN
wpa_cli -iwlan0 set_network 0 key_mgmt WPA-PSK
wpa_cli -iwlan0 set_network 0 proto RSN
wpa_cli -iwlan0 set_network 0 psk \"$2\"
wpa_cli -iwlan0 set_network 0 scan_ssid 1
wpa_cli -iwlan0 select_network 0
wpa_cli -iwlan0 enable_network 0
wpa_cli -iwlan0 reassociate
wpa_cli -iwlan0 status

When I run it, I get the following error: (there are 18 lines of code)

wpacli_connect_wpa2.sh: line 19: syntax error: unexpected end of file

If I run these commands manually everything works fine.

What am I doing wrong?

2
  • 1
    Maybe you have a strange invisible character at the end of the file. happend to me one time... Also there is no need for \", just write " Commented Sep 27, 2015 at 12:41
  • 2
    More than "no need for \"" that is just flat-out incorrect. Commented Sep 27, 2015 at 13:26

1 Answer 1

3

I could reproduce your problem by copy-pasting your script and saving it in DOS mode, so that it has CRLF line endings. Then, I get exactly the same error as you:

line 19: syntax error: unexpected end of file

To fix that, fix the line endings, either by running dos2unix on the script, or something this tr:

tr -d '\r' < script.sh > cleaned.sh && mv cleaned.sh script.sh

And, I suggest to insert this line at the very top:

#!/bin/sh

All shell script should have such line (called the shebang).

Sign up to request clarification or add additional context in comments.

2 Comments

How would that result in unexpected end of file?
That was just a hunch. Since you pushed me, I tested my theory and could reproduce the problem of the OP (see my update). I've no idea why the error message has to be so cryptic. It's rare to see, because usually a script has a shebang, in which case the error message is different, in that case it's a bit easier to see that the problem is the line endings. The OP's script didn't have a shebang, which added to the confusion.

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.