From 0d2eae30f944098e3ad505be748070e258573c9b Mon Sep 17 00:00:00 2001 From: DogeyStamp Date: Sat, 2 Oct 2021 11:24:24 -0400 Subject: [PATCH] Make sedi able to create new files --- .local/bin/sedi | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/.local/bin/sedi b/.local/bin/sedi index de1e8c7..b33aaee 100755 --- a/.local/bin/sedi +++ b/.local/bin/sedi @@ -3,15 +3,32 @@ # Edit a file that requires superuser permissions without running the editor as root # Substitute for sudo -e that uses doas instead. -fname=$(mktemp) - -cat $1 > $fname -$EDITOR $fname - -if [ -n "$(diff $fname $1)" ] - then - diff $fname $1 - doas cp $fname $1 +if [ -z $1 ] +then + exit fi -rm $fname +FNAME=$(mktemp) + +if [ -e $1 ] +then + cp $1 $fname +fi + +$EDITOR $FNAME + +DIFF="" + +if [ -e $1 ] +then + DIFF=$(diff $FNAME $1) +else + DIFF=$(cat $FNAME) +fi + +if [ -n "$DIFF" ] +then + doas cp $FNAME $1 +fi + +rm $FNAME