47 lines
519 B
Plaintext
Raw Normal View History

2021-10-01 20:41:38 -04:00
#!/bin/sh
# Edit a file that requires superuser permissions without running the editor as root
# Substitute for sudo -e that uses doas instead.
2022-05-22 10:34:11 -04:00
set -e
2021-10-02 11:24:24 -04:00
if [ -z $1 ]
then
exit
fi
2022-05-22 10:34:11 -04:00
DNAME=$(mktemp -d)
FNAME=$(basename $1)
FPATH="$DNAME/$FNAME"
2021-10-02 11:24:24 -04:00
if [ -e $1 ]
then
2022-05-22 10:34:11 -04:00
cp "$1" "$FPATH"
else
touch "$FPATH"
2021-10-02 11:24:24 -04:00
fi
2021-10-01 20:41:38 -04:00
2022-05-22 10:34:11 -04:00
$EDITOR "$FPATH"
2021-10-02 11:24:24 -04:00
DIFF=""
2022-05-22 10:34:11 -04:00
if [ ! -e $FPATH ]
then
exit
fi
2021-10-02 11:24:24 -04:00
if [ -e $1 ]
then
2022-05-22 10:34:11 -04:00
DIFF=$(diff "$FPATH" "$1")
2021-10-02 11:24:24 -04:00
else
2022-05-22 10:34:11 -04:00
DIFF=$(cat "$FPATH")
2021-10-02 11:24:24 -04:00
fi
2021-10-01 20:41:38 -04:00
2021-10-02 11:24:24 -04:00
if [ -n "$DIFF" ]
then
2022-05-22 10:34:11 -04:00
doas cp "$FPATH" "$1"
2021-10-01 20:41:38 -04:00
fi
2022-05-22 10:34:11 -04:00
rm "$FPATH"
rmdir $DNAME