-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateMode.sh
More file actions
93 lines (78 loc) · 1.86 KB
/
Copy pathcreateMode.sh
File metadata and controls
93 lines (78 loc) · 1.86 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# cd $1
# touch main.c
# cat ../template_code > main.c
# touch a.h
# cat ../template_header > a.h
# touch makefile
# cat ../template_make > makefile
if [ $# -ne 2 ]
then
echo "Usage: $0 <directory> <number>"
echo "Example $0 charpter1 4"
return
fi
rootdir='/home/book/gitrepo/p_on_c'
if [ -d $1 ]
then
echo "destation directory is $1"
else
mkdir $1
fi
cd $1 #go into destination directory
cat $rootdir/template_make > makefile
#创建头文件
touch a.h
echo "#ifndef _A_H_" > a.h
echo "#define _A_H_" >> a.h
echo "" >> a.h
echo "#include <stdio.h>" >> a.h
echo "#include <stdlib.h>" >> a.h
echo "" >> a.h
for(( i = 1; i <= $2; i++ ))
do
filename="coding$i.c"
if [ -e $filename ]
then
echo "exist"
else
touch $filename #创建源文件
echo "#include \"a.h\"" >> $filename
echo "" >> $filename
echo "void a$i( void )" >> $filename
echo -e "{\n\n}" >> $filename
fi
echo "\\" >> makefile
echo -e -n "\tcoding$i.o" >> makefile #依赖文件写入make文件
echo "void a$i( void );" >> a.h #源文件的入口函数写入头文件
done
#收尾make文件
echo "" >> makefile
echo -e '\t$(CC) $(OPTION) -o test $^ $(BEHIND)' >> makefile
#收尾头文件
echo "" >> a.h
echo "#endif" >> a.h
########################################
#构造main.c文件
echo "#include \"a.h\"" > main.c
echo "" >> main.c
echo "typedef void( *fp )( void );" >> main.c
echo "static const fp func_table[] = {" >> main.c
echo -n -e "\tNULL, " >> main.c
i=1
for(( i = 1; i <= $2; i++ ))
do
if [ $((i % 3)) -eq 0 ]
then
echo "" >> main.c
echo -e -n "\t" >> main.c
fi
if [ $i -eq $2 ]
then
echo "a$i" >> main.c
else
echo -n "a$i, " >> main.c
fi
done
# main.c收尾
echo "};" >> main.c
cat $rootdir/template_code >> main.c