[Solved] Test if command returns given value?

Forum rules
Before you post please read how to get help
Post Reply
JusTertii
Level 3
Level 3
Posts: 171
Joined: Tue Jul 29, 2014 11:21 pm

[Solved] Test if command returns given value?

Post by JusTertii »

Hi guys,

So I'm battling away trying to implement a script that tests whether I've got a wired internet connection (if wired, set up access point; or if unplugged, connect to wifi)

So I've worked out how to test if my link layer is up using ethtool eth0. This spits out a page or so of information, but what it also contains:

Code: Select all

	Link detected: yes
So I'm trying to use this as the basis for an if statement. But I'm stuck, and apparently I'm declaring the if statement wrong. Can anyone see what the problem is?

Code: Select all

if (ethtool eth0 | grep "Link detected: yes")
#take down wifi, bring up AP
else
#take down AP, bring up WiFi
fi
Last edited by JusTertii on Sat Mar 25, 2017 7:14 am, edited 1 time in total.

User avatar
xenopeek
Level 24
Level 24
Posts: 24726
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Test if command returns given value?

Post by xenopeek »

Remove the angled brackets or use square brackets and command substitution.

This tests the result code of the commands:
if ethtool eth0 | grep "Link detected: yes"
This test whether grep shows any lines; it checks the output of the grep command and would be true if there is any output and false if there is none:
if [[ $(ethtool eth0 | grep "Link detected: yes") ]]
Image

JusTertii
Level 3
Level 3
Posts: 171
Joined: Tue Jul 29, 2014 11:21 pm

Re: Test if command returns given value?

Post by JusTertii »

Ah, thanks @xenopeek, much appreciated. Marking solved...

Post Reply

Return to “Scripts & Bash”