Incluindo temporariamente o arquivo sudoers original e limpando o arquivo antigo parece fazer o truque.
includeCurrent=$'#include /etc/sudoers\n'
echo "$includeCurrent$updaterSudoers" | visudo -c -q -f -
Todo o sudoers.sh acabei tendo.
#!/bin/bash
#Clear the current file if it exsists
#Warning: this might break stuff if you edit current user visudo rights
if [[ -f "/etc/sudoers.d/aptget" ]]
then
echo -n > "/etc/sudoers.d/aptget"
fi
#New settings to be added
updaterSudoers="$(cat <<EOF
Cmnd_Alias APTGETUPDATE = /usr/bin/apt-get update *
Cmnd_Alias APTGETINSTALL = /usr/bin/apt-get -y --force-yes install *
%aptgroup ALL = (root) NOPASSWD: APTGETUPDATE, APTGETINSTALL
Defaults!APTGETUPDATE !requiretty
Defaults!APTGETINSTALL !requiretty
EOF
)"
#Include current sudoers file
includeCurrent=$'#include /etc/sudoers\n'
#Validate new settings with current settings
echo "$includeCurrent$updaterSudoers" | visudo -c -q -f -
if [ "$?" -eq 0 ]
then
#Write /etc/sudoers.d/aptget with updateSudoers value
echo "$updaterSudoers" | (EDITOR="tee" visudo -f /etc/sudoers.d/aptget) &> /dev/null
else
#Yell and exit in case of error
echo "ERROR CHECKING SUDOERS FILE:"
echo "$includeCurrent$updaterSudoers" | visudo -c -f -
exit 1
fi
#If sudoers.d or sudoers.d/aptget is not included include only sudoers.d/aptget
grep -xq "#includedir /etc/sudoers\.d/\?" /etc/sudoers || grep -xq "#include /etc/sudoers\.d/aptget" /etc/sudoers
if [ "$?" -eq 1 ]
then
echo "#include /etc/sudoers.d/aptget" | (EDITOR="tee -a" visudo -f /etc/sudoers) &> /dev/null
fi