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

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.

Related

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

Bash How can I use read command to assign multiple words to 1 variable

I am trying to get multiple words/arguments into one variable with read. I tried assigning it into an array and using while loop to put all the elements in the array into 1 string.
read -a info
i=0
datastring=""
while [ $i -lt ${info[#]} ]
do
datastring=$datastring${info[i]}
done
echo "$dataString"
When I run the program it just doesn't do anything and sits there and won't print out datastring and I'm kinda lost on any other way to do it.
read datastring <<<"this sentence contains multiple words"
echo "$datastring"
If you already have an array
datastring=${info[*]}
Will concatenate the array into a single word, using the 1st char of $IFS as a separator. If you want the words all smushed together with no separators, you could do this:
datastring=""
for word in "${info[#]}"; do datastring+=$word; done
or this:
datastring=$(IFS=""; echo "${info[*]}")
or this:
datastring=${info[*]}
datastring=${datastring// /}
Note, all quotes and array indices (* vs #) have been carefully chosen: see
http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion and
http://www.gnu.org/software/bash/manual/bashref.html#Arrays

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

Bash: how can I use read to echo (or do things to) an array member

array content
someCommand=$(cat /etc/somelog) #pseudo
array creation
array=($someCommand)
array length
arrayLen=${#array[#]}
for loop iterates for array length
for (( i=0; i<${arrayLen}; i++ ));
do
adds someprefix (for readability )
$(($i + 1 )) appends number list (for clear options) starting at 1
echo "$(($i + 1 )) ${arr[$i]/#/someprefix}"
done
printf "\n"
echo "Please enter 1-${arrayLen} --> "
asks the user for input
read anwser
stores the value (a case statement woud be better
# to replace the for loop as well - but this is a concept script) just trying to get it to work.
this is what I need help with ### (I think?)
echo "${array[$anwser]}" # do things to #
You are reading to a variable named $answer, but indexing your array with $anwser.

Bash: iteration through array names

I am trying to write code to break up a large array into many different small arrays. Eventually the array I would be passed is one of unknown size, this is just my test subject. I have gotten this far:
#!/bin/bash
num=(10 3 12 3 4 4)
inArray=${#num[#]}
numArrays=$(($inArray/2))
remain=$(($inArray%2))
echo $numArrays
echo $remain
nun=0
if test $remain -gt $nun; then
numArrays=$(($numArrays+1))
fi
array=(1 2)
j=0
for ((i=0;i<$numArrays;i++, j=j+2)); do
array=("${num[#]:$j:2}")
echo "The array says: ${array[#]}"
echo "The size? ${#array[#]}"
done
What I am really having a problem with is : I would like to make the variable 'array' be able to change names slightly every time, so each array is kept and has a unique name after the loop. I have tried making the name array_$i but that returns:
[Stephanie#~]$ ./tmp.sh
3
0
./tmp.sh: line 16: syntax error near unexpected token `"${num[#]:$j:2}"'
./tmp.sh: line 16: ` array_$i=("${num[#]:$j:2}")'
[Stephanie#RDT00069 ~]$ ./tmp.sh
3
0
./tmp.sh: line 16: syntax error near unexpected token `$i'
./tmp.sh: line 16: ` array($i)=("${num[#]:$j:2}")'
Does anyone have any advice?
Thanks
I don't think you can really avoid eval here, but you might be able to do it safely if you're careful. Here's my approach:
for name in "${!array_*}"; do # Get all names starting with array_
i="${name#array_*}" # Get the part after array_
if [[ $i != *[^0-9]* ]]; then # Check that it's a number.
printf '%s is not a valid subarray name\n' "$name"
else
# Create a variable named "statement" that contains code you want to eval.
printf -v statement 'cur_array=( "${%s[#]}" )' "$name"
eval "$statement"
# Do interesting things with $cur_array
fi
done
Before this, when you're just creating the array, you know what $name should be, so just use the printf -v part.
To make it even safer, you could save all the allowed array names in another array and check that $name is a member.
With simple variables, you can use the declare keyword to make indirect assignments:
v=foo
declare $v=5
echo $foo # Prints 5
This doesn't extend to arrays in the obvious (to me, anyway) sense:
i=2
# This produces a syntax error
declare -a array_$i=("${num[#]:$j:2}")
Instead, you can declare an empty array
declare -a array_$i
or assign items one at a time:
declare -a array_$i[0]=item1 array_$i[1]=item2
Here's an example of using a for-loop to copy, say, the 3rd
and 4th letters of a big array into a smaller one. We use
i as the dynamic part of the name of the smaller array, and
j as the index into that array.
letters=(a b c d e f)
i=1
j=0
for letter in "${letters[#]:2:2}"; do
# E.g., i=0 and j=1 would result in
# declare -a array_0[1]=c
declare -a array_$i[$j]=$letter
let j+=1
done
done
echo ${array_1[#]}; # c d
${foo[#]:x:y} gives us elements x, x+1, ..., x+y-1 from foo, and
You can wrap the whole thing inside another for-loop to accomplish the goal of splitting letters into 3 smaller arrays:
# We'll create array_0, array_1, and array_2
for i in 0 1 2; do
# Just like our subset above, but start at position i*2 instead of
# a constant.
for letter in "${letters[#]:$((i*2)):2}"; do
declare -a array_$i[$j]=$letter
done
done
Once you manage to populate your three arrays, how do you access them without eval? Bash has syntax for indirect access:
v=foo
foo=5
echo ${!v} # echoes 5!
The exclamation point says to use the word that follows as a variable whose value should be used as the name of the parameter to expand. Knowing that, you might think you could do the following, but you'd be wrong.
i=1
v=array_$i # array_1
echo ${!v[0]} # array_1[0] is c, so prints c, right? Wrong.
In the above, bash tries to find a variable called v[0] and expand it to get the name of a parameter to expand. We actually have to treat our array plus its index as a single name:
i=1
v=array_$i[0]
echo ${!v} # This does print c
This should work, but this is not a good solution, another language may be better bash does not support multi dimensional arrays
eval array_$i='('"${num[#]:$j:2}"')'
And then, for example
eval 'echo "${array_'$i'[0]}"'

Resources