Skip to contentSkip to conversation

iproute2 / question: returncode when query a match

DISCUSSION Bastian Bittorf 2 participants 3 messages Jun 4, 2015 – Jun 26, 2015 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

when executing e.g.:

root@box:~ ip route list exact '0.0.0.0/0' default via 10.63.42.125 dev eth0.2 metric 2 onlink root@box:~ echo $?
0
root@box:~ ip route list exact '0.0.0.0/8' root@box:~ echo $?
0

i expected an RC of != 0 when there is no match.
is this by design?

root@box:~ ip -V
ip utility, iproute2-ss4.0.0-1-openwrt

bye, bastian

#2 lore raw
In reply to a message not in the archive
Quoted 7 lines · Vadim Kochan
Quoted 7 lines

root@box:~ ip route list exact '0.0.0.0/8'
root@box:~ echo $?
0

i expected an RC of != 0 when there is no match.
is this by design?

Quoted 6 lines
Quoted 2 lines

root@box:~ ip -V
ip utility, iproute2-ss4.0.0-1-openwrt

I think that RC != 0 only in case error happened, but may be its good idea to add such behaviour or add option to consider ret code if there is no results ?

ofcourse this is only useful for scripting:

# ip route list exact '0.0.0.0/8' || do_some_action

instead of the now used construct:
# [ -n "$( ip route list exact '0.0.0.0/8' )" ] || do_some_action

i'am sure there are other queries, where this also makes sense. there are 2 possible ways for implementing this IMHO:

introduce a commandlineswitch like --pedantic
or just always throw an error 1 when there is no match like this:

root@box:~ echo foo | grep bar
root@box:~ echo $?
1

more opinions about that?

bye, bastian

#3 lore raw
Quoted 28 lines · On Fri, 26 Jun 2015 09:31:45 +0200 Bastian Bittorf <[email protected]>…
Quoted 7 lines · Vadim Kochan
Quoted 7 lines

root@box:~ ip route list exact '0.0.0.0/8'
root@box:~ echo $?
0

i expected an RC of != 0 when there is no match.
is this by design?

Quoted 6 lines
Quoted 2 lines

root@box:~ ip -V
ip utility, iproute2-ss4.0.0-1-openwrt

I think that RC != 0 only in case error happened, but may be its good idea to add such behaviour or add option to consider ret code if there is no results ?

ofcourse this is only useful for scripting:

# ip route list exact '0.0.0.0/8' || do_some_action

instead of the now used construct:
# [ -n "$( ip route list exact '0.0.0.0/8' )" ] || do_some_action

i'am sure there are other queries, where this also makes sense. there are 2 possible ways for implementing this IMHO:

introduce a commandlineswitch like --pedantic
or just always throw an error 1 when there is no match like this:

Quoted 7 lines

root@box:~ echo foo | grep bar
root@box:~ echo $?
1

more opinions about that?

bye, bastian

Exit codes for ip route are already used to handle errors. See current man page.

If you want to handle both errors and no match, the scripting tricks to look for empty output are better.