Ifdown Wlan0. Replace ifconfig wlan0 down in your script with ifdown wlan0 and ifco