A Bash code snippet which checks to see if a line exists in a file, and if it doesn’t then uses ‘sudo’ to append the line to the file.

LINE="Required_no_spaces"
FILE="testfile"
echo "Ensure $LINE exists in $FILE"
if [ -z "$(grep $LINE $FILE)" ]; then 
echo $LINE | sudo tee -a $FILE
else  
echo "Already exists"; 
fi