How to set variables inside Linux loop

I try to determine the existing hard drives in each system using a for loop, as shown below, the problem is that when I try to set a variable using the code below, I get sda = true: the command was not found. What is the right way to do this?

#!/bin/bash for i in abcdef do grep -q sd$i /proc/partitions if [ $? == 0 ] then sd$i=true else sd$i=false fi done 
+4
source share
3 answers

You need to use an array or declare :

 declare sd$i=true 
+3
source

I would use an array in this case. For instance:

 $ i=a $ sd[$i]=true $ echo ${sd[a]} true 

As another poster stated, if you want to do this without an array, you can instead create a local variable using syntax like declare sd$i=true . If you want to create a global variable, use export sd$i=true .

+2
source

Source: https://habr.com/ru/post/1342886/


All Articles