<div dir="ltr">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:<div><br></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0)">ipset()
</span><br>{
<br>  echo ipset: params
<br>  echo '$1='"$1"
<br>  echo '$2='"$2"
<br>  echo '$3='"$3"
<br>  echo '$4='"$4"
<br>  echo '$5='"$5"
<br>  echo '$6='"$6"
<br>}
<br>
<br>ipset add "$1" "$2" ${3:+commment "${*:3}"}
<br><br>splante@guinness:/tmp/> ./addip.sh test <a href="http://100.0.0.0/32">100.0.0.0/32</a>
<br>ipset: params
<br>$1=add
<br>$2=test
<br>$3=<a href="http://100.0.0.0/32">100.0.0.0/32</a>
<br>$4=
<br>$5=
<br>$6=
<br>splante@guinness:/tmp/> ./addip.sh test <a href="http://100.0.0.0/32">100.0.0.0/32</a> My    Comment    No   Quotes
<br>ipset: params
<br>$1=add
<br>$2=test
<br>$3=<a href="http://100.0.0.0/32">100.0.0.0/32</a>
<br>$4=commment
<br>$5=My Comment No Quotes
<br>$6=
<br>splante@guinness:/tmp/> ./addip.sh test <a href="http://100.0.0.0/32">100.0.0.0/32</a> "My    Comment    With   Quotes"
<br>ipset: params
<br>$1=add
<br>$2=test
<br>$3=<a href="http://100.0.0.0/32">100.0.0.0/32</a>
<br>$4=commment
<br>$5=My    Comment    With   Quotes
<br>$6=<br></span></div><div><br>You'd just remove the ipset function and change the call to /sbin/ipset</div><div><br></div><div>Scott</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Feb 15, 2022 at 2:37 PM Scott Plante <<a href="mailto:splante@insightsys.com">splante@insightsys.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">How about this?<br><br><span style="font-family:monospace"><span style="color:rgb(0,0,0)">splante@guinness:/tmp/> cat ./addip.sh
</span><br>#!/bin/bash
<br>
<br>echo add "$1" "$2" ${3:+commment \""${@:3}"\"}</span><div><span style="font-family:monospace">  <br>splante@guinness:/tmp/> ./addip.sh test <a href="http://100.0.0.0/32" target="_blank">100.0.0.0/32</a>
<br>add test <a href="http://100.0.0.0/32" target="_blank">100.0.0.0/32</a>
<br>splante@guinness:/tmp/> ./addip.sh test <a href="http://100.0.0.0/32" target="_blank">100.0.0.0/32</a> my comment
<br>add test <a href="http://100.0.0.0/32" target="_blank">100.0.0.0/32</a> commment "my comment"
<br>splante@guinness:/tmp/> ./addip.sh test <a href="http://100.0.0.0/32" target="_blank">100.0.0.0/32</a> "my    comment   with   spaces"
<br>add test <a href="http://100.0.0.0/32" target="_blank">100.0.0.0/32</a> commment "my    comment   with   spaces"<br>
<br></span><div><font face="arial, sans-serif">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.<br><br>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? <br><br>Scott Plante</font></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Feb 13, 2022 at 5:50 PM Alex Carver via Ale <<a href="mailto:ale@ale.org" target="_blank">ale@ale.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">I'm putting a tiny utility script together to make it faster for me to <br>
update ipset lists and add them to a restore file in one shot but I've <br>
run into a slight hiccup with what I wanted to accomplish.<br>
<br>
So in the ideal case the script is called like this:<br>
<br>
addip.sh listname cidr [multiple word comment]<br>
<br>
The key is the multiple word comment which I wanted to be lazy and not <br>
have to type in quotation marks around it.  No problem for that, I just <br>
gather up anything on the command line after the second parameter:<br>
<br>
comment="${*:3}"<br>
<br>
The problem is when I'm trying to pass this to both ipset and to <br>
concatenate to the end of a file.  The call to ipset would be:<br>
<br>
ipset add ${1} ${2} {$comment:+comment} ${comment}<br>
<br>
and appending to file would be:<br>
<br>
echo add ${1} ${2} {$comment:+comment} <br>
${comment:+\"}${comment}${comment:+\"} >> savefile<br>
<br>
<br>
The ipset command uses the alternate text expansion to put the literal <br>
word 'comment' on the command line if $comment is not empty.  The echo <br>
command does something similar and also uses that same method to wrap <br>
the text in double quotes if it's not null (this is to avoid having <br>
'comment ""' in the save line).<br>
<br>
The problem is the comment variable on the ipset line.  If I call my <br>
script with no comments (addip.sh list ip) then it executes ipset just <br>
fine and the file gets a copy of the entry with no comment added.<br>
<br>
But if I call it with the addition of a comment, ipset sees an extra <br>
parameter at the end of it's command line and complains with an unknown <br>
argument error:<br>
<br>
addip.sh test <a href="http://100.0.0.0/32" rel="noreferrer" target="_blank">100.0.0.0/32</a> This is my comment<br>
ipset: Unknown argument: 'is'<br>
<br>
Ok, so normally I would think to do something like this:<br>
<br>
ipset add ${1} ${2} {$comment:+comment} "${comment}"<br>
<br>
But that then breaks the condition where no comment is desired. In this <br>
case I get an unknown argument error again but for an empty one:<br>
<br>
addip.sh test <a href="http://100.0.0.0/32" rel="noreferrer" target="_blank">100.0.0.0/32</a><br>
ipset: Unknown argument: ''<br>
<br>
I was trying to set up the script to have ipset called in only one place <br>
instead of calling it twice using an if/else block but I can't quite <br>
figure out how to get the parameter expansion to work for both cases.<br>
<br>
Plus whatever I do has to also satisfy the echo statement to produce one <br>
of these two strings to append to the file depending whether there's <br>
comments or not:<br>
<br>
add test <a href="http://10.0.0.0/32" rel="noreferrer" target="_blank">10.0.0.0/32</a><br>
add test <a href="http://10.0.0.0/32" rel="noreferrer" target="_blank">10.0.0.0/32</a> comment "This is my comment"<br>
<br>
Thoughts?<br>
_______________________________________________<br>
Ale mailing list<br>
<a href="mailto:Ale@ale.org" target="_blank">Ale@ale.org</a><br>
<a href="https://mail.ale.org/mailman/listinfo/ale" rel="noreferrer" target="_blank">https://mail.ale.org/mailman/listinfo/ale</a><br>
See JOBS, ANNOUNCE and SCHOOLS lists at<br>
<a href="http://mail.ale.org/mailman/listinfo" rel="noreferrer" target="_blank">http://mail.ale.org/mailman/listinfo</a><br>
<br>
</blockquote></div>
</blockquote></div>