Networking Forums

Networking Forums > Computer Networking > Linux Networking > passing two variables with for loop

Reply
Thread Tools Display Modes

passing two variables with for loop

 
 
Bartosz Wegrzyn
Guest
Posts: n/a

 
      10-02-2005, 04:00 PM
Hello,

I would like to do something like this:

PORTS="80 443"
TYPE'"TCP TCP"

for a in "PORTS" $TYPE"
do
iptables -A INPUT -p $a --sport $a -j ACCEPT
#this would translate to iptables -A INPUT -p tcp --sport 80 -j ACCEPT

done


I know that this is wrong.
Is there a way to do this with for loop.

Thanks
 
Reply With Quote
 
 
 
 
Giovanni
Guest
Posts: n/a

 
      10-02-2005, 05:02 PM
On 10/02/05 18:00, Bartosz Wegrzyn wrote:
> Hello,
>
> I would like to do something like this:
>
> PORTS="80 443"
> TYPE'"TCP TCP"
>
> for a in "PORTS" $TYPE"
> do
> iptables -A INPUT -p $a --sport $a -j ACCEPT
> #this would translate to iptables -A INPUT -p tcp --sport 80 -j ACCEPT
>
> done
>
>
> I know that this is wrong.
> Is there a way to do this with for loop.
>
> Thanks



Not exactly as you would but try this:

VARS="'tcp 80' 'tcp 443'"
for vars in $VARS ## assigns the 2 words within ' '
do
set $vars ## assigns two words to $1 and $2
iptables -A INPUT -p $1 --sport $2 -j ACCEPT
done

Ciao
Giovanni
--
A computer is like an air conditioner,
it stops working when you open Windows.
Registered Linux user #337974 <http://counter.li.org/>
 
Reply With Quote
 
Bartosz Wegrzyn
Guest
Posts: n/a

 
      10-02-2005, 05:07 PM
Giovanni wrote:
> On 10/02/05 18:00, Bartosz Wegrzyn wrote:
>
>> Hello,
>>
>> I would like to do something like this:
>>
>> PORTS="80 443"
>> TYPE'"TCP TCP"
>>
>> for a in "PORTS" $TYPE"
>> do
>> iptables -A INPUT -p $a --sport $a -j ACCEPT
>> #this would translate to iptables -A INPUT -p tcp --sport 80 -j ACCEPT
>>
>> done
>>
>>
>> I know that this is wrong.
>> Is there a way to do this with for loop.
>>
>> Thanks

>
>
>
> Not exactly as you would but try this:
>
> VARS="'tcp 80' 'tcp 443'"
> for vars in $VARS ## assigns the 2 words within ' '
> do
> set $vars ## assigns two words to $1 and $2
> iptables -A INPUT -p $1 --sport $2 -j ACCEPT
> done
>
> Ciao
> Giovanni

thanks

 
Reply With Quote
 
Unruh
Guest
Posts: n/a

 
      10-02-2005, 05:55 PM
Bartosz Wegrzyn <(E-Mail Removed)> writes:

>Hello,


>I would like to do something like this:


>PORTS="80 443"
>TYPE'"TCP TCP"


What is this second one supposed to mean? It has no closing single quote.


>for a in "PORTS" $TYPE"


And this is even worse. there are unballanced double quotes.

And you probably want $PORTS, not "PORTS"

>do
>iptables -A INPUT -p $a --sport $a -j ACCEPT
>#this would translate to iptables -A INPUT -p tcp --sport 80 -j ACCEPT


No it certainly would not translate to that. It would translate to
iptables -S INPUT -p 80 443 tcp -sport 80 443 -j ACCEPT
on the first run trough and to
nonesense on the second and third run through.


>done



>I know that this is wrong.
>Is there a way to do this with for loop.


What do you want to do?


>Thanks

 
Reply With Quote
 
Bartosz Wegrzyn
Guest
Posts: n/a

 
      10-02-2005, 06:06 PM
