Capture multiline output into an array in bash [duplicate] - arrays

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

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

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

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

Error in creating array variable in Debian 8.4

I have a very basic shell script containing
#!/bin/sh
NAME[0]="Hello"
echo ${NAME[0]}
So when I run this script an error occurs stating
./test.sh: 2: ./test.sh: NAME[0]=Hello: not found
./test.sh: 3: ./test.sh: Bad substitution
So basically I looked through a few tutorials and found this to be the basic way to declare arrays. So I am confused as to why this is an error. Any ideas?
You are starting your script as #!/bin/sh, which has a soft link to dash (The current version of sh which conforms with the POSIX 1003.2 and 1003.2a specifications for the shell) and dash doesn't support arrays. In debian 8 onwards dash has become default shell, so if you run ls -la /bin/sh the output will be /bin/sh -> dash
However bash still remains the default login shell, only the default /bin/sh used in shell scripts has been changed. So if you run your code on terminal, it will work just fine. More information on why this switch was made in Ubuntu can be found here.
If you want to use arrays in your script then you must start your script with #!/bin/bash
So your script works perfectly if modified like this
#!/bin/bash
NAME[0]="Hello"
echo ${NAME[0]}
More information on Dash as Sh DashAsBinSh

I am getting error "array.sh: 3: array.sh: Syntax error: "(" unexpected"

I have written the following code:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
And I am getting error:
array.sh: 3: array.sh: Syntax error: "(" unexpected
From what I came to know from Google, that this might be due to the fact that Ubuntu is now not taking "#!/bin/bash" by default... but then again I added the line but the error is still coming.
Also I have tried by executing bash array.sh but no luck! It prints blank.
My Ubuntu version is: Ubuntu 14.04
Given that script:
#!/bin/bash
#Simple array
array=(1 2 3 4 5)
echo ${array[*]}
and assuming:
It's in a file in your current directory named array.sh;
You've done chmod +x array.sh;
You have a sufficiently new version of bash installed in /bin/bash (you report that you have 4.3.8, which is certainly new enough); and
You execute it correctly
then that should work without any problem.
If you execute the script by typing
./array.sh
the system will pay attention to the #!/bin/bash line and execute the script using /bin/bash.
If you execute it by typing something like:
sh ./array.sh
then it will execute it using /bin/sh. On Ubuntu, /bin/sh is typically a symbolic link to /bin/dash, a Bourne-like shell that doesn't support arrays. That will give you exactly the error message that you report.
The shell used to execute a script is not affected by which shell you're currently using or by which shell is configured as your login shell in /etc/passwd or equivalent (unless you use the source or . command).
In your own answer, you say you fixed the problem by using chsh to change your default login shell to /bin/bash. That by itself should not have any effect. (And /bin/bash is the default login shell on Ubuntu anyway; had you changed it to something else previously?)
What must have happened is that you changed the command you use from sh ./array.sh to ./array.sh without realizing it.
Try running sh ./array.sh and see if you get the same error.
Instead of using sh to run the script,
try the following command:
bash ./array.sh
I solved the problem miraculously. In order to solve the issue, I found a link where it was described to be gone by using the following code. After executing them, the issue got resolved.
chsh -s /bin/bash adhikarisubir
grep ^adhikarisubir /etc/passwd
FYI, "adhikarisubir" is my username.
After executing these commands, bash array.sh produced the desired result.

Resources