bash index multiple different arrays at same index for loop [duplicate] - arrays

This question already has answers here:
bash script loop through two variables in lock step
(2 answers)
bash shell script two variables in for loop
(8 answers)
bash script loop multiple variables
(2 answers)
Closed 1 year ago.
Let's say I have three arrays that look like the following:
a=(banana apple grape)
b=(1 2 3)
c=(yellow red purple)
If I wanted to loop through such that each index of each variable is matched, how would I do this? For example, I would want something like the following:
for i in range(0,len(a))
do
echo "$a[i] $b[i] $c[i]"
done
such that the results would be:
banana 1 yellow
apple 2 red
grape 3 purple

The syntax to access array elements is ${array[index]}.
for ((i=0 ; i < ${#a[#]} ; ++i)) ; do
echo "${a[i]} ${b[i]} ${c[i]}"
done
You can use seq to simulate Python's range:
for i in $(seq 1 ${#a[#]}) ; do
echo "${a[i-1]} ${b[i-1]} ${c[i-1]}"
done
where ${#array[#]} returns the size of the array.

Related

Why does Bash allocate extra array spots if I have dashes in entries? [duplicate]

This question already has answers here:
How to find the length of an array in shell?
(7 answers)
Closed last year.
I tell Bash (v4.4) to give me an array, and I ask how long it is:
$ list=(one two three four)
$ echo ${#list}
3
Why are there so many more entries if I have - in some words?
$ list=(one-two three-four)
$ echo ${#list}
7
If I ask for the items, most of them are empty. All the ones with values are first, despite the (apparently) odd behavior with - in words.
$ for ix in $(seq 0 ${#list}); do
> echo "- ${list[$ix]}"
> done
- one-two
- three-four
-
-
-
-
-
What is going on?
"One-two" has seven characters. You find the length of the list array with echo ${#list[#]}.
echo ${#list} gives you the string length of the first list element. For Bash, i recommend the following cheatsheet: https://devhints.io/bash

how to use looping variables to populate an array [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed last year.
when I populate an array using explicit range value it get populated correctly using the code below:
arr=()
for i in {0..10}
do
arr+=( 1 )
done
echo ${arr[#]}
and I got the following output (nothing surprising)
However, when storing the range in a variable and calling it as in the code below it only shows one element:
range=10
arr=()
for i in {0..$range}
do
arr+=( 1 )
done
echo ${arr[#]}
I got the following output
Variable expansion does not occur before brace expansion so you only get a literal {0..1} as a single iterated argument.
Use the old school method instead:
for (( i = 0; i <= range; ++i )); do
Used <= here because .. is inclusive.

Create an array in bash from given start and end point [duplicate]

This question already has answers here:
How do I iterate over a range of numbers defined by variables in Bash?
(20 answers)
Closed 4 years ago.
I have this code that works:
$ array=( {100..300..100} )
$ for k in ${array[#]} ; do echo $k ; done
100
200
300
I want to parametrize the start and end points (and also the increment, because why not?)
I tried this:
$ low=100
$ high=300
$ incr=100
$ array=( {$low..$high..$incr} )
But it didn't work:
$ for k in ${array[#]} ; do echo $k ; done
{100..300..100}
What am I doing wrong?
From bash manual:
A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer.
So, parameter and variable expansion are not performed on x, y and incr. You should use seq:
arr=( $(seq $low $incr $high) )

Bash: How to separate array elements with tabs - and print on the same line? [duplicate]

This question already has answers here:
How can I join elements of an array in Bash?
(34 answers)
Closed 4 years ago.
I have an array in bash...say, my_array:
my_array={1,2,3,4}
I need two requirements for this:
1) Print all of these elements on the same line, and 2) Separate each element with a tab.
When I print the elements out, here is what the output should be:
1 2 3 4
With each "gap" in between the elements being a tab.
Any suggestions on how to do this would be appreciated. Thank you.
EDIT
Here's what I've tried so far:
1) I know I can print out an array on the same line as so:
echo {my_array[*]}
2) To get the desired tabs, I've tried making a variable with just a tab inside, and adding it to my array between each element:
temp=" "
for(...)
do
((my_array+=$i))
((my_array+=$temp))
done
This, however, gives me an error.
EDIT 2
Solution provided by Inan
This works:
printf '%s\t' "${my_array[#]}"
However, a couple things here; how would I delete the last tab, after the very last element?
Why not using "tr"?
my_array={1,2,3,4}
echo $my_array | tr "," "\t"
Result:
{1 2 3 4}
If:
my_array=(1,2,3,4)
echo $my_array | tr "," "\t"
Result:
1 2 3 4

How to use two arrays in a shell script for loop? [duplicate]

This question already has answers here:
Iterate over two arrays simultaneously in bash
(6 answers)
Closed 5 years ago.
I have two arrays in a bash script, every array have same number of elements, I need to write 2nd array's every element for every element in the first array in a for loop
first array name: ARR_MPOINT
second array name: ARR_LVNAME
piece of the script:
ARR_MPOINT=(/tmp /var /log);
ARR_LVNAME=(lv_tmp lv_var lv_log)
for MPOINT in "${ARR_MPOINT[#]}"
do
echo "/dev/mapper/VolGroup01-${ARR_LVNAME[#]} $MPOINT xfs defaults 1 2"
done
I need to print below output
/dev/mapper/VolGroup01-lv_tmp /tmp xfs defaults 1 2
/dev/mapper/VolGroup01-lv_var /var xfs defaults 1 2
/dev/mapper/VolGroup01-lv_log /log xfs defaults 1 2
If the arrays have the same length you can access the elements by index:
for ((i=0; i<${#ARR_MPOINT[#]}; i++)); do
echo "/dev/mapper/VolGroup01-${ARR_LVNAME[i]} ${ARR_MPOINT[i]} xfs defaults 1 2"
done
You could replace / with lv_ and only use the first array to get the output:
for MPOINT in "${ARR_MPOINT[#]}"; do
echo "/dev/mapper/VolGroup01-${MPOINT//\//lv_} $MPOINT xfs defaults 1 2"
done

Resources