5

I have checked the previous post on this error. still did not get the solution working. Here is my bash script. can some one help me figure out the issue . I have used https://www.shellcheck.net/ to see any error. I did not find any.

Error:

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

script:

#!/bin/bash
function BuildSimpleContainer ()
{
docker search mariadb
docker pull mariadb:latest
docker run --name mariadbtestfour -e MYSQL_ROOT_PASSWORD=mypass -d mariadb --log-bin --binlog-format=MIXED
docker exec -it mariadbtest bash
apt-get -qq update
apt-get -qq -y install curl
apt-get -qq -y install wget
apt-get update
apt-get install apt-transport-https
apt-get update
cd /home
mkdir mdsd
cd mdsd/
wget '<blob url to pfx file>'
echo "certififcate Downloaded"
wget '<blob url file1>'
echo "file1 Downloaded"
wget 'blob url file2'
echo "file2 Downloaded"
}   
BuildSimpleContainer
4
  • The error message is pretty self-explanatory: the ( shouldn't be there on line 2.
    – devius
    Commented Feb 26, 2018 at 19:31
  • 3
    How do you call this script?
    – user147505
    Commented Feb 26, 2018 at 19:34
  • 1
    @tomasz has hit it right on the nose. The script is being executed by sh, not bash, and is choking on the function declaration syntax.
    – DopeGhoti
    Commented Feb 26, 2018 at 19:38
  • 1
    Related: difference between "function foo() {}" and "foo() {}" Commented Feb 26, 2018 at 20:53

2 Answers 2

10

My guess is that you don't run this script as a Bash script, but with some other shell, which doesn't accept this syntax. Eg. sh.

As you can see in Shellcheck, and as Bash's man on my Debian confirms, this syntax is correct:

name () compound-command [redirection]
function name [()] compound-command [redirection]

You are using the second one from the two above, and your compound-command is the contents of the {...} braces. And that's fine as far as man bash goes. But then, there are voices against this construct, as in the link in John Moon's comment below the other answer. And in the link to Greg's Wiki in that answer again.

Going back to your question, taking into consideration the name of the script, namely Dockerfunctiontest.sh, I might think you don't see much difference between sh and bash. Probably you run the script with this command:

sh Dockerfunctiontest.sh

While it would be reasonable to run it with either:

bash Dockerfunctiontest.sh

or

./Dockerfunctiontest.sh

The last one picks the interpreter basing on the first line in the script, namely:

#!/bin/bash

And while the interpreter named in the script is Bash, why not name the file the proper way?

Dockerfunctiontest.bash

Less confusion in the future.

-2

It's simple! Just remove the ( and ) from the declaration and try again, as difference between "function foo() {}" and "foo() {}" says.

2
  • 3
    Why are you recommending the least portable version? The POSIX variant without the word function would seem to be the canonical variant Commented Mar 7, 2022 at 13:08
  • 1
    How do you know they're running that with a Korn-compatible shell then? Also, the error message is exactly what Dash would give, and it only supports the POSIX-style function definition. function foo would be valid syntax in Dash, but it'd try to run a command called function. (Dash is /bin/sh in Debian and Ubuntu.)
    – ilkkachu
    Commented Mar 7, 2022 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .