Tuesday, May 7, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 774  / 2 Years ago, tue, november 15, 2022, 11:11:59

I try to detect if there is a network link (cable plugged/unplugged) in an upstart script (Ubuntu 12.04). Here the relevant part of my script (which didndt work):



script
if [ /sbin/ethtool eth0 | /bin/grep "Link detected: yes" > /dev/null ] ; then
exec prog1
else
exec prog2
fi

end script


(try to start prog1 if there is a link otherwise prog2.) How to fix that?


More From » upstart

 Answers
0

The correct way to do this is to use



if /sbin/ethtool eth0 | /bin/grep -q "Link detected: yes" ; then
exec prog1
else
exec prog2
fi


The -q argument to grep will discard stdout, and the if statement checks the status of the command it runs. [ /sbin/ethtool eth0 | /bin/grep -q "Link detected: yes" ] is not a valid command, because [ is actually a program that accepts arguments like grep. So [ cannot understand /sbin/ethtool eth0 | /bin/grep -q "Link detected: yes" and it fails.


[#24806] Wednesday, November 16, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kroapathe

Total Points: 445
Total Questions: 117
Total Answers: 99

Location: Botswana
Member since Sun, Sep 19, 2021
3 Years ago
;