Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash - error message 'Syntax error: "(" unexpected'

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
like image 262
rsmith Avatar asked Jun 14 '11 16:06

rsmith


People also ask

What is unexpected end of file syntax error?

Unexpected End of File errors can occur when a file doesn't have the proper closing tags. Sometimes this error can present itself as the white screen of death, or a 500 error.

What is syntax error in Linux?

This error message also surfaces when you are entering commands in the Linux command line for everyday tasks such as copying files manually etc. The main reasons why this error message occurs is either because of bad syntax or problem of the OS in interpreting another system's commands/shell.

What does $() mean Bash?

Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).


1 Answers

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.

like image 160
Rafe Kettler Avatar answered Sep 25 '22 04:09

Rafe Kettler