From 961162c34600b8c05cca557a5d619136d91d9a4f Mon Sep 17 00:00:00 2001 From: dogeystamp Date: Sun, 22 May 2022 10:34:11 -0400 Subject: [PATCH] sedi: General improvements --- src/.local/bin/sedi | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/.local/bin/sedi b/src/.local/bin/sedi index d61a877..0e1c8d4 100755 --- a/src/.local/bin/sedi +++ b/src/.local/bin/sedi @@ -3,32 +3,44 @@ # 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 -FNAME=$(mktemp) +DNAME=$(mktemp -d) +FNAME=$(basename $1) +FPATH="$DNAME/$FNAME" if [ -e $1 ] then - cp $1 $FNAME + cp "$1" "$FPATH" +else + touch "$FPATH" fi -$EDITOR $FNAME +$EDITOR "$FPATH" DIFF="" +if [ ! -e $FPATH ] +then + exit +fi + if [ -e $1 ] then - DIFF=$(diff $FNAME $1) + DIFF=$(diff "$FPATH" "$1") else - DIFF=$(cat $FNAME) + DIFF=$(cat "$FPATH") fi if [ -n "$DIFF" ] then - doas cp $FNAME $1 + doas cp "$FPATH" "$1" fi -rm $FNAME +rm "$FPATH" +rmdir $DNAME