How to convert string into an integer in shell script? [duplicate] - arrays

This question already has answers here:
Variables in bash seq replacement ({1..10}) [duplicate]
(7 answers)
Closed 5 years ago.
On bash shell.
I am trying to give variable length of an array in for loop range and print all the elements one by one till last element. My code looks like this
for a in {0..$((${#array[#]}))}; do
echo "${array[$a]}"
done
getting below error while executing script
line 14: {0..9}: syntax error: operand expected (error token is "{0..9}")
How to convert length of array into a integer? Thanks in advance!

for a in `eval echo {0..$((${#array[#]}-1))}`
do
# do stufff with $a like echo ${array[$a]}
done
should be fine.. Mind that the count starts from 0 to total-1 and that is what is done in using $(( )).

Related

Bash: Append 'find'-results to indexed array using loop [duplicate]

This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
How can I store the "find" command results as an array in Bash
(8 answers)
Closed last year.
I have the following code which isn't working:
declare -a pids
find /foo/bar/ -iname "*.pid" -print0 | while read -d $'\0' file
do
pids+=("$file")
done
for i in "${pids[#]}"
do
echo $i
done
It should create an indexed array, search the given folder for all PIDs and append them to the array using a while loop however it seems that nothing is happening.
For me it looks like the results of find don't get appended to the array because the for loop at the bottom doesn't echo anything. I'm not sure if the += operant is correct for this but looking at similar question it seems to be. When trying to manually append a value to the array pids, instead of creating a new value on the next index it just adds the string to the first value (index 0).
For example this:
declare -a pids
pids+="foo1"
pids+="foo2"
results in this:
pids[0]="foo1foo2"
instead of:
pids[0]="foo1"
pids[1]="foo2"
Is += not the right operant for this or is something else wrong?

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?

")syntax error: invalid arithmetic operator (error token is " when looping with array [duplicate]

This question already has an answer here:
Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]
(1 answer)
Closed 5 years ago.
I have a file like this:
<Overall>3
<Overall>1
<Overall>4
<Overall>5
...
Im trying to read the numbers after the overall tag, put them in an array and and after doing the operations with them to add the result to total.
array=($(grep '<Overall>' "$file" | cut -d'>' -f 2))
total=0
for each in "${array[#]}"
do
total+=$(awk -v awkEach="${array[$each]}" 'BEGIN{print (awkEach-4.78)^2}')
done
But I get: ")syntax error: invalid arithmetic operator (error token is "
I read all similar questions and tried different things, but nothing seems to work.
you can replace all with this,
$ awk -F'>' '{sum+=($2-4.78)^2} END{print sum}' file
18.1136

Using for loops to traverse through an array in shell script [duplicate]

This question already has answers here:
Loop through an array of strings in Bash?
(21 answers)
Closed 7 years ago.
following is my test code to traverse though elements placed in an array. However I am getting the specified error. Please help me understand what the error is, and why I am getting it, so that I can rectify my mistake.
Code:
#!/bin/bash
categories=("men" "women" "kids")
for i in "${categories[#]}"
do:
echo $i;
done
Remove the colon exists after do
$ categories=("men" "women" "kids")
$ for i in "${categories[#]}";do echo $i;done
men
women
kids

Resources