Script to check ip route
Hey guys, first time post...
I'm trying to get a script to check if a route exists after I connect a VPN... First time trying to get this done and need some help... This is what I have so far -- Code:
#!/bin/bash Getting these errors when I try and run it... ./routecheck.sh: line 5: [ip: command not found ./routecheck.sh: line 11: [ip: command not found If I run my command 'ip route show 10.10.200.0/24 | wc -l' it works fine.. i get a value of 1 when the route is active... but for some reason in the script it doesn't work... i'm sure i'm missing something stupid here.. but i'm new to this... so I need help please. Thanks! Richard |
The error is not about the ip command. It’s a syntax error about the test command. Put a space between the brackets and the test.
Code:
[ $EXIST -eq 0 ] |
Quote:
But the "if [ <some command> -eq 0 ]" construct won't work regardless, because "-eq" expects the left part to be a literal number. If you want to compare the output from the command(s), use Code:
if [ $(<command>) -eq <value> ]; then Code:
if [ "$(<command>)" = "<string>" ]; then However, if you want to check the exit code of a command, this will work fine: Code:
if <command>; then Unfortunately, neither ip nor wc returns an exit code other than 0 in this particular case, regardless of the contents of the routing table. However, grep would: Code:
if ip route show 10.10.200.0/24 | grep via; then |
scasey -- I tried the space and it just gave me another error -- too many arguments...
Ser Olmy -- Thanks for the suggestions! I found the one that worked for me... here is the final script that works just fine! Code:
#!/bin/bash |
Welcome to LinuxQuestions.
Use http://www.shellcheck.net to check your programming syntax. Similar to the above since the ip command returns nothing if no route exists Code:
#!/bin/bash |
All times are GMT -5. The time now is 07:15 AM. |