[Permission Issue] Running Script on android bootup

I am trying to run a script once android boots up so it will always checks for pin 37’s status to decide if devices needs to go into sleep or not, and i followed the process mentioned by @Terry in another thread Launch a script at boot - #5 by Terry
and created my userinit file with the right commands (copied from another user), the userinit file has 777 permission as well.

However when the script runs i get the following errors:

/system/etc/init.d/userinit[2]:
: not found
/system/etc/init.d/userinit[4]: can’t create /sys/class/gpio/export
: Permission denied
/system/etc/init.d/userinit[5]: can’t create /sys/class/gpio/gpio431/direction
: No such file or directory
/system/etc/init.d/userinit[16]: syntax error: unexpected 'done’
run-parts:/system/etc/init.d/userinit: exit status 1
/system/bin/preinstall.sh[12]: syntax error: unmatched 'if’

how can i make sure that permission is there when this script runs ? userinit has 777 permission, is there something else that i need to do to make this work ?

here is userinit codes:

#!/system/bin/sh
echo ‘Start Power Service…’
echo 431 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio431/direction
while true
do
pwrctrl=$( cat /sys/class/gpio/gpio431/value )
if [ “$pwrctrl” = 0 ]; then
echo ‘mem’ > /sys/power/state
echo ’ Set sleep…’

fi
sleep 1

done

You need to change the permission of /sys/class/gpio/export

# chmod 777 /sys/class/gpio/export