jeudi 25 juillet 2019

Suppress errors in if statement

I have the following code in which I try to check if the machine obtains it's IP adress from dhcp or if it's static. The code has to work for both ubuntu and sles 12. Now if I'm using the code on sles I get an error that the file is not found which is normal. But I don't want the user to see this message. Is there a way to redirect the output of an if statement? Normal redirection doesn't seem to work.

I already tried putting bash &> /dev/null after the if statement but that doesn't change a thing.

if [[ ! "$(grep "dhcp4: true" /etc/netplan/50-cloud-init.yaml | awk '{print $2}')" == "true" ]] || [[ ! "$(grep 'BOOTPROTO' /etc/sysconfig/network/ifcfg-eth0 | cut -d '=' -f2 | sed "s/'//g" | xargs)" == "dhcp" ]]
then
    # more code...
fi  

If anyone has a better solution to check if the client obtains the IP via dhcp I would appreciate if you let me know.

Aucun commentaire:

Enregistrer un commentaire