sedi: General improvements

This commit is contained in:
dogeystamp 2022-05-22 10:34:11 -04:00
parent de2fb12185
commit 961162c346
Signed by: dogeystamp
GPG Key ID: 7225FE3592EFFA38

View File

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