-
Notifications
You must be signed in to change notification settings - Fork 0
/
enano.sh
39 lines (32 loc) · 833 Bytes
/
enano.sh
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
#!/bin/bash
## 用来快速备份->清空->nano编辑文件
# 检查是否有传入文件名
if [ "$#" -ne 1 ]; then
echo "Usage: enano.sh filename"
exit 1
fi
file=$1
backup_file="${file}.bak1"
# 如果文件不存在或文件内容为空,则直接使用 nano 编辑
if [[ ! -f $file ]] || [[ ! -s $file ]]; then
nano $file
exit 0
fi
# 查找下一个可用的备份文件名
while [[ -f $backup_file ]]; do
number=$(echo $backup_file | grep -o -E '[0-9]+$')
number=$((number + 1))
backup_file="${file}.bak${number}"
done
# 创建备份
cp $file $backup_file && echo "Backup created: $backup_file"
# 检查备份是否成功
if [[ -f $backup_file ]]; then
# 清空原文件
> $file
# 使用 nano 编辑文件
nano $file
else
echo "Backup failed, aborting edit."
exit 1
fi