#!/bin/sh
if [ -z "$1" ]; then
echo "Starting up visudo with this script as first parameter"
export EDITOR=$0 && sudo -E visudo
else
echo "Changing sudoers"
echo "# Dummy change to sudoers" >> $1
fi
这个脚本将在 sudoers 的末尾添加一行“ # Dumychange to sudoers”。没有黑客,也没有竞争条件。
注释版本,解释了这实际上是如何工作的:
if [ -z "$1" ]; then
# When you run the script, you will run this block since $1 is empty.
echo "Starting up visudo with this script as first parameter"
# We first set this script as the EDITOR and then starts visudo.
# Visudo will now start and use THIS SCRIPT as its editor
export EDITOR=$0 && sudo -E visudo
else
# When visudo starts this script, it will provide the name of the sudoers
# file as the first parameter and $1 will be non-empty. Because of that,
# visudo will run this block.
echo "Changing sudoers"
# We change the sudoers file and then exit
echo "# Dummy change to sudoers" >> $1
fi