bash character substitution? Need help

Madison Kelly linux-5ZoueyuiTZhBDgjK7y7TUQ at public.gmane.org
Fri Feb 6 19:07:21 UTC 2004


Hi Grant, and thank you for replying. May I ask another question?

How does this part work?
MAC=${X,1,17}
  [ ${#X} -gt 18 ] &&
   {

I hope it isn't an overly junior question but it looks quite different 
from the code I have been building on. Thanks again!!

Madison

Grant Cullen wrote:
> Madison, the following will give the basic parsing.
> 
> Good luck
> 
> #!/bin/bash
> X='00:11:22:33:44:55>21,53,80-85'
> MAC=${X,1,17}
> [ ${#X} -gt 18 ] &&
>  {
>   # we have ports - length greater than 18
>   IFS=','
>   for PORT in ${x:18}
>    do
>     echo "iptable entry with port " $PORT
>    done
>  } ||
>  {
>   # no ports
>   echo "iptable entry without ports"
>  }
> 
> Grant Cullen
> JADALL Consulting Ltd.
> grant.cullen-rieW9WUcm8FFJ04o6PK0Fg at public.gmane.org
> 416-706-4447
> 
> -----Original Message-----
> From: owner-tlug-lxSQFCZeNF4 at public.gmane.org [mailto:owner-tlug at ss.org]On Behalf Of Madison
> Kelly
> Sent: Friday, February 06, 2004 12:40
> To: tlug-lxSQFCZeNF4 at public.gmane.org
> Subject: [TLUG]: bash character substitution? Need help
> 
> 
> Hi all,
> 
>    I am working on an iptables bash script but am handicapped by not
> being a programmer (or, just barely being one). Anyway, I've been able
> to figure out how to take a string and read out variables based on a
> character to split on but now what I need it to read a value into the
> same variable over and over again based on the split. Here is my
> specific issue:
> 
> I have the variable:
> 
> # Enter as (MAC address)>Port(-range),Port(-range),..,Port(-range)
> TRUSTED_IB_TCP=00:11:22:33:44:55>21,53,80-85
> 
>    Now I want to check and see if ports are defined. If not, allow all
> ports from the given MAC address. If so, read out the ports one after
> another in a 'for/while' style loop splitting on the ','. Then inside
> each of those, split on '-' and check to see if there is a value after
> the '-'.
> 
>    The problem is, I know how to split on the ',' (IFS=',' ...) but this
> doesn't seem like it is going to help me because I want to run a
> 'for/while' loop for each value, not assign new variables based on the
> split (like I have done so far). Is there some way to substitute the ','
> with ' ' and then do a 'for/while' loop?
> 
>    I am Googling now but I am having trouble finding the answer. I am
> hoping someone here might be able to point me in the right direction.
> Thanks!
> 
> Madison
> 
> PS - If it will help, I'll post what I have of the relevant parts of the
> script.


--
The Toronto Linux Users Group.      Meetings: http://tlug.ss.org
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://tlug.ss.org/subscribe.shtml





More information about the Legacy mailing list