Linux bash 语法错误:“(”意外

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6347119/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:31:48  来源:igfitidea点击:

bash Syntax error: "(" unexpected

linuxmacosbashscriptingsyntax-error

提问by rsmith

For some reason this function is working properly, the terminal is outputting

由于某种原因,此功能工作正常,终端正在输出

newbootstrap.sh: 2: Syntax error: "(" unexpected

Here is my code (line 2 is function MoveToTarget() {)

这是我的代码(第 2 行是函数MoveToTarget() {

#!/bin/bash  
function MoveToTarget() {
    #This takes to 2 arguments: source and target
    cp -r -f "" ""
    rm -r -f ""
}

function WaitForProcessToEnd() {
    #This takes 1 argument. The PID to wait for
    #Unlike the AutoIt version, this sleeps 1 second
    while [ $(kill -0 "") ]; do
            sleep 1
    done
}

function RunApplication() {
    #This takes 1 application, the path to the thing to execute
    exec ""
}

#our main code block
pid=""
SourcePath=""
DestPath=""
ToExecute=""
WaitForProcessToEnd $pid
MoveToTarget $SourcePath, $DestPath
RunApplication $ToExecute
exit

回答by Rafe Kettler

You're using the wrong syntax to declare functions. Use this instead:

您使用错误的语法来声明函数。改用这个:

MoveToTarget() {
    # Function
}

Or this:

或这个:

function MoveToTarget {
    # function
}

But not both.

但不能两者兼而有之。

Also, I see that later on you use commas to separate arguments (MoveToTarget $SourcePath, $DestPath). That is also a problem. bash uses spaces to separate arguments, not commas. Remove the comma and you should be golden.

此外,我看到稍后您使用逗号分隔参数 ( MoveToTarget $SourcePath, $DestPath)。这也是一个问题。bash 使用空格来分隔参数,而不是逗号。删除逗号,你应该是金色的。

回答by yaobin

I'm also new to defining functions in bash scripts. I'm using a bash of version 4.3.11(1):-release (x86_64-pc-linux-gnu)on Ubuntu 14.04.

我也是在 bash 脚本中定义函数的新手。我在Ubuntu 14.04上使用4.3.11(1):-release (x86_64-pc-linux-gnu) 版本的 bash 。

I don't know why but the definition that starts with the keyword functionnever works for me.

我不知道为什么,但以关键字开头的定义对我不起作用function

A definition like the following

如下定义

function check_and_start {
  echo Hello
}

produces the error message:

产生错误信息:

Syntax error: "}" unexpected

If I put the {to a new line like:

如果我把它{放到一个新行,如:

function my_function
{
    echo Hello.
}

It prints a Hello.when I run the script, even if I don't call this function at all, which is also what we want.

Hello.在我运行脚本时打印一个,即使我根本不调用这个函数,这也是我们想要的。

I don't know why this wouldn't work because I also looked at many tutorials and they all put the open curly brace at the end of the first line. Maybe it's the version of bash that we use?? Anyway, just put it here for your information.

我不知道为什么这行不通,因为我也看了很多教程,他们都把开大括号放在第一行的末尾。也许这是我们使用的bash版本?不管怎样,把它放在这里供您参考。

I have to use the C-style function definition:

我必须使用 C 风格的函数定义:

check_and_start() {
  echo 
}

check_and_start World!
check_and_start Hello,\ World!

and it works as expected.

它按预期工作。

回答by user11461797

I had the same issue. I was running scripts on Ubuntu sometimes using sh vs dash. Seems running scripts with sh causes the issue, but running scripts with dash work fine.

我遇到过同样的问题。我有时会使用 sh 与 dash 在 Ubuntu 上运行脚本。似乎使用 sh 运行脚本会导致问题,但使用 dash 运行脚本可以正常工作。