Bash 从变量生成IP

Bash 从变量生成IP,bash,ip-address,Bash,Ip Address,我正在尝试根据我设置的IP范围从文件中删除多个IP条目 文件的外部: 15.12.168.192 13.13.168.192 23.12.168.192 23.15.168.192 等等 我只想在脚本下面设置范围区域 #!/bin/bash # RANGE START=192.168.12 END=192.168.13 ##collecting the last two digit from START and END first=`echo $START | sed 's/^.*\(.

我正在尝试根据我设置的IP范围从文件中删除多个IP条目

文件的外部:

15.12.168.192 13.13.168.192 23.12.168.192 23.15.168.192 等等

我只想在脚本下面设置范围区域

#!/bin/bash

# RANGE 
START=192.168.12
END=192.168.13


##collecting the last two digit from START and END
first=`echo $START | sed 's/^.*\(.\{2\}\)$/\1/'`
last=`echo $END | sed 's/^.*\(.\{2\}\)$/\1/'`

## echo both
echo $first
echo $last

for ip in 192.168.1.{$first..$last}; do
echo "$ip"

## conversion of the IPs.
echo $ip | sed -e '/\n/!G;s/\([^.]*\)\.\(.*\n\)/&\2.\1/;//D;s/\n//'
done
即使还有针对文件的最后一步(grep-v),我已经被困在这个阶段;我知道这是一个语法图

这是一行:

for ip in 192.168.1.{$first..$last}; do
echo "$ip"

这是使用
$first
$last
来生成给我带来麻烦的每个IP。

{12..45}
仅适用于文本,不能使用变量。但是,如果只有一对大括号,则无需展开:

for fourth in $( seq $first $last) ; do
    echo 192.168.1.$fourth
done

{12..45}
仅适用于文本,不能使用变量。但是,如果只有一对大括号,则无需展开:

for fourth in $( seq $first $last) ; do
    echo 192.168.1.$fourth
done

使用
first=${START###*.}
可以更简单地设置
first
;类似地,对于
last
。一种更可读的反转八位字节的方法是
IFS=。使用
first=${START#####*.}
,可以更简单地设置读取一个b c d
first
;类似地,对于
last
。一种更可读的反转八位字节的方法是
IFS=。读取一个b c d原因是shell在参数扩展之前进行了大括号扩展:不需要调用
seq
,您可以使用算术表达式执行类似c的for循环:
for((i=$first;原因是shell在参数扩展之前进行大括号扩展:不需要调用
seq
,您可以使用算术表达式执行类似C的for循环:
for((i=$first;i