Bash - 错误消息“语法错误:”(“意外”

发布于 2024-11-15 10:00:24 字数 786 浏览 2 评论 0原文

由于某种原因,该功能可以正常工作。终端正在输出

newbootstrap.sh:2:语法错误:“(”意外

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

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

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

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

# Our main code block
pid="$1"
SourcePath="$2"
DestPath="$3"
ToExecute="$4"
WaitForProcessToEnd $pid
MoveToTarget $SourcePath, $DestPath
RunApplication $ToExecute
exit

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() {)

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

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

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

# Our main code block
pid="$1"
SourcePath="$2"
DestPath="$3"
ToExecute="$4"
WaitForProcessToEnd $pid
MoveToTarget $SourcePath, $DestPath
RunApplication $ToExecute
exit

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终止放荡 2024-11-22 10:00:24

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

MoveToTarget() {
    # Function
}

或者这个:

function MoveToTarget {
    # function
}

但不能两者都使用。

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

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.

情魔剑神 2024-11-22 10:00:24

我对在 Bash 脚本中定义函数也很陌生。我在 Ubuntu 14.04(Trusty塔尔)。

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

像下面这样的定义

function check_and_start {
  echo Hello
}

会产生错误消息:

语法错误:“}”意外

如果我将 { 放在新行上,例如:

function my_function
{
    echo Hello.
}

当我运行脚本时,即使我不调用,它也会打印 Hello.这个功能根本没有,这也是我们想要的。

我不知道为什么这行不通,因为我也看了很多教程,它们都将左大括号放在第一行的末尾。也许这是我们使用的 Bash 版本?无论如何,只是将其放在这里供您参考。

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

check_and_start() {
  echo $1
}

check_and_start World!
check_and_start Hello,\ World!

并且它按预期工作。

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 (Trusty Tahr).

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

A definition like the following

function check_and_start {
  echo Hello
}

produces the error message:

Syntax error: "}" unexpected

If I put the { on 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.

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.

I have to use the C-style function definition:

check_and_start() {
  echo $1
}

check_and_start World!
check_and_start Hello,\ World!

and it works as expected.

╰沐子 2024-11-22 10:00:24

如果遇到“语法错误:”(“意外”,则使用“bash”而不是使用“sh”。

例如:

bash install.sh 

If you encounter "Syntax error: "(" unexpected", then use "bash" instead of using "sh".

For example:

bash install.sh 
独﹏钓一江月 2024-11-22 10:00:24

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

I had the same issue. I was running scripts on Ubuntu sometimes using sh vs. Dash. It seems running scripts with sh causes the issue, but running scripts with Dash works fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文