I got 'syntax error: "(" unexpected' (expecting "done") [duplicate] - bash

This question already has answers here:
I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
(3 answers)
Closed 7 years ago.
I have a very simple shell script which I'm using to loop through directories, and call another shell script. I wrote it on my local machine (OS X running Bash 3.2) and am using it on a remote server running Bash 4.2.
On the server, when I type which bash, I get /bin/bash, so I added the line on top. I still get this error, pointing to the line that begins arrIN=...
8: run_all_verification.sh: Syntax error: "(" unexpected (expecting "done")
The shell script:
#!/usr/bin/bash
# Base name for all experiments
BASE_EXP_ID=$1;
for i in ${BASE_EXP_ID}*
do
# Split file name by "__"
arrIN=(${i//__/ });
EXP_ID=${arrIN[0]}
NUM_FEATURES=${arrIN[1]}
echo "${EXP_ID} ${NUM_FEATURES}"
sh run_verification.sh ${EXP_ID} ${NUM_FEATURES}
done

Your error message is from Dash, probably because you ran sh filename.
To run a script with Bash, use bash filename (or ./filename).

Related

Run script with sh or bash [duplicate]

This question already has answers here:
Bash script process substitution Syntax error: "(" unexpected
(3 answers)
shebang not working to run bash scripts in linux
(1 answer)
Syntax error: "(" unexpected -- with !(*.sh) in bash script [duplicate]
(3 answers)
syntax error near unexpected token `<'
(2 answers)
Closed 3 years ago.
I have a .sh file. It includes bash syntax.
#!/bin/bash
function foo() {
// do something
}
doo()
// do something
sh doesn't link to bash on my system.
Below command doesn't work:
sh sample.sh
It throws syntax error. Below command works fine.
bash sample.sh
I think '#!/bin/bash' is useless for my case. I know that sh != bash. But do I must specify sh/bash/etc like upper example to run .sh file?
The shebang (#!) is only used if you execute the file (i.e: ./sample.sh). Of course, the file must have execution permissions.
If you execute sh with your file as argument its content is interpreted by sh (not taking into account the shebang). The same for bash or any other command that reads the file represented by the first argument.

Command substitution with variables gives command not found? [duplicate]

This question already has answers here:
Getting "command not found" error in bash script
(6 answers)
Closed 3 years ago.
I'm having a BASH script:
#!/usr/bin/env bash
PATH=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
YAML=$(envsubst < ${PATH}/test.yml)
echo "${YAML}"
It results:
./test.sh: line 5: envsubst: command not found
I see that having a variable inside command substitution causes such error.
If I'm not using any:
YAML=$(envsubst < ./test.yml)
Then I'm having an expected successful script execution.
I've tried different syntax using quotes all over the place, but nothing helped.
How do I successfully use variable inside command substitution?
Bash version:
GNU bash, version 4.2.46(1)-release (x86_64-redhat-linux-gnu)
UPDATE: A duplicate question. Found an answer here:
https://stackoverflow.com/a/5642584/5935309
The problem is that you are changing PATH, which is used by Bash internally to define where to look for programs (and in which order).
You changed PATH to only contain the current working dir, and that's not where envsubst is located.
The solution is to use something different than PATH, like FILE_PATH.

how to write bch commands in bash file [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
How to assign the output of a Bash command to a variable? [duplicate]
(5 answers)
Closed 4 years ago.
I want to write a bash script to get block count . It is giving error
./script.sh: line 4: =: command not found
Below is my script
#!/bin/bash
# getblockcount
$blockcount = bitcoin-cli getblockcount
echo $blockcount
Kindly tell what is wrong .
There should not be space around the operator.. So remove the space around = and thing should work. Also there are some other bits.. Here is the corrected one.. Ensure command bitcoin-cli getblockcount from terminal gives right result.
#!/bin/bash
# getblockcount
blockcount=$(bitcoin-cli getblockcount)
echo $blockcount

bash: appending to list fails with "not found" [duplicate]

This question already has an answer here:
Syntax error in sh - built and working for bash [duplicate]
(1 answer)
Closed 5 years ago.
I have the following script
list=\(\)
val=123
list+=\(\"$val\"\)
When I copy-paste it to the terminal it works fine, but when I try to run it from inside a .sh file, I get this error:
file.sh: 9: file.sh: list+=("123"): not found
I found this answer here, but it doesn't seem applicable since I'm not using a space.
btw, I use \ in front of ( because otherwise I get Syntax error: "(" unexpected, this also happend only when running from a script file.
What causes the error?
I'm working on GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
You are assigning the literal string () to list, not implicitly declaring it as an empty array.
list=()
val=123
list+=("$val")

`Syntax error: "(" unexpected` when trying to set a list of files as an array variable in bash [duplicate]

This question already has an answer here:
I got 'syntax error: "(" unexpected' (expecting "done") [duplicate]
(1 answer)
Closed 7 years ago.
Maybe I'm being really silly here but I can't seem to figure this out:
#!/usr/bin/env bash
...
DELTAS=($(ls -p /foo/bar/ | grep -P '^\d+[^\.]+\.sh$'))
...
If I run this interactively it's fine, and echo $DELTAS returns:
1-foo.sh
2-bar.sh
However, when this is run inside a bash script, I receive Syntax error: "(" unexpected; any ideas?
I'm guessing that you've not put #!/bin/bash at the top of your script, and so it's running using /bin/sh rather than /bin/bash. This would run under dash rather than bash on Ubuntu, for example.
That syntax is a bash extension.

Resources