bash - 用于不包括某些 IP 的 IP 范围的循环
bash - for loop for IP range excluding certain IPs
我有下面的 for 循环
for ip in 10.11.{32..47}.{0..255}
do
echo "<ip>${ip}</ip>"
done
我想从上面的 for 循环中排除这个 iprange: 10.11.{32..35}.{39..61}
。此 ip 范围是上述范围的子集。有办法吗?
我试过了,没用:
abc=10.11.{34..37}.{39..61}
for ip in 10.11.{32..47}.{0..255}
do
if [[ $ip == $abc ]]
then
echo "not_defined"
else
echo "<ip>${ip}</ip>"
fi
done
试试这个:
printf "%s\n" 10.11.{32..47}.{0..255} 10.11.{32..35}.{39..61} | sort | uniq -u | while read ip; do echo $ip; done
试试这个:
for ip in 10.11.{32..47}.{0..255}
do
echo 10.11.{32..35}.{39..61} | grep -q "\<$ip\>" && continue
echo "<ip>${ip}</ip>"
done
这当然是一个简单的解决方案,它仍然循环遍历整个集合并丢弃一些不需要的元素。正如您的评论所暗示的那样,这可能会在跳过的部分产生不必要的延迟。为避免这些,您可以像这样在处理的同时生成值:
for ip in 10.11.{32..47}.{0..255}
do
echo 10.11.{32..35}.{39..61} | grep -q "\<$ip\>" && continue
echo "${ip}"
done | while read ip
do
process "$ip"
done
如果 process "$ip"
至少花费了最少的时间,那么很可能不再考虑生成值的时间。
如果你想完全跳过这些值,你也可以为你的 IP 使用一个更复杂的术语(但是这样就不再清楚这段代码是如何从你在问题中给出的规范中派生出来的,所以我最好彻底评论):
# ranges below result in
# 10.11.{32..47}.{0..255} without 10.11.{32..35}.{39..61}:
for ip in 10.11.{32..35}.{{0..38},{62..255}} 10.11.{36..47}.{0..255}
do
echo "${ip}"
done
我有下面的 for 循环
for ip in 10.11.{32..47}.{0..255}
do
echo "<ip>${ip}</ip>"
done
我想从上面的 for 循环中排除这个 iprange: 10.11.{32..35}.{39..61}
。此 ip 范围是上述范围的子集。有办法吗?
我试过了,没用:
abc=10.11.{34..37}.{39..61}
for ip in 10.11.{32..47}.{0..255}
do
if [[ $ip == $abc ]]
then
echo "not_defined"
else
echo "<ip>${ip}</ip>"
fi
done
试试这个:
printf "%s\n" 10.11.{32..47}.{0..255} 10.11.{32..35}.{39..61} | sort | uniq -u | while read ip; do echo $ip; done
试试这个:
for ip in 10.11.{32..47}.{0..255}
do
echo 10.11.{32..35}.{39..61} | grep -q "\<$ip\>" && continue
echo "<ip>${ip}</ip>"
done
这当然是一个简单的解决方案,它仍然循环遍历整个集合并丢弃一些不需要的元素。正如您的评论所暗示的那样,这可能会在跳过的部分产生不必要的延迟。为避免这些,您可以像这样在处理的同时生成值:
for ip in 10.11.{32..47}.{0..255}
do
echo 10.11.{32..35}.{39..61} | grep -q "\<$ip\>" && continue
echo "${ip}"
done | while read ip
do
process "$ip"
done
如果 process "$ip"
至少花费了最少的时间,那么很可能不再考虑生成值的时间。
如果你想完全跳过这些值,你也可以为你的 IP 使用一个更复杂的术语(但是这样就不再清楚这段代码是如何从你在问题中给出的规范中派生出来的,所以我最好彻底评论):
# ranges below result in
# 10.11.{32..47}.{0..255} without 10.11.{32..35}.{39..61}:
for ip in 10.11.{32..35}.{{0..38},{62..255}} 10.11.{36..47}.{0..255}
do
echo "${ip}"
done