简体   繁体   English

BASH特定设备的特定IP地址

[英]BASH specific ip address for specific device

I can't seem to get this thing to work. 我似乎无法使这件事起作用。 I need to verify specific ip address on a specific card. 我需要验证特定卡上的特定IP地址。 I created a simple function, that receives two variables, the device name ( $1 ) and ip address ( $2 ). 我创建了一个简单的函数,该函数接收两个变量,设备名称( $1 )和ip地址( $2 )。 I need to also return the value of true/false (0/1). 我还需要返回true / false(0/1)的值。 The test works properly (I used debug code) but the return value is always empty. 测试工作正常(我使用了调试代码),但是返回值始终为空。 It needs to be a function because, it will be called several times. 它必须是一个函数,因为它将被多次调用。 I have tried several variations on return value all seem to return blank. 我已经尝试过几种返回值的变体,但似乎都返回空白。 This was my latest iteration. 这是我的最新迭代。

#!/bin/bash

function ip_addr_tst()
{ 
    ip addr show dev $1|fgrep -q "$2"
    if [ $? -eq 0 ]; then
        retval=0
        echo "found"
    else
        retval=1
        echo "not found"
    fi
    return "$retval"
}

answer=$(ip_addr_tst ens192 192.168.2.180)
echo $answer

so I used echo $? 所以我用echo $? and it worked. 而且有效。 I tested with false and true Network cards/Ip addresses and it worked perfectly. 我使用了正确的网卡/ IP地址进行了测试,它运行良好。 Just so people can see 只是人们可以看到

function ip_addr_tst() { 函数ip_addr_tst(){

ip addr show dev $1|fgrep -q "$2" ip addr show dev $ 1 | fgrep -q“ $ 2”

if [ $? -eq 0 ]; then
    retval=0
    echo "found"
else
    retval=1
    echo "not found"
fi
return "$retval"

} }

answer=$(ip_addr_tst ens192 192.168.2.180) 答案= $(ip_addr_tst ens192 192.168.2.180)

echo $? 回声$?

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

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