因此,在这里,我尝试从ifconfig文件中获取信息,但是对于使用简单ifconfig命令的错误也是如此。
!#/bin/sh
if [/home/pi/ifconfig | grep -Eo ‘inet (addr:)?([0-9]*\.){3}[0-9]*’ | grep -Eo ‘([0-9]*\.){3}[0-9]*’ | grep -v ‘127.0.0.1’ = *.*.1.*]
then
echo “good1”
else
echo “notGood2”
fi我所犯的错误
test: 2: test: [/home/pi/ifconfig: not found
grep: =: No such file or directory
grep: *.*.1.*]: No such file or directory
notGood2发布于 2018-10-17 17:48:40
您可以使用ip addr显示主机上所有接口和子网的IP地址:
$ ip -f inet addr show | awk '$1 == "inet" { print $2 }'
127.0.0.1/8
192.168.0.2/24如果你不关心子网,你可以去掉它:
$ ip -f inet addr show | awk '$1 == "inet" { print $2 }' | cut -d/ -f1
127.0.0.1
192.168.0.2根据注释,如果您只想以某种原因查看IP地址(Es)的第三个八进制是什么,这就足够简单了:
# given this:
$ ip -f inet addr show | awk '$1 == "inet" { print $2 }'
127.0.0.1/8
192.168.25.2/24
# we can do this:
$ ip -f inet addr show | awk '$1 == "inet" { print $2 }' | cut -d. -f3
0
25发布于 2018-10-17 18:48:54
#!/bin/bash
for i in $(/sbin/ifconfig | grep inet | awk '{print $2}')
do
if [[ $i =~ ^[0-9]{1,3}\.[0-9]{1,3}\.1|0.[0-9]{1,3}$ ]]; then
echo "$i good1"
else
echo "$i notGood2"
fi
done发布于 2018-10-17 20:49:21
我找到答案了,伙计们,我会解释I=1,因为它是
i=1
if [ $i = 1 ]; then
echo $i good1
else
echo $i notGood2
fi这就是我想要的,不管怎样,你们两个都帮我找到了正确的答案,伙计们!Thx
https://unix.stackexchange.com/questions/476073
复制相似问题