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 reading yn in first place.
  • termux comes full blown bash living @ /data/data/com.termux/files/usr/bin/bash, prevent function problem.

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -