I get Syntax error: word unexpected (expecting ")") [duplicate] - arrays

This question already has answers here:
Bash array: Unexpected Syntax error [closed]
(3 answers)
Closed 6 years ago.
When I run the following code
#!/bin/bash
folder="~/Desktop/"
if [ -d "$folder" ];
then
cd $folder;
listofpdf=$( find *.pdf -type f);
fi
echo ${listofpdf};
for words in $listofpdf
do
array+=("$words")
done
I get "Syntax error: word unexpected (expecting ")")". It seems to be related to the array variable. Appreciate corrections . Thanks in advance

Although your script is a valid bash script, it is not a valid POSIX shell script: arrays are not supported. Even if sh is a link to or copy of bash, it runs in a strict POSIX mode. In order to run a script with bash extensions, it must be run with bash, either by running bash script or (as you did) using #!/bin/bash as the shebang and executing the script directly with ./script

Related

Command not found assignement array script bash [duplicate]

This question already has answers here:
bash command not found when setting a variable
(2 answers)
Closed 2 years ago.
Bash in Linux give me command not found error in Line 9 where I do $array[$i]="Name_Of_File$i".
There are also the correct numbers printed besides the error. But i can't figure how it is possible. Maybe some spaces ? On the net i fuond these sintax of writing assignement with an array.
declare -a array;
start=0;
NumFile=$(ls -1 -d log/log_cassiere* | wc -l);
for (( i=$start; i<$NumFile; i++))
do
$array[$i]="Name_Of_File$i";
done
echo ${array[0]};
The problem with:
$array[$i]="Name_Of_File$i"
is that it is actually interpreting $array[$i] because you have a $ preceding it. That means it will attempt to replace the left side of the assignment with the value. Instead, you should have:
array[$i]="Name_Of_File$i"

Pass array as bash script variable [duplicate]

This question already has answers here:
How to pass an array argument to the Bash script
(8 answers)
passing multiple arrays as a parameter in bash
(2 answers)
Closed 3 years ago.
I have a bash script that I need to run for a series of files. The script takes 4 arguments. The first ref is a file, fun is a directory, R1arr and R2arr each are arrays containing a series of files with specific features in a given directory.
My script.sh is as follows:
ref="$1"
fun=$2
R1arr="$#"
R2arr="$#"
analyseReads -i "$ref" -o "$fun" --left "${R1arr[#]}" --right "${R2arr[#]}"
And I run it for various files as follows:
FORWARD=($(for i in Sample_STE*/*R1; do echo $i; done))
REVERSE=($(for i in Sample_STE*/*R2; do echo $i; done))
bash script.sh "$ref" "$fun" "${FORWARD[#]}" "${REVERSE[#]}"
I get an error since "${FORWARD[#]}" and "${REVERSE[#]}" does not only contain the files in the array FORWARD and REVERSE but it also contains "$ref" and "$fun". Why does this happen and how could I solve this issue?

How to use arrays in bash script in Debian Jessie? [duplicate]

This question already has answers here:
I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"
(3 answers)
Difference between sh and Bash
(11 answers)
Closed 5 years ago.
When I try to execute the following
#!/bin/sh
folders=("/usr/include")
for i in ${folders[#]}; do
echo ${i}
done
exit 0
I get test.sh: 3: test.sh: Syntax error: "(" unexpected. I also tried using #!/bin/bash but it didn't work neither. I tried this on Debian Jessie in a script and it produced the above mentioned error. The same script runs fine on Ubuntu 14.04.
Additionally I tried
# foo=(/usr/include); echo $foo
/usr/include
which worked well.
I also checked it for non-printable characters with cat -e test.sh
#!/bin/sh$
$
folders=("/usr/include")$
for i in ${folders[#]}; do$
echo ${i}$
done$
exit 0$
Edit
I executed the script as sh ./test.sh
With bash, you should use declare -a :
#!/bin/bash
declare -a folders=("/usr/include");
for i in ${folders[#]}; do
echo ${i}
done
exit 0

translation from bash to ash shell: how to handle arrays defined by input?

I try to transfer the excellent example docker-haproxy from centos to alpine.
A shell script is used to process a list of values given as parameters to the script into an array, then write these values plus their index to some file.
The following construction works in bash:
ServerArray=${SERVERS:=$1}
...
for i in ${ServerArray[#]}
do
echo " " server SERVER_$COUNT $i >> /haproxy/haproxy.cfg
let "COUNT += 1"
done
but not in ash (or sh):
syntax error: bad substitution
The error refers to line
for i in ${ServerArray[#]}
What is the correct syntax here? I guess the line
ServerArray=${SERVERS:=$1}
does not define an array as intended, but googling for long did not help me.
bash to sh (ash) spoofing says
sh apparently has no arrays.
If so, how to solve the problem then?
I guess I can do with this construction:
#!/bin/sh
# test.sh
while [ $# -gt 0 ]
do
echo $1
shift
done
delivers
/ # ./test 172.17.0.2:3306 172.17.0.3:3306
172.17.0.2:3306
172.17.0.3:3306
which is what I need to proceed

Capture multiline output into an array in bash [duplicate]

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).

Resources