userDepth
userDepth

Reputation: 173

Bash if with subshell not running as expected

I'm trying to automate dhclient release and new IPv4 new connection, When the connection is failing I usually do:

dhclient -r
dhclient -4

I created a script to automate this by pinging and running an if statement relative to the ping results.

#!/bin/bash

TEST=1;

while [[ $TEST -ne 0 ]]; do
    if [[ $(ping -c 5 "www.ubuntu.com") -ne 0 ]]; then
        dhclient -r;
        dhclient -4;

    else
        echo "Connection is good";
        TEST=0;
    fi
done

When I try to reverse the logic it doesn't say the connection is good but it goes and runs as false. Why is this?

#!/bin/bash

TEST=1;

while [[ $TEST -ne 0 ]]; do
    if [[ $(ping -c 5 "www.ubuntu.com") -eq 0 ]]; then
        echo "Connection is good";
        TEST=0;
    else
        dhclient -r;
        dhclient -4;
    fi
done

Upvotes: 0

Views: 418

Answers (0)

Related Questions