My apologies for not being clearer with my first request.
I've been struggling with trying to get what should be a pretty simple bash script command to process but I'm getting screwed up by the single quote character. My intent is to create a shell script that would accept a variable (a subnet notation) from the command line and then execute a command that would run the firewall-cmd command with some specific syntax (including single quotes, which is screwing me up), and then reload the firewall rules. I'm using printf that creates the correct script, but I can't figure out how to execute that text string/line without getting "" characters in the output, and trying to create a variable with the string I want also screws things up.
So, I'd love to be able to type in "./banish.sh 1.22.228.0/24", it would execute the printf-generated command, and then also reload the firewalld rules.
My current code:
#! /bin/bash
printf "firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=$1 drop'\n"
firewall-cmd --reload
I realize the "printf" command is really just echoing that text to the screen and not executing it (like it does for the "firewall-cmd --reload" line, but if I try to assign the output of printf to a variable, that then starts putting all the back slashes in it.
Any thoughts? TIA
export TEST='1.22.228.0/24' && printf "firewall-cmd --permanent --add-rule='rule family=ipv4 source address=$TEST drop'\n" | tee /tmp/test.txt
– ajgringo619 Aug 09 '23 at 22:05firewall-cmd
, and there are no backslashes on sight here. Anyway, I don't see what the point of theprintf
is to begin with, i.e. why mess with strings and not just run the command with the variable directly. – ilkkachu Aug 10 '23 at 04:10