Command not found assignement array script bash [duplicate] - c

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"

Related

Elements not being appended to array in loop [duplicate]

This question already has answers here:
A variable modified inside a while loop is not remembered
(8 answers)
Closed 5 years ago.
I am writing a Bash Shell script, which reads a bunch of lines from a file, and tries to append each line to an array, thus:
# counter variable
i=0
cat doc.txt | while read -r line; do
myArr[i]="$line"
((i=i+1))
done
The file doc.txt contains one word in every line. When I print the array (through echo ${myArr[#]} or for x in ${myArr[#]};do echo $x; done), my array seems to be empty. What am I doing wrong? Thanks!
Edit: I also tried doing this in Ksh, with the same results. An answer in either Bash or Ksh is acceptable, since I seem to be having the same issue in both.
You are running the while loop in a subshell, so any changes you make to the array disappear once that shell exits. Use input redirection instead:
while IFS= read -r line; do
myArr+=("$line")
done < doc.txt
If you using bash 4.0 or later, you can use a single command
readArray -t myArr < doc.txt

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

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

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

Calling c code which takes record as argument within awk [duplicate]

This question already has answers here:
Calling an executable program using awk
(9 answers)
Closed 9 years ago.
I want to call Code.c from within awk. Note that the Code.c takes current record (i.e. $0) as argument.
I am new to shell scripting so any help would be appreciated.
Best Regards
#!/bin/bash
while read line
do
para=`echo $line | awk '{print $0}'`
./c_code $para
done < your_process_file

Resources