-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoasedit
34 lines (29 loc) · 1.3 KB
/
doasedit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/sh
RED='\033[0;31m'
NC='\033[0m' # No Color
if [ "$USER" == 'root' ]; then
echo -e "${RED}error:${NC} cannot run \`doasedit\` as root" >&2
exit
elif ! [ -x "$(command -v doas)" ]; then # Check if doas is installed. If it isn't installed.
echo -e "${RED}error:${NC} could not find command \`doas\`" >&2
exit
elif [ -z "$1" ]; then # Check if there was any input. If there was no input, abort.
echo -e "${RED}error:${NC} no input given" >&2
exit
elif [ -d "$1" ]; then # Check if the inputted file is a directory. If it is a directory, abort.
echo -e "${RED}error:${NC} $1: is a directory" >&2
exit
fi
tempDir="$(mktemp -d)" # Create the directory for the temporary file. Example: /tmp/tmp.rYT4bE3AiS/
tempFile="$tempDir/$(basename $1)" # Create the temporary file. Example: /usr/bin/bash -> /tmp/tmp.rYT4bE3AiS/bash
if [ -e "$1" ]; then # Make sure file exists before copying. If it doesn't, proceed without copying
cp "$1" "$tempFile"
fi
eval "$EDITOR $tempFile" # Start writing to the file. Use $EDITOR as the editor.
if (cmp -s $1 $tempFile) || ! [ -f $tempFile ]; then
echo "no changes written to $1"
else
doas cp $tempFile $1
rm $tempDir -rf
echo "wrote changes to file $1"
fi