android - "function: not found" error under Termux sh -
after starting script testyon.sh:
#!/bin/sh function yon { while true; echo "start proc?[y/n]: " read -r "[y/n]: " yn case $yn in [yy]*) echo "starting" ; return 0 ;; [nn]*) echo "stopped" ; return 1 ;; esac done }
i getting error:
$ sh testyon.sh testyon.sh: 2: testyon.sh: function: not found testyon.sh: 7: testyon.sh: syntax error: newline unexpected (expecting ")") $
how solve this?
i guess whatever shell run when call sh
thrown off function syntax. portable way of declaring function is
yon () { while true; echo "start proc?[y/n]: " read -r "[y/n]: " yn case $yn in [yy]*) echo "starting" ; return 0 ;; [nn]*) echo "stopped" ; return 1 ;; esac done }
reference: posix spec, shell command language, function definition command.
two remarks:
- you seem prompt user twice, i'm not sure if prompt in
read -r
command @ all. seems prevent readingyn
in first place. - termux comes full blown bash living @
/data/data/com.termux/files/usr/bin/bash
, prevent function problem.
Comments
Post a Comment