Giovanni wrote:
> On 10/02/05 18:00, Bartosz Wegrzyn wrote:
>
>> Hello,
>>
>> I would like to do something like this:
>>
>> PORTS="80 443"
>> TYPE'"TCP TCP"
>>
>> for a in "PORTS" $TYPE"
>> do
>> iptables -A INPUT -p $a --sport $a -j ACCEPT
>> #this would translate to iptables -A INPUT -p tcp --sport 80 -j ACCEPT
>>
>> done
>>
>>
>> I know that this is wrong.
>> Is there a way to do this with for loop.
>>
>> Thanks

>
>
>
> Not exactly as you would but try this:
>
> VARS="'tcp 80' 'tcp 443'"
> for vars in $VARS ## assigns the 2 words within ' '
> do
> set $vars ## assigns two words to $1 and $2
> iptables -A INPUT -p $1 --sport $2 -j ACCEPT
> done
>
> Ciao
> Giovanni

actually this does not work
code is:

VAR="'a 1' 'b 2'"

for a in $VAR
do
set $a
echo "1:$1 2:$2"
done

output is
[root@localhost /]# sh new
1:'a 2:
1:1' 2:
1:'b 2:
1:2' 2:


 
Reply With Quote
 
Bartosz Wegrzyn
Guest
Posts: n/a

 
      10-02-2005, 06:14 PM
Unruh wrote:
> Bartosz Wegrzyn <(E-Mail Removed)> writes:
>
>
>>Hello,

>
>
>>I would like to do something like this:

>
>
>>PORTS="80 443"
>>TYPE'"TCP TCP"

>
>
> What is this second one supposed to mean? It has no closing single quote.
>
>
>
>>for a in "PORTS" $TYPE"

>
>
> And this is even worse. there are unballanced double quotes.
>
> And you probably want $PORTS, not "PORTS"
>
>
>>do
>>iptables -A INPUT -p $a --sport $a -j ACCEPT
>>#this would translate to iptables -A INPUT -p tcp --sport 80 -j ACCEPT

>
>
> No it certainly would not translate to that. It would translate to
> iptables -S INPUT -p 80 443 tcp -sport 80 443 -j ACCEPT
> on the first run trough and to
> nonesense on the second and third run through.
>
>
>
>>done

>
>
>
>>I know that this is wrong.
>>Is there a way to do this with for loop.

>
>
> What do you want to do?
>
>
>
>>Thanks


i want this


VAR="'a 1' 'b 2'"

for a in $VAR
do
set $a
echo "1:$1 2:$2"
done

but the output is wrong

i was explecting "1:a 2:1" in first line but I got this

[root@localhost /]# sh new
1:'a 2:
1:1' 2:
1:'b 2:
1:2' 2:
 
Reply With Quote
 
Giovanni
Guest
Posts: n/a

 
      10-02-2005, 07:02 PM
On 10/02/05 20:06, Bartosz Wegrzyn wrote:
> Giovanni wrote:
>
>> On 10/02/05 18:00, Bartosz Wegrzyn wrote:
>>
>>> Hello,
>>>
>>> I would like to do something like this:
>>>
>>> PORTS="80 443"
>>> TYPE'"TCP TCP"
>>>
>>> for a in "PORTS" $TYPE"
>>> do
>>> iptables -A INPUT -p $a --sport $a -j ACCEPT
>>> #this would translate to iptables -A INPUT -p tcp --sport 80 -j ACCEPT
>>>
>>> done
>>>
>>>
>>> I know that this is wrong.
>>> Is there a way to do this with for loop.
>>>
>>> Thanks

>>
>>
>>
>>
>> Not exactly as you would but try this:
>>
>> VARS="'tcp 80' 'tcp 443'"
>> for vars in $VARS ## assigns the 2 words within ' '
>> do
>> set $vars ## assigns two words to $1 and $2
>> iptables -A INPUT -p $1 --sport $2 -j ACCEPT
>> done
>>
>> Ciao
>> Giovanni

>
> actually this does not work
> code is:
>
> VAR="'a 1' 'b 2'"
>
> for a in $VAR
> do
> set $a
> echo "1:$1 2:$2"
> done


