47 lines
519 B
Bash
Executable File

#!/bin/sh
# Edit a file that requires superuser permissions without running the editor as root
# Substitute for sudo -e that uses doas instead.
set -e
if [ -z $1 ]
then
exit
fi
DNAME=$(mktemp -d)
FNAME=$(basename $1)
FPATH="$DNAME/$FNAME"
if [ -e $1 ]
then
cp "$1" "$FPATH"
else
touch "$FPATH"
fi
$EDITOR "$FPATH"
DIFF=""
if [ ! -e $FPATH ]
then
exit
fi
if [ -e $1 ]
then
DIFF=$(diff "$FPATH" "$1")
else
DIFF=$(cat "$FPATH")
fi
if [ -n "$DIFF" ]
then
doas cp "$FPATH" "$1"
fi
rm "$FPATH"
rmdir $DNAME