LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Having trouble with echoing strings in round robin fashion (https://www.linuxquestions.org/questions/linux-newbie-8/having-trouble-with-echoing-strings-in-round-robin-fashion-719211/)

luvshines 04-15-2009 01:57 AM

Having trouble with echoing strings in round robin fashion
 
Hi all,
i am totally new to shell scripting and the first script which i am trying is to echo string variables in round robin manner.
the code which i am trying is
Code:

#!/bin/bash

path0="/etc"
path1="/usr"
path2="/bin"
path3="/boot"
path4="/tmp"

pathvar=0
for((i=10;i<=100;i+=10))
    do
        echo ""
        echo "$path`expr $pathvar % 5`"
        ((pathvar = pathvar + 1))
    done

The output which i get is
Code:

0

1

2

3

4

0

1

2

3

4

and not the value of path0, path1...

Help plz...

eco 04-15-2009 02:16 AM

Try this:

Code:

#!/bin/bash

path0="/etc"
path1="/usr"
path2="/bin"
path3="/boot"
path4="/tmp"

for i in $path0 $path1 $path2 $path3 $path4; do
        printf "${i}\n\n"
done


luvshines 04-15-2009 02:28 AM

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 ??

eco 04-15-2009 03:56 AM

Here is what I did to get it working. It's not the only way but it will work :)

Code:

#!/bin/bash

path0="/etc"
path1="/usr"
path2="/bin"
path3="/boot"
path4="/tmp"

i=10
for x in $path0 $path1 $path2 $path3 $path4
do
        echo ""
        echo "create set_$i"
        echo "mount set_$i ROOT:${x}"
        ((i = $i + 10))
done


colucix 04-15-2009 04:31 AM

Use indirect variable reference as in:
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"
        my_path=path`expr $pathvar % 5`
        echo "create set_$i server_`expr $server % 3 + 1`:${!my_path}"
        echo "mount set_$i ROOT:/dir_$i"
        ((server = server + 1))
        ((pathvar = pathvar + 1))
done


luvshines 04-15-2009 09:22 AM

yeah, this works fine. Thanx :)
but can you explain the significance of the ! inside the curly brackets.:confused:

colucix 04-15-2009 09:25 AM

It is the syntax introduced in Bash version 2 for the indirect variable reference:
Code:

${!varname}
means the value of the variable referenced by varname.

lithanus 04-16-2009 07:50 PM

Quote:

Originally Posted by luvshines (Post 3509320)
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.


All times are GMT -5. The time now is 02:33 AM.