Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message "syntax error near unexpected token `('"

Tags:

bash

shell

I am trying to execute

sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force application (1995)

but I get this error:

bash: syntax error near unexpected token `('

However,

sudo -su db2inst1 id

gives me correct output. So it must be something about the ()

If I try

sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force application \(1995\)

I get

/bin/bash: -c: line 0: syntax error near unexpected token (' \ /bin/bash: -c: line 0: /opt/ibm/db2/V9.7/bin/db2 force application (1995)'

Running /opt/ibm/db2/V9.7/bin/db2 force application (1995) as db2inst1 user gives me the same error, but running

/opt/ibm/db2/V9.7/bin/db2 "force application (1995)"

works fine


The right syntax is

sudo -su db2inst1 '/opt/ibm/db2/V9.7/bin/db2 "force application (1995)"'
like image 861
Radek Avatar asked Apr 06 '11 00:04

Radek


People also ask

What Is syntax error near unexpected token?

The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.

How do I fix an unexpected token?

As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.

What is syntax error in bash?

The syntax error is because of (). Remove () from the file like this: #!/bin/bash function hello { echo "Hello world" } or you can just run the following command to edit the file for you: sed -i 's/() //g' hello.sh. You should now be able to run the file with the desired result.

What is #/ bin bash?

Now to explicitly specify the type of shell used by the script, Shebang is used. So we can use shebang, that is, #!/bin/bash at the start or top of the script to instruct our system to use bash as a default shell. Let us consider it with the help of an example. A script is nothing but several commands.


2 Answers

NOTE: While this answer seems to have been correct at the time [sudo was changed later that same year to add extra escaping around characters in the arguments with -i and -s], it is not correct for modern versions of sudo, which escape all special characters when constructing the command line to be passed to $SHELL -c. Always be careful and make sure you know what passing a command to your particular version of sudo will do, and consider carefully whether the -s option is really needed for your command and/or, if it would, if you'd be better served with sudo sh -c.


Since you've got both the shell that you're typing into and the shell that sudo -s runs, you need to quote or escape twice. Any of the following three would have worked with this now-ancient version of sudo:

sudo -su db2inst1 '/opt/ibm/db2/V9.7/bin/db2 "force application (1995)"'
sudo -su db2inst1 '/opt/ibm/db2/V9.7/bin/db2 force\ application\ \(1995\)'
sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force\\ application\\ \\\(1995\\\)

Out of curiosity, why do you need -s? Can't you just do the following?

sudo -u db2inst1 /opt/ibm/db2/V9.7/bin/db2 'force application (1995)'
sudo -u db2inst1 /opt/ibm/db2/V9.7/bin/db2 force\ application\ \(1995\)
like image 88
Random832 Avatar answered Oct 10 '22 11:10

Random832


Try

sudo -su db2inst1 /opt/ibm/db2/V9.7/bin/db2 force application \(1995\)
like image 23
user541686 Avatar answered Oct 10 '22 09:10

user541686