Bash 脚本错误:“函数:未找到”。为什么会出现这个?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12468889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash script error: "function: not found". Why would this appear?
提问by E T
I'm trying to run a bash script
on my Ubuntu machine and it is giving me an error:
我正在尝试bash script
在我的 Ubuntu 机器上运行一个,但它给了我一个错误:
function not found
未找到功能
To test, I created the following script which works fine on my laptop but not on my Desktop. Any ideas as to why? My laptop is a mac if that's relevant.
为了测试,我创建了以下脚本,它在我的笔记本电脑上运行良好,但在我的台式机上运行良好。关于为什么的任何想法?如果相关的话,我的笔记本电脑是 mac。
#!/bin/bash
function sayIt {
echo "hello world"
}
sayIt
This returns "hello world" on my laptop, but on my Desktop it returns:
这在我的笔记本电脑上返回“hello world”,但在我的桌面上它返回:
run.sh: 3: function not found hello world run.sh: 5: Syntax error: "}" unexpected
run.sh: 3: 找不到函数 hello world run.sh: 5: 语法错误:“}”意外
回答by Ned Deily
Chances are that on your desktop you are not actually running under bash
but rather dash
or some other POSIX-compliant shell that does not recognize the function
keyword. The function
keyword is a bashism, a bash extension. POSIX syntax does not use function
and mandates the use of parenthesis.
很可能在您的桌面上,您实际上并没有在运行,bash
而是在dash
其他一些无法识别function
关键字的POSIX 兼容 shell下运行。该function
关键字是bashism,一个bash扩展。POSIX 语法不使用function
并且强制使用括号。
$ more a.sh
#!/bin/sh
function sayIt {
echo "hello world"
}
sayIt
$ bash a.sh
hello world
$ dash a.sh
a.sh: 3: function: not found
hello world
a.sh: 5: Syntax error: "}" unexpected
The POSIX-syntax works in both:
POSIX 语法适用于以下两种情况:
$ more b.sh
#!/bin/sh
sayIt () {
echo "hello world"
}
sayIt
$ bash b.sh
hello world
$ dash b.sh
hello world
回答by Prateek Joshi
I faced the same problem, I then modified the syntax and it worked for me. Try to removethe keyword functionand add brackets ()after the function name.
我遇到了同样的问题,然后我修改了语法,它对我有用。尽量去掉关键字function,在函数名后加括号()。
#!/bin/bash
sayIt()
{
echo "hello world"
}
sayIt
回答by Juda Barnes
ls -la /bin/sh
ls -la /bin/sh
check the sym link where it point to bash or dash
检查它指向 bash 或 dash 的符号链接
回答by Piotr Wadas
Doesn't it require () after function name, or at the call?
在函数名之后或调用时不需要 () 吗?
function sayIt() { ...
}
sayIt()
? :)
? :)
Hmm, actually, on MY mac, it works just as you pasted..
嗯,实际上,在我的 mac 上,它和你粘贴的一样工作..
dtpwmbp:~ pwadas$ cat aa.sh
#!/bin/bash
function sayIt() {
echo "hello world"
}
sayIt
dtpwmbp:~ pwadas$ ./aa.sh
hello world
dtpwmbp:~ pwadas$
Compare bash version, AFAIR some older version required "()"s.
比较 bash 版本,AFAIR 一些旧版本需要“()”。
dtpwmbp:~ pwadas$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin12)
Copyright (C) 2007 Free Software Foundation, Inc.
dtpwmbp:~ pwadas$
Also compare state of shopt options ( man bash ), on both shells, maybe one of them have some compat syntax turned on or off ? "shopt" command without args will list state of options supported.
还要比较 shopt 选项( man bash )的状态,在两个 shell 上,也许其中一个打开或关闭了一些兼容语法?不带参数的“shopt”命令将列出支持的选项状态。