LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-15-2009, 01:57 AM   #1
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Rep: Reputation: 16
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...
 
Old 04-15-2009, 02:16 AM   #2
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
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
 
Old 04-15-2009, 02:28 AM   #3
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
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 ??
 
Old 04-15-2009, 03:56 AM   #4
eco
Member
 
Registered: May 2006
Location: BE
Distribution: Debian/Gentoo
Posts: 412

Rep: Reputation: 48
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
 
Old 04-15-2009, 04:31 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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
 
Old 04-15-2009, 09:22 AM   #6
luvshines
Member
 
Registered: Apr 2009
Posts: 74

Original Poster
Rep: Reputation: 16
yeah, this works fine. Thanx
but can you explain the significance of the ! inside the curly brackets.
 
Old 04-15-2009, 09:25 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 04-16-2009, 07:50 PM   #8
lithanus
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 1
Quote:
Originally Posted by luvshines View Post
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.
 
  


Reply

Tags
string



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with the weighted round-robin algo!! vishamr2000 Programming 5 03-25-2010 04:27 AM
Round Robin with Squid marche Linux - Newbie 0 12-19-2008 10:00 AM
dns round-robin tzkolinux Linux - Networking 1 12-27-2004 03:49 AM
round robin for servers deepagodkhindi Programming 5 05-11-2003 05:50 AM
Round Robin DNS Lucsi Linux - Software 1 07-18-2002 04:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration