Array value not getting updated in shell script - arrays

i have a shell script for putting values into an array. The script is working fine for me. The problem is, if i put the array inside a for loop (between do and done), and when i try to print the array length outised the loop,i.e, after done, the values are not getting updated. Here is the sameple code;
function _read_value
{
count=0
array[0]="hi"
for ///some condition
do
count++
array[count]="hello"
done
echo ${#array[#]
}
When i try to print the length of the array at the end, it is showing the length as 1.
Any help would be appreciated.

I made some small changes in your script and its updating values and the count also comes fine.(I made change in count increment and in loop)
#!/bin/bash
function read_value
{
count=0
while [ $count -le 5 ]
do
array[count]="hello"
count=`expr $count + 1`
done
echo ${array[#]}
echo ${#array[#]}
}
read_value

Related

Storing current index number as a variable - Bash

I'm attempting to create a bash script that updates the timestamps of files in alphabetical order. The script works but seems to be passing it's flags to the touch command used within. After my getopts while loop, I've added the following: shift $((OPTIND-1)) to update my index. It took me a while but I realize that the problem with my code is that I later initialize my for loop at index 0 instead of the current index number (i.e, for ((i=0; i<=${#file[-1]}; i++))). How do I store my current index number as a variable so that I can tell my for loop to start there?
#!/bin/bash
file=($#)
reverse=false
delay=0 #In seconds
verbose=false
sort() {
touch "${file[$i]}"
if [ $verbose = true ]; then
echo "${file[$i]}"
fi
sleep $delay
}
while getopts "rvt:" opts; do
case ${opts} in
r) reverse=true;;
v) verbose=true;;
t) delay=${OPTARG};;
?) exit;;
esac
done
shift $((OPTIND-1))
if [ $reverse = false ]; then
for ((i=0; i<=${#file[-1]}; i++)); do
sort
done
else
for ((i=${#file[#]}-1; i>=0; i--)); do
sort
done
fi

Filling a dynamic array with user input in shell scripting

I am new to shell scripting. I am trying to create an array of size n, where n is input by the user during the run time.
while [ $i -lt $n ]
do
echo For person $i enter the name?
read io
eval Name[$index]= $io
done
When I try to do this, the values are overwritten every time the loop gets the input from user.
For ex: if person 1 is - Tom,if person 2 is - John. Then when i try to print the names of all person at the end of the script, person 1 name is overwritten with person n th name.(which means, all names are stored in a single variable instead of an array).
Can someone tell me where am i going wrong?
You need to increment i in the loop so that it eventually exits. This line increments i by 1:
let i+=1
You don't need to use eval in eval Name[$index]= $io.
There is no variable named index (at least not in your code sample). I assume you meant to use i there. (i.e., Name[$index] should be Name[$i])
This code works:
#!/bin/sh -e
Name=()
i=0
while [ $i -lt 4 ]
do
echo For person $i enter the name?
read io
Name[$i]=${io}
let i+=1
done
echo names:
for n in "${Name[#]}"
do
echo $n
done

Add value to array using Bash script

I have written the following script ,
#!/bin/bash
My_File=Image.csv
Value=YES
cat "$My_File" | while IFS=, read first last
do
echo "first='$first' last='$last'"
if [ "$last" == "$Value" ];
then
echo Match_Found
echo $first
array+=("$first")
echo $first is Added
fi
done
echo (${#array[#]})
It dosenot add any value to the array , Could someone point out to the issue .
The Input is as follow ,
FA_2015-01_666,NO
FA_2015-01_777,YES
FA_2015-01_888,NO
FA_2015-01_999,YES
FA_2015-01_555,YES
Redirect the file in, don't pipe from cat or the loop is run in a subshell and everything in it is lost when it ends.
#!/bin/bash
My_File="Image.csv"
Value=YES
while IFS=, read first last
do
if [ "$last" == "$Value" ];
then
echo Match_Found
echo $first
array+=("$first")
echo $first is Added
fi
done < "$My_File"
echo "${#array[#]}"
I haven't done some bash coding in some time, but it should be like this (The way i do it):
#our table to insert an item to
table=( )
tableLength=${#table[#]}
#inserting items
table[$tableLength]="foo"
#you have to refresh this variable everytime
tableLength=${#table[#]}
table[$tableLength]="bar"
#or you can do this without refreshing the length variable
table[${#table[#]}]="foobar"
I recommend you use:
table=()
table[${#table[#]}]="item!"
using # as the length, and table[#] to display all items, so it gets the length of the array and sets an item to the length of an array or table

How do i create array and put value on it - BASH script

ahhh array and loops my weakest links. I was trying to create array depending on user input so
printf "%s\n" "how may array you want"
read value
after this i will ask what value user want to put on a array(this is the bit im stuck on)
i=1
while [ $i -le $value ]
do
echo "what value you want to put in array $i"
read number
echo $number >> array.db
i=$(( i+1 ))
echo
done
although this method works(i think) but i'm not too sure if i'm actually creating an array and putting value to that array.
you can expand arrays in bash dynamically. you can use this snippet
a=(); a[${#a[#]}]=${number}; echo ${a[#]}
The first statement defines an empty array. with the second (which you can use in your while loop) you insert a value at last elment position + 1, due to ${#a[#]} represents the length of a. the third statement just prints all elements in the array.

change array value while looping in bash

The code
SourceFolder[0]=""
SourceFolder[1]="inbound2"
SourceFolder[2]="inbound3"
for i in "${!SourceFolder[#]}"
do
if [ -z "${SourceFolder[$i]}"]; then
${SourceFolder[$i]} = "TEST"
fi
done
${SourceFolder[$i]} = "TEST" - doesn't work
it says
=: command not found
How to change value in current loop index in an array?
Because of the first space = is not interpreted as an assignment. There is a full explanation on So.
Btw ${SourceFolder[$i]} evaluate the array element, which is not what you want to do. For instance for the first one it is the empty string.
Replaces with SourceFolder[$i]=
You must change indexnumber in the your array:
ARRAYNAME[indexnumber]=value
ok, you have array is:
array=(one two three)
you can add count to your script for initialize and change element for array indexnumber, example:
#!/bin/bash
count=0
array=(one two three)
for i in ${array[#]}
do
echo "$i"
array[$count]="$i-indexnumber-is-$count"
count=$((count + 1))
echo $count
done
echo ${array[*]}
Result:
bash test-arr.sh
one
1
two
2
three
3
one-indexnumber-is-0 two-indexnumber-is-1 three-indexnumber-is-2

Resources