[ale] Bash parameter expansions

Scott Plante splante at insightsys.com
Wed Feb 16 11:16:15 EST 2022


It occurred to me that I was printing the command line you were looking
for, but printing it is not really what you're after. You want to call the
command with the parameters grouped in the right positional parameter
spots. To test for that, I make a fake ipset function that just prints it's
parameters as it sees them. I wasn't keen to actually run ipset but I think
this now works the way you want:

ipset()
{
 echo ipset: params
 echo '$1='"$1"
 echo '$2='"$2"
 echo '$3='"$3"
 echo '$4='"$4"
 echo '$5='"$5"
 echo '$6='"$6"
}

ipset add "$1" "$2" ${3:+commment "${*:3}"}

splante at guinness:/tmp/> ./addip.sh test 100.0.0.0/32
ipset: params
$1=add
$2=test
$3=100.0.0.0/32
$4=
$5=
$6=
splante at guinness:/tmp/> ./addip.sh test 100.0.0.0/32 My    Comment    No
  Quotes
ipset: params
$1=add
$2=test
$3=100.0.0.0/32
$4=commment
$5=My Comment No Quotes
$6=
splante at guinness:/tmp/> ./addip.sh test 100.0.0.0/32 "My    Comment    With
  Quotes"
ipset: params
$1=add
$2=test
$3=100.0.0.0/32
$4=commment
$5=My    Comment    With   Quotes
$6=

You'd just remove the ipset function and change the call to /sbin/ipset

Scott

On Tue, Feb 15, 2022 at 2:37 PM Scott Plante <splante at insightsys.com> wrote:

> How about this?
>
> splante at guinness:/tmp/> cat ./addip.sh
> #!/bin/bash
>
> echo add "$1" "$2" ${3:+commment \""${@:3}"\"}
>
> splante at guinness:/tmp/> ./addip.sh test 100.0.0.0/32
> add test 100.0.0.0/32
> splante at guinness:/tmp/> ./addip.sh test 100.0.0.0/32 my comment
> add test 100.0.0.0/32 commment "my comment"
> splante at guinness:/tmp/> ./addip.sh test 100.0.0.0/32 "my    comment
>   with   spaces"
> add test 100.0.0.0/32 commment "my    comment   with   spaces"
>
> You even get the option of using quotes for space preservation  in the
> comment, but you only need to quote the comment for that case.
>
> Obviously you need to add your call to ipset and whatever parameter
> checking and logging you need, but does that work for the expansion
> question?
>
> Scott Plante
>
> On Sun, Feb 13, 2022 at 5:50 PM Alex Carver via Ale <ale at ale.org> wrote:
>
>> I'm putting a tiny utility script together to make it faster for me to
>> update ipset lists and add them to a restore file in one shot but I've
>> run into a slight hiccup with what I wanted to accomplish.
>>
>> So in the ideal case the script is called like this:
>>
>> addip.sh listname cidr [multiple word comment]
>>
>> The key is the multiple word comment which I wanted to be lazy and not
>> have to type in quotation marks around it.  No problem for that, I just
>> gather up anything on the command line after the second parameter:
>>
>> comment="${*:3}"
>>
>> The problem is when I'm trying to pass this to both ipset and to
>> concatenate to the end of a file.  The call to ipset would be:
>>
>> ipset add ${1} ${2} {$comment:+comment} ${comment}
>>
>> and appending to file would be:
>>
>> echo add ${1} ${2} {$comment:+comment}
>> ${comment:+\"}${comment}${comment:+\"} >> savefile
>>
>>
>> The ipset command uses the alternate text expansion to put the literal
>> word 'comment' on the command line if $comment is not empty.  The echo
>> command does something similar and also uses that same method to wrap
>> the text in double quotes if it's not null (this is to avoid having
>> 'comment ""' in the save line).
>>
>> The problem is the comment variable on the ipset line.  If I call my
>> script with no comments (addip.sh list ip) then it executes ipset just
>> fine and the file gets a copy of the entry with no comment added.
>>
>> But if I call it with the addition of a comment, ipset sees an extra
>> parameter at the end of it's command line and complains with an unknown
>> argument error:
>>
>> addip.sh test 100.0.0.0/32 This is my comment
>> ipset: Unknown argument: 'is'
>>
>> Ok, so normally I would think to do something like this:
>>
>> ipset add ${1} ${2} {$comment:+comment} "${comment}"
>>
>> But that then breaks the condition where no comment is desired. In this
>> case I get an unknown argument error again but for an empty one:
>>
>> addip.sh test 100.0.0.0/32
>> ipset: Unknown argument: ''
>>
>> I was trying to set up the script to have ipset called in only one place
>> instead of calling it twice using an if/else block but I can't quite
>> figure out how to get the parameter expansion to work for both cases.
>>
>> Plus whatever I do has to also satisfy the echo statement to produce one
>> of these two strings to append to the file depending whether there's
>> comments or not:
>>
>> add test 10.0.0.0/32
>> add test 10.0.0.0/32 comment "This is my comment"
>>
>> Thoughts?
>> _______________________________________________
>> Ale mailing list
>> Ale at ale.org
>> https://mail.ale.org/mailman/listinfo/ale
>> See JOBS, ANNOUNCE and SCHOOLS lists at
>> http://mail.ale.org/mailman/listinfo
>>
>>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.ale.org/pipermail/ale/attachments/20220216/3b3e5849/attachment.htm>


More information about the Ale mailing list