Bash - 错误消息“语法错误:”(“意外”
由于某种原因,该功能可以正常工作。终端正在输出
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您使用了错误的语法来声明函数。使用这个来代替:
或者这个:
但不能两者都使用。
另外,我看到稍后您使用逗号来分隔参数(
MoveToTarget $SourcePath, $DestPath
)。这也是一个问题。 Bash 使用空格来分隔参数,而不是逗号。去掉逗号,你应该是金色的。You're using the wrong syntax to declare functions. Use this instead:
Or this:
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.我对在 Bash 脚本中定义函数也很陌生。我在 Ubuntu 14.04(Trusty塔尔)。
我不知道为什么,但以关键字
function
开头的定义对我来说永远不起作用。像下面这样的定义
会产生错误消息:
如果我将
{
放在新行上,例如:当我运行脚本时,即使我不调用,它也会打印
Hello.
这个功能根本没有,这也是我们想要的。我不知道为什么这行不通,因为我也看了很多教程,它们都将左大括号放在第一行的末尾。也许这是我们使用的 Bash 版本?无论如何,只是将其放在这里供您参考。
我必须使用 C 风格的函数定义:
并且它按预期工作。
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
produces the error message:
If I put the
{
on a new line like: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:
and it works as expected.
如果遇到“语法错误:”(“意外”,则使用“bash”而不是使用“sh”。
例如:
If you encounter "Syntax error: "(" unexpected", then use "bash" instead of using "sh".
For example:
我有同样的问题。我有时会使用
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 withsh
causes the issue, but running scripts with Dash works fine.