Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script loop multiple variables

I am trying to write something like following

for i in {a..z} && j in {1..26}
do
echo "/dev/sd"$i"1               /disk$j                                 ext4     noatime        1 1" >> test
done

Of course this is not correct syntax. Can someone please help me with correct syntax for doing this?

like image 354
user654320 Avatar asked Jul 05 '26 08:07

user654320


1 Answers

To be generic, you can use 'length' as shown below.

#!/bin/bash

# Define the arrays
array1=("a" "b" "c" "d")
array2=("w" "x" "y" "z")

# get the length of the arrays
length=${#array1[@]}
# do the loop
for ((i=0;i<=$length;i++)); do
        echo -e "${array1[$i]} : ${array2[$i]}"
done

You can also assign the array like the following

array1=`awk -F" " '$1 == "CLIENT" { print $2 }' clientserver.lst`
like image 141
Guna Avatar answered Jul 08 '26 19:07

Guna



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!