简体   繁体   English

变量/参数扩展中的字符串项

[英]String items in Variable / Parameter Expansion

I'm trying to use a variable instead of explicitly listing my zone list in multiple places. 我尝试使用变量,而不是在多个位置显式列出我的区域列表。

I realize that I could just download all of the files, and just apply the ones I'm looking for. 我意识到我可以下载所有文件,然后仅应用我要查找的文件。 I feel like I should be able to do this, and I guess this makes this a bit of an academic exercise. 我觉得我应该能够做到这一点,我想这使这有点学术性的练习。 Perhaps my terminology is off, but most of the searching I've done turned up results based on integers. 也许我的术语不正确,但是我完成的大多数搜索都基于整数来显示结果。 The code below works as is... but i'd like to swap out the {ru,cn...} with a $zone_list variable. 下面的代码按原样工作...但是我想用$zone_list变量替换{ru,cn...} This just as easily could be file extensions or types {jpg,php,htm,html} ... am I off base here, or should I be thinking about this another way?? 就像文件扩展名或类型{jpg,php,htm,html}一样容易...我是不是这里的基地,还是我应该以另一种方式考虑呢?

#!/bin/bash

ipset=/sbin/ipset
## zone_list="ru,cn,in,hk"
## zone_list="{ru,cn,in,hk}"

## wget -N http://www.ipdeny.com/ipblocks/data/countries/{$zone_list}.zone -P ~/testing/iptables/blacklists/zones/
wget -N http://www.ipdeny.com/ipblocks/data/countries/{ru,cn,in,hk}.zone -P ~/testing/iptables/blacklists/zones/

## $ipset -F
## $ipset -N -! blacklist hash:net maxelem 262144

for ip in $(cat ~/testing/iptables/blacklists/zones/{ru,cn,in,hk}.zone); do
   ##  $ipset -A blacklist $ip
   echo $ip
done

Edit: Updated script with the answer from Dzienny below 编辑:更新了脚本,下面是Dzienny的回答

#!/bin/bash

ipset=/sbin/ipset
zone_list="{ru,cn,in,hk}"

eval "urls=(http://www.ipdeny.com/ipblocks/data/countries/${zone_list}.zone)"
wget -N "${urls[@]}" -P /srv/iptables-data/blacklists/zones/

## BLACKZONE RULE (select country to block and ip/range)
## $ipset -F
## $ipset -N -! blacklist hash:net maxelem 262144

eval "zonefiles=(/srv/iptables-data/blacklists/zones/${zone_list}.zone)"
for ip in $(cat "${zonefiles[@]}"); do
   ## $ipset -A blacklist $ip
   echo $ip
done

Bash evaluates brace expansions before expanding variables. Bash在扩展变量之前先评估 括号扩展 This means that if you put brace expansion string into a variable and use it, it will not be interpreted, instead the string will stay unchanged. 这意味着,如果将大括号扩展字符串放入变量中并使用它,它将不会被解释,而是字符串将保持不变。

You could use eval builtin to evaluate the content of the variable. 您可以使用内置的eval来评估变量的内容。 Example: 例:

zones_exp="{ru,cn,in,hk}"
eval "urls=(http://www.ipdeny.com/ipblocks/data/countries/${zones_exp}.zone)"
wget -N "${urls[@]}" -P ~/testing/iptables/blacklists/zones/

Or you can achieve deduplication of brace expansion by making a helper function, eg: 或者,您可以通过创建辅助函数来实现括号扩展的重复数据删除,例如:

function insert_zones_exp() {
  expansion=("$1"{ru,cn,in,hk}"$2");
}

where the first argument is the parth before a zone and the second part after the zone value. 其中第一个参数是区域之前的parth,第二部分是区域值之后的parth。

And then using it: 然后使用它:

insert_zones_exp "http://www.ipdeny.com/ipblocks/data/countries/" ".zone"
wget -N "${expansion[@]}" -P ~/testing/iptables/blacklists/zones/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM