help with she script
alejandro at varnet.biz
Thu Jul 7 17:37:50 GMT 2005
On Wed, 6 Jul 2005 23:29:17 -0400
"fbsd_user" <fbsd_user at a1poweruser.com> wrote:
> On Sun, 3 Jul 2005 16:47:24 -0400
> "fbsd_user" <fbsd_user at a1poweruser.com> wrote:
> > This is my last coding problem.
> > target="check-state"
> > # Find the rule number of the target rule where you want the
> > # pass rules inserted before.
> > ruleno=`ipfw list | sed -n -e "s/00\([0-9]*\) $target/\1/p"`
> > The output of 'ipfw list' looks like this
> > nnnnn a 5 position sequence rule number
> > blank followed by a empty single position
> > x-x a 10 to 80 position rule text
> 00010 allow ip from any to any via lo0
> 00015 check-state
> 00110 allow tcp from any to 18.104.22.168 dst-port 53 out via dc0
> setup keep-state
> 00111 allow udp from any to 22.214.171.124 dst-port 53 out via dc0
> 00120 allow udp from any to any dst-port 67 out via dc0 keep-state
> 00200 allow tcp from any to any dst-port 80 out via dc0 setup
> > When the rule text matches the target text I want the
> > first 5 position rule number to go into ruleno.
> > Large rules files may use all 5 positions and then
> > the above code will fail to get the rule number.
> > Tried to remove the s/00\ but had syntax problems.
If you want to include the first two digits you have to remove the two
zeros from the pattern.
ruleno=`ipfw list | sed -n -e "s/\([0-9]*\) $target/\1/p"`
This is because in the other pattern when the rule number does not start
with "00" then it will not be matched and nothing will be returned.
The 's' is the substitute command, '/' is the operand separator, the
'\(' and '\)' construct saves the text it matches in the register '\1',
'[0-9]*' matches any number of digits (even 0 of them), $target is
replaced with the contents of the shell variable. All of that is
replaced by just the text matched between '\(' and '\)'. The 'p' flag
causes the line (after substitution, that is, the text between '\(' and
'\)') to be printed (and assigned to ruleno).
Hope that helps.
More information about the freebsd-questions