-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit
executable file
·68 lines (59 loc) · 1.81 KB
/
edit
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# this script edits a note, it inserts the file name into the first line
# after editing the file, it reads the first line and renames the file accordingly
# It takes two arguments: 1. the editor command and 2. the note to be edited
set -eu
EDITOR=$1
initial_path=$2
filename="$(basename "$initial_path")"
basename="${filename%.*}"
while true; do
# insert the filename if it isn't already there
#first_line="$(head --lines=1 -- "$initial_path")"
#if [[ "$first_line" != "$basename" ]]; then
#fi
if [[ -f "$initial_path" ]]; then
ed -s +1 "$initial_path" <<< $'i\n'"$basename"$'\n.\nw'
fi
# edit the file and read the first line
$EDITOR "$initial_path"
edited_line="$(head --lines=1 -- "$initial_path")"
[[ -z "$edited_line" ]] && exit 1;
ed -s +1 "$initial_path" <<< $'d\nw'
if [[ "$basename" == "$edited_line" ]]; then
break
fi
if [[ "$filename" == *.* ]]; then
extension=".${filename##*.}"
else
extension=""
fi
chosen_path="$(dirname "$initial_path")/$edited_line${extension}"
if [[ -e "$chosen_path" ]]; then
{ echo "i"
echo "=== Error Message starts here ==="
echo "The name '$edited_line' is already taken"
echo "Please delete this error message then:"
echo "- Save and quit to accept the original name"
echo "- or, choose another name"
echo "=== Error Message ends here ==="
echo
echo "."
echo "w"
} | ed -s +1 "$initial_path";
continue
fi
if mv "$initial_path" "$chosen_path"; then
break;
else
{ echo "i"
echo "Error: rename operation failed"
echo "File name is reset to the initial name"
echo "Choose another name or keep the original"
echo "Make sure to remove this message once done"
echo "."
echo "w"
} | ed -s +1 "$initial_path";
continue
fi
done