I'm trying to add to the hosts file a certain IP. This IP can be retrieved with 2 different commands, but the best one doesn't always return it.
I'm trying to write a single command which checks the first command and if it returns an IP, add the line to the file, else use the second command.
Priority command:
docker network inspect bridge | grep Gateway | grep -o -E '[0-9\.]+'
Alternative command:
ifconfig docker | grep -oP 'inet:\K[^ ]+'
I've tried to write an if-else command using -eq ''
or -ne 0
but I can't make it work when I try to use it as a parameter to add the line in the hosts file.
It could be something like this (but in one line shell command):
if [ docker network inspect bridge | grep Gateway | grep -o -E '[0-9\.]+' -ne 0 ] ;
then sudo echo $(docker network inspect bridge | grep Gateway | grep -o -E '[0-9\.]+') "symfony.dev" >> /etc/hosts;
else sudo echo $(ifconfig docker0 | awk '/inet:/{ print substr($2,6); exit }') "symfony.dev" >> /etc/hosts;
fi
ip
.{ command | command | command ; } || { command | command ; }