Loops (including exclusing whille values)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loops (including exclusing whille values)
# 1  
Old 04-01-2009
Loops (including exclusing whille values)

Is there a way to make the following script more efficient using a single loop? I want n to be 1 through 254 excluding 10 and 225. How would I go about doing that?

Code:
#!/usr/local/bin/bash
nr="172.16.2."
n=1
msg="Adding static route for: "
while [ $n -le 9 ]
do
        /bin/echo $msg$nr$n
        /sbin/route add $nr$n -interface tun0
        n=$((n+1))
done
n=11
while [ $n -le 224 ]
do
        /bin/echo $msg$nr$n
        /sbin/route add $nr$n -interface tun0
        n=$((n+1))
done
n=232
while [ $n -le 254 ]
do
        /bin/echo $msg$nr$n
        /sbin/route add $nr$n -interface tun0
        n=$((n+1))
done

Thanks! Smilie

J.
# 2  
Old 04-01-2009
Something like that ?
Code:
#!/usr/local/bin/bash
nr="172.16.2."
msg="Adding static route for: "

for n in {1..254};
do
	case $n in 
		10|225) continue;;
		*)	/bin/echo $msg$nr$n
			/sbin/route add $nr$n -interface tun0;;
	esac
done

# 3  
Old 04-01-2009

Code:
while [ $n -le 254 ]
do
   [ $n -eq 10 ] || [ $n -eq 225 ] && continue
   echo $msg$nr$n 
   /sbin/route add $nr$n -interface tun0
   n=$((n+1))
done

# 4  
Old 04-01-2009
Awesome!

Thanks guys! Smilie
# 5  
Old 04-01-2009
Quote:
Originally Posted by cfajohnson

Code:
while [ $n -le 254 ]
do
   [ $n -eq 10 ] || [ $n -eq 225 ] && continue
   echo $msg$nr$n 
   /sbin/route add $nr$n -interface tun0
   n=$((n+1))
done

For some reason I'm having trouble getting it to work this way. It gets to 9 and hangs at 10.

Thanks!
# 6  
Old 04-01-2009

Sorry; the increment needs to be at the top of the loop:

Code:
n=0
while [ $n -le 254 ]
do
   n=$((n+1))
   [ $n -eq 10 ] || [ $n -eq 225 ] && continue
   echo $msg$nr$n 
   /sbin/route add $nr$n -interface tun0
done

# 7  
Old 04-01-2009
Quote:
Originally Posted by cfajohnson

Sorry; the increment needs to be at the top of the loop:

Code:
n=0
while [ $n -le 254 ]
do
   n=$((n+1))
   [ $n -eq 10 ] || [ $n -eq 225 ] && continue
   echo $msg$nr$n 
   /sbin/route add $nr$n -interface tun0
done

That did it. Thank you!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting error while including values in xml tags using shell script

Hi All, Please find the code below where I want to add the variable value in between the XML tags. I am taking one string and my goal is to put them between the xml tags. Ex : in between <name> , <lname> Kindly suggest a correction because while executing this script I am getting and... (8 Replies)
Discussion started by: rajneesh4U
8 Replies

2. Shell Programming and Scripting

How to use ls with pattern and including path?

Hello to all, Maybe someone could help me, my question is: How can a filter the print of command ls for the files with names of the form "abc*.txt" including the path? I've done this: If I move with command cd to /My/Path/Is/This/ and send this command: ls -lst abc*.txt -i... (37 Replies)
Discussion started by: Ophiuchus
37 Replies

3. Shell Programming and Scripting

how to use loops to take the values line by line

Hi am using aix ... I have tried so far but am getting unknown test operator error for example i have three files in one dir ... A2008 A2408 A2808 final output results should be A0108 is missing --- A1908 is missing A2108 is missing ..... #! /bin/ksh i=`ls A* |cut -c2-3` j=`date... (3 Replies)
Discussion started by: Venkatesh1
3 Replies

4. Shell Programming and Scripting

Sendmail including gif's ?

My department uses an outlook mail merge to send login information to multiple users who request access. I would like to use sendmail for obvious reasons. I copied a sample email source, with all the disgusting html formatting from outlook, and can successfully send emails almost exactly as I did... (0 Replies)
Discussion started by: MaindotC
0 Replies

5. Shell Programming and Scripting

Reading arguments but exclusing some

I have a tcsh script which I pass arguments to. I want to store all the arguments except for the options. Currently I am using the line below, however I want to also exclude and variation of them (e.g. whether any letter is uppercase or lower case) Is there a neat way to do it? set... (0 Replies)
Discussion started by: kristinu
0 Replies

6. Shell Programming and Scripting

including libraries in Makefile.am

hi, I have the following code in my Makefile.am that works fine for simple programs: bin_PROGRAMS=test test_SOURCES=test.cpp so, when I run 'make test', it runs the following command: g++ -o test test.cpp however, when I tried to run a program that includes the QuantLib library, I have to... (12 Replies)
Discussion started by: bacpp
12 Replies

7. Shell Programming and Scripting

round decimal values and use in loops

i have a file in which 3 values are stored like --4.72 4.42 3.86 what i wanna do is that take read each value and compare with a fixed value say 5 but cant do so as i am getting an error for the same. please check the code cat /home/nsadm/auto/Logging/uptime.txt| while read a b c do if... (2 Replies)
Discussion started by: gemnian.g
2 Replies

8. Programming

Error while including libraries

Hi All, When i am trying to include graphics.h ,dos.h and conio.h, its giving error as follows: pgm.c:2:17: dos.h: No such file or directory pgm.c:3:22: graphics.h: No such file or directory pgm.c:4:19: conio.h: No such file or directory Whereas stdio.h, stdlib.h and time.h gets... (3 Replies)
Discussion started by: jisha
3 Replies

9. UNIX for Dummies Questions & Answers

Including files

Hi, Is it possible to include files (print with EOF, sort of like ssi) in perl/cgi? Thanks (1 Reply)
Discussion started by: marringi
1 Replies

10. Shell Programming and Scripting

PERL: including files

I am wondering how I can include external files in a perl script. I'm currently working on a website, and I'd like to put my menu items in a subroutine for example, and put that in another file such as menu.pl. That way, I can call the subroutine from each page (such as news.pl), and if I want to... (2 Replies)
Discussion started by: LNC
2 Replies
Login or Register to Ask a Question