Bash脚本错误:"功能:找不到&QUOT ;.为什么会出现? [英] Bash script error: "function: not found". Why would this appear?

查看:134
本文介绍了Bash脚本错误:"功能:找不到&QUOT ;.为什么会出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的Ubuntu的机器上运行 bash脚本,这是给我的错误:

I'm trying to run a bash script on my Ubuntu machine and it is giving me an error:

找不到功能

要测试,我创建了下面的脚本,它工作在我的笔记本电脑却没有关于我的桌面罚款。任何想法,为什么?我的笔记本是一台Mac,如果这是相关的。

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.

#!/bin/bash

function sayIt {   
   echo "hello world"
}

sayIt

这将返回的Hello World在我的笔记本电脑,但在我的台式机返回的是:

This returns "hello world" on my laptop, but on my Desktop it returns:

run.sh:3:功能未找到的Hello World run.sh:5:语法错误:
  }意外

run.sh: 3: function not found hello world run.sh: 5: Syntax error: "}" unexpected

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

推荐答案

有机会,您的桌面上,你实际上并没有在庆典而是破折号或其他兼容P​​OSIX的外壳,不承认函数关键字。在函数关键字是一个bashism,一个bash扩展。 POSIX语法不使用函数并授权使用括号。

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.

$ 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

在POSIX语法工作在两个:

The POSIX-syntax works in both:

$ more b.sh
#!/bin/sh

sayIt () {   
   echo "hello world"
}

sayIt
$ bash b.sh
hello world
$ dash b.sh
hello world

这篇关于Bash脚本错误:"功能:找不到&QUOT ;.为什么会出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