LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices

Reply
 
Search this Thread
Old 12-27-2011, 10:33 AM   #1
yaron.kadem
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Rep: Reputation: Disabled
A question about for loop (increasing two parameters)


Hi All,

I have a question about the for loop in Bash, I would like to produce plenty of lines using for loop where the first field increases by one but only when the second one reaches its limit.

I would like the third octet of the IP address 1.1.1.1 to change to the number 2 but only when the fourth octet reaches the number 5.

for i in {1..5};do echo "ip route add 1.1.1.$i/32 via 192.168.168.1";done

here is an example of what I need my for loop to do:

ip route add 1.1.1.1/32 via 192.168.168.1
ip route add 1.1.1.2/32 via 192.168.168.1
ip route add 1.1.1.3/32 via 192.168.168.1
ip route add 1.1.1.4/32 via 192.168.168.1
ip route add 1.1.1.5/32 via 192.168.168.1
ip route add 1.1.2.1/32 via 192.168.168.1
ip route add 1.1.2.2/32 via 192.168.168.1
ip route add 1.1.2.3/32 via 192.168.168.1
ip route add 1.1.2.4/32 via 192.168.168.1
ip route add 1.1.2.5/32 via 192.168.168.1


Help would be much appreciated.

Yaron
 
Old 12-27-2011, 10:51 AM   #2
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,790

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
@ Reply

Hi yaron.kadem,

You have to use 2 loops to achieve the required output. It will look like as follows:
Code:
#!/bin/bash
for j in {1..5}
do
     for i in {1..5}
     do 
            echo "ip route add 1.1.$j.$i/32 via 192.168.168.1"
            if [ $i = 5]
            then 
                  continue
            fi
     done
done
[code]
 
1 members found this post helpful.
Old 12-27-2011, 11:04 AM   #3
yaron.kadem
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
@ reply

Wow, thanks a lot, it works perfectly.


thanks
 
Old 12-27-2011, 11:07 AM   #4
T3RM1NVT0R
Senior Member
 
Registered: Dec 2010
Location: Internet
Distribution: Linux Mint, Ubuntu, SLES, CentOS
Posts: 1,790

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
@ Reply

You're welcome. Enjoy linux!!!

Please mark this thread as solved.
 
Old 12-27-2011, 01:42 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Or you can use a single brace expansion expression to do the whole thing:

Code:
printf "%s\n" "ip route add 1.1."{1..5}.{1..5}"/32 via 192.168.168.1"
brace expansion
 
1 members found this post helpful.
Old 12-28-2011, 02:25 AM   #6
yaron.kadem
LQ Newbie
 
Registered: Sep 2011
Posts: 16

Original Poster
Rep: Reputation: Disabled
@ reply

thanks David for another way to do that.

Yaron
 
  


Reply

Tags
bash scripting, loop


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Loop through list of URLs in txt file, parse out parameters, pass to wget in bash. dchol Linux - Newbie 16 07-27-2011 02:19 PM
lilo boot parameters question ewsmith Linux - Software 2 01-27-2011 06:47 AM
How increasing RAM affects kernel parameters pacavatar Linux - Newbie 7 11-17-2010 06:09 PM
Sabayon LiveDVD Loop from USB - Grub kernel parameters manwithaplan Linux - General 0 01-14-2009 01:25 AM
question re: search - priority of parameters secesh LQ Suggestions & Feedback 0 02-05-2005 10:43 AM


All times are GMT -5. The time now is 07:26 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration