Placeholders in bash script for firewall

♀尐吖头ヾ 提交于 2019-12-25 00:33:19

问题


i want to write function, that will make:

  1. reading text from file in a loop

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!