Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bash nested loop not working? 2

Status
Not open for further replies.

rigstars2

Instructor
Dec 18, 2011
64
US
Struggling here to get both IPs in my arr1 to be blocked going to the URLs listed in arr2 In the future, I'd like to expand arr1 to add more IPs if needed.
Can someone please assist with my code below? Thanks in advance.

#!/bin/sh

arr1="192.168.1.12 192.168.1.14"
arr2="facebook.com twitter.com linkedin.com tumblr.com instagram.com"

for ((i=0;i<${#arr1[@]};++i)); do
for DOMAIN in $arr2; do
echo "-I FORWARD -p tcp -s "$arr" --dport 443 -m string --string "$DOMAIN" --algo bm --to 65535 -j DROP"
done
done
exit
 
Hi

You not declared variable $arr of type array ( at least not in the fragment you posted ).
If you intended to use $arr1, that is not of type array.

Bash:
[navy]arr1[/navy][teal]=[highlight]([/highlight][/teal][purple]192.168[/purple][teal].[/teal][purple]1.12 192.168[/purple][teal].[/teal][purple]1.14[/purple][teal][highlight])[/highlight][/teal]
[navy]arr2[/navy][teal]=[/teal][i][green]"facebook.com twitter.com linkedin.com tumblr.com instagram.com"[/green][/i]

[b]for[/b] [teal](([/teal][navy]i[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal]i[teal]<[/teal][navy]${#arr1[@]}[/navy][teal];++[/teal]i[teal]));[/teal] [b]do[/b]
    [b]for[/b] DOMAIN [b]in[/b] [navy]$arr2[/navy][teal];[/teal] [b]do[/b]
        echo -I FORWARD -p tcp -s [i][green]"$[highlight]{[/highlight]arr[highlight]1[/highlight][i][highlight]}[/highlight]"[/green][/i] --dport [purple]443[/purple] -m string --string [i][green]"$DOMAIN"[/green][/i] --algo bm --to [purple]65535[/purple] -j DROP
    [b]done[/b]
[b]done[/b]

Regarding the [tt]/bin/sh[/tt] in your shebang, you know your script is not Bourne shell compatible, right ? ( The C-like [tt]for[/tt] is Bash & Ksh specific extension. )


Feherke.
feherke.github.io
 
Thanks feherke,

I'm aware of the /bin/sh in my shebang. This script is being run on my linux router.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top