Script to control GPU fans in Ubuntu 16.10

Hello,

Please have a look at the script below.

#!/bin/bash

nvidia-settings -a "GPUFanControlState=1"

while true
do

gputemp=`nvidia-settings -q GPUCoreTemp |awk -F ":" 'NR==2{print $3}' |sed 's/[^0-9]*//g'`

case "${gputemp}" in
        3[0-9])
                newfanspeed="0"
                ;;
        4[0-9])
                newfanspeed="15"
                ;;
        5[0-9])
                newfanspeed="30"
                ;;
        6[0-9])
                newfanspeed="50"
                ;;
        7[0-9])
                newfanspeed="70"
                ;;
        *)
                newfanspeed="100"
                ;;
esac

nvidia-settings -a "GPUTargetFanSpeed=${newfanspeed}" 2>&1 >/dev/null

sleep 5s

done

This is the error

ERROR: Error assigning value 15 to attribute 'GPUTargetFanSpeed' (matteo:0[fan:0]) as specified in assignment
       'GPUTargetFanSpeed=15' (Unknown Error).

I cannot understand what I am using wrong.
I am using the standard nvidia drivers which are coming with ubuntu 16.10 64bit, which are the 367.57. My GPU is a Gigabyte 1070 G1.

If you know the solution I would greatly appreciate it.
Thank you