chiccop96
Posts: 6
Joined: Thu Apr 26, 2018 1:09 pm

Script Problem

Thu Apr 26, 2018 1:33 pm

Hi Everybody,

I'm new in raspberry languages and I apologise for not writing in the right section.

I have a problem on writing this script:

Code: Select all

#!/bin/bash
#=================================================================
# Script Variables Settings
wlan='wlan0'
gateway='8.8.4.4'
alias ifup='/sbin/ifup'
alias ifdown='/sbin/ifdown'
alias ifconfig='/sbin/ifconfig'
# Only send two pings, sending output to /dev/null as we don't want to fill logs on our sd card. 
# If you want to force ping from your wlan0 you can connect next line and uncomment second line 
ping -c2 ${gateway} > /dev/null # ping to gateway from Wi-Fi or from Ethernet
# ping -I ${wlan} -c2 ${gateway} > /dev/null # only ping through Wi-Fi 
# If the return code from ping ($?) is not 0 (meaning there was an error)
if [ $? != 0 ]
then
ifdown --force wlan0
ifup wlan0
sleep 5
ifup wlan0;
fi
ping -I ${wlan} -c2 ${gateway} > /dev/null
echo " - Auto Reconnect Wi-Fi Status for $wlan Script Ended ";
Trying to executing in the terminal it gives me this error:

/home/pi/Scripts/check_wifi.sh: 22: /home/pi/Scripts/check_wifi.sh: Syntax error: end of file unexpected (expecting "then")

I also tried to change the command sh ....Script.... in bash.....script.... but i always have the problem
syntax error: unexpected end of file[/i ]

What can it be?

B.Goode
Posts: 6442
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Script Problem

Thu Apr 26, 2018 1:47 pm

Maybe the problem is here -

Code: Select all

if [ $? != 0 ]

See a bash shell tutorial, such as

https://linuxacademy.com/blog/linux/con ... tatements/

or

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html

chiccop96
Posts: 6
Joined: Thu Apr 26, 2018 1:09 pm

Re: Script Problem

Thu Apr 26, 2018 2:27 pm

B.Goode wrote:
Thu Apr 26, 2018 1:47 pm
Maybe the problem is here -

Code: Select all

if [ $? != 0 ]

See a bash shell tutorial, such as

https://linuxacademy.com/blog/linux/con ... tatements/

or

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-6.html
Tried what you said and also what it's written in the link you send me but it gives me always the same error.

B.Goode
Posts: 6442
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Script Problem

Thu Apr 26, 2018 2:30 pm

Tried what you said and also what it's written in the link you send me but it gives me always the same error.
What exactly have you tried?

What does your code look like now?

What is the exact error message?

pcmanbob
Posts: 4353
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Script Problem

Thu Apr 26, 2018 3:06 pm

I would have expected there to be some indentation after the then, for the lines of code to be executed by the then part of the code.

Code: Select all

#!/bin/bash
#=================================================================
# Script Variables Settings
wlan='wlan0'
gateway='8.8.4.4'
alias ifup='/sbin/ifup'
alias ifdown='/sbin/ifdown'
alias ifconfig='/sbin/ifconfig'
# Only send two pings, sending output to /dev/null as we don't want to fill logs on our sd card. 
# If you want to force ping from your wlan0 you can connect next line and uncomment second line 
ping -c2 ${gateway} > /dev/null # ping to gateway from Wi-Fi or from Ethernet
# ping -I ${wlan} -c2 ${gateway} > /dev/null # only ping through Wi-Fi 
# If the return code from ping ($?) is not 0 (meaning there was an error)
if [ $? != 0 ]
then
    ifdown --force wlan0
    ifup wlan0
    sleep 5
    ifup wlan0;
fi
ping -I ${wlan} -c2 ${gateway} > /dev/null
echo " - Auto Reconnect Wi-Fi Status for $wlan Script Ended ";
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

B.Goode
Posts: 6442
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Script Problem

Thu Apr 26, 2018 7:58 pm

Trying to executing in the terminal it gives me this error:
How, exactly, are you executing the script?

Because if I execute the script you first posted on my RPi3 running Raspbian it runs without error...

chiccop96
Posts: 6
Joined: Thu Apr 26, 2018 1:09 pm

Re: Script Problem

Fri Apr 27, 2018 9:58 am

I created a file called wifi.sh in a folder and then I try to bash it doing:

sh path.../wifi.sh

and also

bash path...wifi.sh

Both time it gets me error or at the end of the script or in the if part

pcmanbob
Posts: 4353
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Script Problem

Fri Apr 27, 2018 11:38 am

Your original script works ok for me when run on my pi3B+ running stretch lite.

so script I called tw.sh and saved it in /home/pi , I made the script executable
then called it at the command line with ./tw.sh

did you buy any chance write the script on a windows pc and then copy it over to the pi, this can some times cause strange problems with the hidden \r\n that windows uses as the end of line, rather than the \n that Linux uses.
Remember we want information.......................no information no help
The use of crystal balls & mind reading is not supported

Return to “General programming discussion”

Who is online

Users browsing this forum: No registered users and 6 guests