I came across a problem that I am trying to solve and I was not able to find a definitive google search result for what I am trying to do. I have not done many for loops in bash and maybe I am abusing it a bit.
I am trying to solve an issue where i is certain line that is returned form a simple one liner and associate a counter with it.
One liner format:
n=0; for i in $( ls /dev/ | grep sd | awk '!/a/' ); do ( n=$(($n+1)); ( pvs | grep $i ) > /dev/null && echo /dev/$i configured || ( echo $n $i || ( echo "ERROR ADDING SECONDARY DRIVES ( /dev/$i )"; ) ) ); done; echo $n;
Expanded Format:
n=0;
for i in $( ls /dev/ | grep sd | awk '!/a/' );
do (
n=$(($n+1));
( pvs | grep $i ) > /dev/null && echo /dev/$i configured
|| (
echo $n $i
|| (
echo "ERROR ADDING SECONDARY DRIVES ( /dev/$i )";
)
)
);
done;
echo $n;
Output:
/dev/sdb configured
/dev/sdc configured
1 sdd
1 sde
1 sdf
1 sdg
0
Desired output:
/dev/sdb configured
/dev/sdc configured
1 sdd
2 sde
3 sdf
4 sdg
4
The issue that I seem to be running into is that at the beginning of the second and subsequent loops the variable n is reset to its initial value of 0
This would greatly help me to create directories /data(1..n) as well as create independent volume groups and logical volumes.
I only have /dev/sdc /dev/sdb as true hard drives, drives /dev/sde... were temporary created using the touch command.
Thank you all for your time.
-Robert
$i) inside arithmetic evaluation ($(( ))), just use the variable name (i).