gpt4 book ai didi

android - Termux sh下"function: not found"错误

转载 作者:行者123 更新时间:2023-11-29 19:08:51 26 4
gpt4 key购买 nike

启动脚本testyon.sh后:

#!/bin/sh 
function yon {
while true; do
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 }

我收到这个错误:

$ sh testyon.sh
testyon.sh: 2: testyon.sh: function: not found
testyon.sh: 7: testyon.sh: Syntax error: newline unexpected (expecting ")")
$

如何解决?

最佳答案

我猜想调用 sh 时运行的任何 shell 都会被函数语法抛出。声明函数的可移植方式是

yon() { 
while true; do
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
}

引用:POSIX 规范,Shell 命令语言,Function Definition Command .

两点说明:

  • 您似乎提示了用户两次,但我不确定 read -r 命令中的提示是否有任何作用。它实际上似乎首先阻止了将任何内容读入 yn
  • Termux 带有一个完整的 Bash,位于 /data/data/com.termux/files/usr/bin/bash,这将防止您的功能问题。

关于android - Termux sh下"function: not found"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46143607/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com