问题
i want to write function, that will make:
reading text from file in a loop
executing command
I have a command
for BAD_INPUT_IP in `grep -v ^# /etc/rc.d/bad_input.ip`; do
iptables -I INPUT -s $BAD_INPUT_IP -j DROP
done
I want to rebuild this to function:
function go_loop (){
for BAD_INPUT_IP in `grep -v ^# $1`; do
iptables -I INPUT -s $BAD_INPUT_IP -j DROP
done
}
go_loop "/etc/rc.d/bad_input.ip"
First part of task i made. Everything is ok. But how to make placeholder for this command ?
iptables -I INPUT -s $BAD_INPUT_IP -j DROP
I will have a lot of different commands, where i will need a loop. Can you help me ? Thanks.
回答1:
Set the variable before running going to the function
function go_loop (){
for BAD_INPUT_IP in `grep -v ^# $file`; do
iptables -I INPUT -s $BAD_INPUT_IP -j DROP
done
}
file="/etc/rc.d/bad_input.ip"
go_loop
来源:https://stackoverflow.com/questions/45690828/placeholders-in-bash-script-for-firewall