I have a script that runs to fix any network issues
echo ' routes:' >> /etc/netplan/50-cloud-init.yaml
echo ' - to: 51.89.XXX.XXX/32' >> /etc/netplan/50-cloud-init.yaml
echo ' via: 0.0.0.0' >> /etc/netplan/50-cloud-init.yaml
echo ' scope: link' >> /etc/netplan/50-cloud-init.yaml
echo ' - to: 2XXX:41d0:XX0:XXff:ff:ff:ff:ff' >> /etc/netplan/50-cloud-init.yaml
echo ' scope: link' >> /etc/netplan/50-cloud-init.yaml
However how am I able to prevent it from running twice if those values are already set? I would like to script to check first if each of the lines are present and if they are then it will not add them.
The simplest way that comes to mind is to first issue a
grep
call for the text to be added on the file itself and then use the||
( OR ) operator and after that issue theecho ' ' >>
call like so:The text will be added only if it is not already present in the file.
The command after
||
will only be executed if the command before it fails.This means if
fails to return a result which means the text is not in the file,
then
Will be executed and the text will be added.
Otherwise if the text is already in the file nothing will be added to it.
This has to be used for each line of text you wish to check for before adding it.