-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheditroot
executable file
·47 lines (40 loc) · 931 Bytes
/
editroot
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
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# edit a file, with sudo if current user lacks write permissions
# also turns file:line into file +line
set -e
set -u
SUDO=sudo # empty is false, non-empty is true
EDITOR=${EDITOR:-vi}
error() {
printf "$*\n"
false
}
try_edit_as_root() {
printf "You don't have permission to edit %s\n" "$1"
confirm "Edit as root" && "$SUDO" "$EDITOR" "$1"
}
if test $# -eq 0; then
"$EDITOR"
else
line=${1##*:}
if test "$line" != "$1"; then
file=${1%:$line}
else
file=$1
line=
fi
set -- "$file" ${line:++$line}
if test -e "$file"; then
if test -w "$file"; then
"$EDITOR" "$@"
else
try_edit_as_root "$@" || view "$@"
fi
else
if test -w "$(dirname "$file")"; then
"$EDITOR" "$@"
else
try_edit_as_root "$@" || error "$file does not exist"
fi
fi
fi