batch add text to file head

批量在文件的行首添加文本!

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
#! /bin/bash
function read_dir(){
# 注意此处这是两个反引号,表示运行系统命令
for file in `ls $1`
do
# 注意此处之间一定要加上空格,否则会报错
if [ -d $1"/"$file ]
then
read_dir $1"/"$file
else
# echo $1
# echo $1"/"$file
# 在此处处理文件即可
if [ "`awk 'NR==1{print $1}' $1"/"$file`" = "---" ]; then
#echo $1"/"$file
echo "" > /dev/null
else
echo "" > /dev/null
echo $1"/"$file
gsed -i "1s/^/---\ndate: 2018-09-21 12:30:00\ntitle: ${file%.*}\npublished: false\n---\n/" $1"/"$file
# echo ${file%.*}
# [[ "test.txt.md" == "${file}" ]] && echo $1"/"$file && echo ${file%.*}
# [[ "test.txt.md" == "${file}" ]] && gsed -i "1s/^/---\ndate: 2018-09-21 12:30:00\ntitle: ${file%.*}\npublished: false\n---\n/" $1"/"$file
fi
# [[ "md"x != "${file##*.}"x ]] && mv $1"/"$file $1"/"$file.md
fi
done
}
#读取第一个参数
read_dir $1