Sorry I did not remember correctly. I answered without checking old
scripts :-(

for a in 'a 1' 'b 2' ## pairs are directly in the for statement
do
set $a
echo "1:$1 2:$2"
done

Ciao
Giovanni
--
A computer is like an air conditioner,
it stops working when you open Windows.
Registered Linux user #337974 <http://counter.li.org/>
 
Reply With Quote
 
Bartosz Wegrzyn
Guest
Posts: n/a

 
      10-02-2005, 07:05 PM
Giovanni wrote:
> On 10/02/05 20:06, Bartosz Wegrzyn wrote:
>
>> Giovanni wrote:
>>
>>> On 10/02/05 18:00, Bartosz Wegrzyn wrote:
>>>
>>>> Hello,
>>>>
>>>> I would like to do something like this:
>>>>
>>>> PORTS="80 443"
>>>> TYPE'"TCP TCP"
>>>>
>>>> for a in "PORTS" $TYPE"
>>>> do
>>>> iptables -A INPUT -p $a --sport $a -j ACCEPT
>>>> #this would translate to iptables -A INPUT -p tcp --sport 80 -j ACCEPT
>>>>
>>>> done
>>>>
>>>>
>>>> I know that this is wrong.
>>>> Is there a way to do this with for loop.
>>>>
>>>> Thanks
>>>
>>>
>>>
>>>
>>>
>>> Not exactly as you would but try this:
>>>
>>> VARS="'tcp 80' 'tcp 443'"
>>> for vars in $VARS ## assigns the 2 words within ' '
>>> do
>>> set $vars ## assigns two words to $1 and $2
>>> iptables -A INPUT -p $1 --sport $2 -j ACCEPT
>>> done
>>>
>>> Ciao
>>> Giovanni

>>
>>
>> actually this does not work
>> code is:
>>
>> VAR="'a 1' 'b 2'"
>>
>> for a in $VAR
>> do
>> set $a
>> echo "1:$1 2:$2"
>> done

>
>
> Sorry I did not remember correctly. I answered without checking old
> scripts :-(
>
> for a in 'a 1' 'b 2' ## pairs are directly in the for statement
> do
> set $a
> echo "1:$1 2:$2"
> done
>
> Ciao
> Giovanni

can i do it indirectly

 
Reply With Quote
 
Floyd L. Davidson
Guest
Posts: n/a

 
      10-02-2005, 07:17 PM
Bartosz Wegrzyn <(E-Mail Removed)> wrote:
>i want this
>
>VAR="'a 1' 'b 2'"
>for a in $VAR
>do
>set $a
>echo "1:$1 2:$2"
>done
>
>but the output is wrong
>
>i was explecting "1:a 2:1" in first line but I got this
>
>[root@localhost /]# sh new
>1:'a 2:
>1:1' 2:
>1:'b 2:
>1:2' 2:


You could probably use an array to do it, but here's another
way

VAR="A 1 B 2"


set $VAR
while [ $# -gt 0 ] ; do
echo "1:$1 2:$2"
shift;shift;
done

--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) (E-Mail Removed)
 
Reply With Quote
 
Bartosz Wegrzyn
Guest
Posts: n/a

 
      10-02-2005, 09:35 PM
Floyd L. Davidson wrote:
> Bartosz Wegrzyn <(E-Mail Removed)> wrote:
>
>>i want this
>>
>>VAR="'a 1' 'b 2'"
>>for a in $VAR
>>do
>>set $a
>>echo "1:$1 2:$2"
>>done
>>
>>but the output is wrong
>>
>>i was explecting "1:a 2:1" in first line but I got this
>>
>>[root@localhost /]# sh new
>>1:'a 2:
>>1:1' 2:
>>1:'b 2:
>>1:2' 2:

>
>
> You could probably use an array to do it, but here's another
> way
>
> VAR="A 1 B 2"
>
>
> set $VAR
> while [ $# -gt 0 ] ; do
> echo "1:$1 2:$2"
> shift;shift;
> done
>

thanks this works great
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VPN to different network for passing data only WyoITGuy Windows Networking 1 05-29-2008 08:32 PM
ftp from one remote to another without passing through own PC Liam Linux Networking 14 08-17-2006 09:15 AM
Programatic monitoring of TCP variables Sam Windows Networking 1 10-10-2003 11:13 AM
Environment Variables Matthew Windows Networking 0 07-27-2003 07:11 AM
iptables & variables toto19 Linux Networking 2 07-15-2003 04:08 PM



1 2 3 4 5 6 7 8 9 10 11