Quote:
Originally Posted by luvshines
Thanks for the reply, but the problem still stands.
Well actually the problem which i have described is only subset of the whole program.
The actual code is
Code:
#!/bin/bash
path0="/etc"
path1="/usr"
path2="/bin"
path3="/boot"
path4="/tmp"
server=0
pathvar=0
for((i=10;i<=100;i+=10))
do
echo ""
echo "create set_$i"
echo "create set_$i server_`expr $server % 3 + 1`:$path`expr $pathvar % 5`"
echo "mount set_$i ROOT:/dir_$i"
((server = server + 1))
((pathvar = pathvar + 1))
done
So i can't that for loop which you mentioned. Any other solution ??
|
I think maybe you're looking for something more like this:
Code:
#!/bin/bash
path=(/etc /usr /bin /boot /tmp)
for((server=0;server<=10;server+=1))
do
for((pathvar=0;pathvar<=4;pathvar++))
do
setname=set_$((server * 5 + pathvar))
servername=server_$server
pathname=${path[$pathvar]}
dirname="ROOT:/dir_$((server * 5 + pathvar))"
echo ""
echo "create $setname"
echo "create $setname $servername:$pathname"
echo "mount $setname $dirname"
done
done
That yields this:
Code:
create set_0
create set_0 server_0:/etc
mount set_0 ROOT:/dir_0
create set_1
create set_1 server_0:/usr
mount set_1 ROOT:/dir_1
create set_2
create set_2 server_0:/bin
mount set_2 ROOT:/dir_2
create set_3
create set_3 server_0:/boot
mount set_3 ROOT:/dir_3
create set_4
create set_4 server_0:/tmp
mount set_4 ROOT:/dir_4
create set_5
create set_5 server_1:/etc
mount set_5 ROOT:/dir_5
create set_6
create set_6 server_1:/usr
mount set_6 ROOT:/dir_6
create set_7
create set_7 server_1:/bin
mount set_7 ROOT:/dir_7
create set_8
create set_8 server_1:/boot
mount set_8 ROOT:/dir_8
create set_9
create set_9 server_1:/tmp
mount set_9 ROOT:/dir_9
...
I broke the variables out of the echo statements to help clarify things a bit. I also rearranged the logic a little, because I'm not sure the logic you had was going to do what you wanted. MOD 5 on a variable that is always a multiple of 10 will always yield 0.
$((expression)) is a different way of doing `expr expression`.
At any rate, I think the heart of the problem you are having is that you are trying to emulate using an array. Since BASH has array variables, it is far easier (and cleaner) to just use an array.
Basically, you can define array variables in bash with either
Code:
variable[subscript]=value
or
Code:
variable=(value1 value2 value3 ...)
and you get the values back with
Code:
echo $variable[subscript]
You can see
http://tldp.org/LDP/Bash-Beginners-G...ect_10_02.html for a much more in depth description of how to use arrays in bash.