lnlidawei

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

c_cpp:编译脚本regcc:编译当前目录的单文件源程序

 

 

 

 

一、说明

 

  1、单文件的源程序:源程序是一个独立完整的文件。

 

  2、regcc功能:

      2.1、显示所有文件:在当前目录,显示所有文件;

      2.2、显示可执行文件:在当前目录,显示所有可执行文件;

      2.3、编译C源文件:在当前目录,编译所有的C源文件并且生成可执行文件;

      2.4、删除可执行文件:在当前目录,删除与“C源文件”同名的可执行文件;

      2.5、所有:显示所有可执行文件;删除C源程序编译后的可执行文件;显示所有文件;编译所有C源程序文件并且生成可执行文件。

 

  3、regcc 脚本格式:

      3.1、regcc 脚本格式: ./regcc  <options>

      3.2、regcc 选项说明:

          选项    功能

          a      在当前目录,显示所有的文件。

           l      在当前目录 ,显示所有的可以执行文件。

          d        在当前目录 ,删除所有与“c源文件”同名的可执行文件。

          c      在当前目录,编译所有的C源程序文件并且生成可执行文件。

          x        (x=all, all=aldc)在当前目录并且存在文件 “ *.c ” ,删除所有与“c源文件”同名的可执行文件;编译所有文件“ *.c”生成“可执行文件”; 显示所有文件。

        3.3、regcc 脚本使用示例:

            3.3.1、./regcc  a      //  显示当前目录的所有文件;

            3.3.2、./regcc  l       //  显示当前目录所有可执行文件;

            3.3.3、./regcc  d      //  删除当前目录与C源程序同名的所有可执行的文件;

 

 

 

 

二、程序源码

 

  1 [root@rocky c]# cat  regcc
  2 #!/usr/bin/bash
  3 
  4 
  5 
  6 
  7 # ls *.c | gawk -F. '{print $1}' | xargs
  8 
  9 # ls *.c| gawk -F. '{ print  $1, $0}' | xargs -n2 gcc -Wall -o
 10 
 11 
 12 function lsall()
 13 {
 14         echo
 15         echo '  == List All Files: Start =='
 16         ic=1
 17         for la in $(ls)
 18         do
 19                 echo "      List_Name_${ic}:    " ${la}
 20                 ic=$((${ic}+1))
 21         done
 22         echo '  == List All Files: END =='
 23         echo
 24 }
 25 
 26 
 27 function lsexe()
 28 {
 29         echo
 30         echo
 31         echo '  == List Executable Files of C : start =='
 32 
 33         ic=1
 34 
 35         for dl in $(ls)
 36         do
 37                 if [ -x ${dl} ]
 38                 then
 39                         echo "      LIST_EXE_NAME_${ic}:"    ${dl}
 40                         ic=$((${ic}+1))
 41                 fi
 42         done
 43         echo '  == List Executable Files of C : over =='
 44         echo
 45         echo
 46 }
 47 
 48 
 49 function gccexe
 50 {
 51         echo
 52         echo
 53         echo '  == Compile *.c Files: start =='
 54 
 55         ls *.c| gawk -F. '{ print  $1, $0}' | xargs -n2 gcc -Wall -o
 56 
 57         echo '  == Compile *.c Files: over =='
 58         echo
 59         echo
 60 }
 61 
 62 
 63 function delexe()
 64 {
 65         echo
 66         echo
 67         echo '  == Delete Executable Files of C : start =='
 68 
 69         ic=1
 70 
 71         for dl in $(ls *.c |gawk -F. '{print $1}')
 72         do
 73                 if [ -x ${dl} ]
 74                 then
 75                         echo "      DELETE_EXE_NAME_${ic}:"    ${dl}
 76                         rm  -rf  ${dl}
 77                         ic=$((${ic}+1))
 78                 fi
 79         done
 80         echo '  == Delete Executable Files of C : over =='
 81         echo
 82         echo
 83 }
 84 
 85 
 86 function run()
 87 {
 88         echo
 89         echo
 90         echo '  == REGCC help: start '
 91                 echo
 92                 echo '      COMMAND     <OPTION> '
 93                 echo '      ./regcc     <a|l|d|c|x> '
 94                 echo
 95                 echo '      OPTIONS      FUNCTION'
 96                 echo '         a         list all files in the current directory .'
 97                 echo '         l         list all executable files in the current directory .'
 98                 echo '         d         delete all executable files with a same name of c source files(*.c) in current directory .'
 99                 echo '         c         compile all files of c source files in current directory .'
100                 echo '         x         list files in the current directory;' 
101                 echo '                  delete files with a same name within source files(*.c) ;'
102                 echo '                  generate exe files by (*.c) .'
103                 echo
104                 echo '      EXAMPLE    OPTIONS      FUNCTIONS'
105                 echo '      ./regcc      l          list all executable files in current directory .'
106                 echo '      ./regcc      d          delete all executable with a same name of c source files in current directory .'
107                 echo
108         echo '  == REGCC help: end '
109         echo
110         echo
111 
112 
113         if [[ $1 = "l" ]]
114         then
115                 lsexe
116 
117         elif [[ $1 = "a" ]]
118         then
119                 lsall
120         elif [[ $1 = "d" ]]
121         then
122                 delexe
123 
124         elif [[ $1 = "c" ]]
125         then
126                 gccexe
127 
128         elif [[ $1 = "x" ]]
129         then
130 
131                 lsexe
132 
133                 delexe
134 
135                 lsall
136 
137                 gccexe
138 
139                 lsall
140 
141         else
142                 echo
143                 echo
144                 echo '  OS: REGCC: Nothing to do .'
145                 echo
146                 echo
147         fi
148 }
149 
150 
151 run $1
152 
153 
154 [root@rocky c]# 
155 [root@rocky c]# 

 

 

 

 

 

 

三、应用示例

 

  1 [root@rocky c]# ll
  2 total 136
  3 -rwxr-xr-x 1 root root 25928 Dec 24 23:29 all_test
  4 -rw-r--r-- 1 root root   837 Dec 24 19:00 all_test.c
  5 drwxr-xr-x 2 root root    46 Dec 24 22:27 comb
  6 -rwxr-xr-x 1 root root 25904 Dec 24 23:29 getin
  7 -rw-r--r-- 1 root root   279 Dec 19 00:46 getin.c
  8 -rwxr-xr-x 1 root root 25888 Dec 24 23:29 macro_test
  9 -rw-r--r-- 1 root root   201 Dec 24 17:24 macro_test.c
 10 -rwx--x--x 1 root root  2480 Dec 24 23:26 regcc
 11 -rwx--x--x 1 root root  2480 Dec 24 23:26 regcc-backup
 12 -rwxr-xr-x 1 root root 26024 Dec 24 23:29 return_struct
 13 -rw-r--r-- 1 root root   905 Dec 24 19:01 return_struct.c
 14 [root@rocky c]# 
 15 [root@rocky c]# 
 16 [root@rocky c]# ./regcc  a
 17 
 18 
 19   == REGCC help: start 
 20 
 21       COMMAND     <OPTION> 
 22       ./regcc     <a|l|d|c|x> 
 23 
 24       OPTIONS      FUNCTION
 25          a         list all files in the current directory .
 26          l         list all executable files in the current directory .
 27          d         delete all executable files with a same name of c source files(*.c) in current directory .
 28          c         compile all files of c source files in current directory .
 29          x         list files in the current directory;
 30                         delete files with a same name within source files(*.c) ;
 31                         generate exe files by (*.c) .
 32 
 33       EXAMPLE    OPTIONS      FUNCTIONS
 34       ./regcc      l          list all executable files in current directory .
 35       ./regcc      d          delete all executable with a same name of c source files in current directory .
 36 
 37   == REGCC help: end 
 38 
 39 
 40 
 41   == List All Files: Start ==
 42       List_Name_1:     all_test
 43       List_Name_2:     all_test.c
 44       List_Name_3:     comb
 45       List_Name_4:     getin
 46       List_Name_5:     getin.c
 47       List_Name_6:     macro_test
 48       List_Name_7:     macro_test.c
 49       List_Name_8:     regcc
 50       List_Name_9:     regcc-backup
 51       List_Name_10:     return_struct
 52       List_Name_11:     return_struct.c
 53   == List All Files: END ==
 54 
 55 [root@rocky c]# 
 56 [root@rocky c]# 
 57 [root@rocky c]# ./regcc  l
 58 
 59 
 60   == REGCC help: start 
 61 
 62       COMMAND     <OPTION> 
 63       ./regcc     <a|l|d|c|x> 
 64 
 65       OPTIONS      FUNCTION
 66          a         list all files in the current directory .
 67          l         list all executable files in the current directory .
 68          d         delete all executable files with a same name of c source files(*.c) in current directory .
 69          c         compile all files of c source files in current directory .
 70          x         list files in the current directory;
 71                         delete files with a same name within source files(*.c) ;
 72                         generate exe files by (*.c) .
 73 
 74       EXAMPLE    OPTIONS      FUNCTIONS
 75       ./regcc      l          list all executable files in current directory .
 76       ./regcc      d          delete all executable with a same name of c source files in current directory .
 77 
 78   == REGCC help: end 
 79 
 80 
 81 
 82 
 83   == List Executable Files of C : start ==
 84       LIST_EXE_NAME_1: all_test
 85       LIST_EXE_NAME_2: comb
 86       LIST_EXE_NAME_3: getin
 87       LIST_EXE_NAME_4: macro_test
 88       LIST_EXE_NAME_5: regcc
 89       LIST_EXE_NAME_6: regcc-backup
 90       LIST_EXE_NAME_7: return_struct
 91   == List Executable Files of C : over ==
 92 
 93 
 94 [root@rocky c]# 
 95 [root@rocky c]# 
 96 [root@rocky c]# ./regcc  d
 97 
 98 
 99   == REGCC help: start 
100 
101       COMMAND     <OPTION> 
102       ./regcc     <a|l|d|c|x> 
103 
104       OPTIONS      FUNCTION
105          a         list all files in the current directory .
106          l         list all executable files in the current directory .
107          d         delete all executable files with a same name of c source files(*.c) in current directory .
108          c         compile all files of c source files in current directory .
109          x         list files in the current directory;
110                         delete files with a same name within source files(*.c) ;
111                         generate exe files by (*.c) .
112 
113       EXAMPLE    OPTIONS      FUNCTIONS
114       ./regcc      l          list all executable files in current directory .
115       ./regcc      d          delete all executable with a same name of c source files in current directory .
116 
117   == REGCC help: end 
118 
119 
120 
121 
122   == Delete Executable Files of C : start ==
123       DELETE_EXE_NAME_1: all_test
124       DELETE_EXE_NAME_2: getin
125       DELETE_EXE_NAME_3: macro_test
126       DELETE_EXE_NAME_4: return_struct
127   == Delete Executable Files of C : over ==
128 
129 
130 [root@rocky c]# 
131 [root@rocky c]# 
132 [root@rocky c]# ls -l
133 total 24
134 -rw-r--r-- 1 root root  837 Dec 24 19:00 all_test.c
135 drwxr-xr-x 2 root root   46 Dec 24 22:27 comb
136 -rw-r--r-- 1 root root  279 Dec 19 00:46 getin.c
137 -rw-r--r-- 1 root root  201 Dec 24 17:24 macro_test.c
138 -rwx--x--x 1 root root 2480 Dec 24 23:26 regcc
139 -rwx--x--x 1 root root 2480 Dec 24 23:26 regcc-backup
140 -rw-r--r-- 1 root root  905 Dec 24 19:01 return_struct.c
141 [root@rocky c]# 
142 [root@rocky c]# 
143 [root@rocky c]# ./regcc  c
144 
145 
146   == REGCC help: start 
147 
148       COMMAND     <OPTION> 
149       ./regcc     <a|l|d|c|x> 
150 
151       OPTIONS      FUNCTION
152          a         list all files in the current directory .
153          l         list all executable files in the current directory .
154          d         delete all executable files with a same name of c source files(*.c) in current directory .
155          c         compile all files of c source files in current directory .
156          x         list files in the current directory;
157                         delete files with a same name within source files(*.c) ;
158                         generate exe files by (*.c) .
159 
160       EXAMPLE    OPTIONS      FUNCTIONS
161       ./regcc      l          list all executable files in current directory .
162       ./regcc      d          delete all executable with a same name of c source files in current directory .
163 
164   == REGCC help: end 
165 
166 
167 
168 
169   == Compile *.c Files: start ==
170   == Compile *.c Files: over ==
171 
172 
173 [root@rocky c]# 
174 [root@rocky c]# 
175 [root@rocky c]# ls -l
176 total 136
177 -rwxr-xr-x 1 root root 25928 Dec 24 23:34 all_test
178 -rw-r--r-- 1 root root   837 Dec 24 19:00 all_test.c
179 drwxr-xr-x 2 root root    46 Dec 24 22:27 comb
180 -rwxr-xr-x 1 root root 25904 Dec 24 23:34 getin
181 -rw-r--r-- 1 root root   279 Dec 19 00:46 getin.c
182 -rwxr-xr-x 1 root root 25888 Dec 24 23:34 macro_test
183 -rw-r--r-- 1 root root   201 Dec 24 17:24 macro_test.c
184 -rwx--x--x 1 root root  2480 Dec 24 23:26 regcc
185 -rwx--x--x 1 root root  2480 Dec 24 23:26 regcc-backup
186 -rwxr-xr-x 1 root root 26024 Dec 24 23:34 return_struct
187 -rw-r--r-- 1 root root   905 Dec 24 19:01 return_struct.c
188 [root@rocky c]# 
189 [root@rocky c]# 
190 [root@rocky c]# ./regcc  x
191 
192 
193   == REGCC help: start 
194 
195       COMMAND     <OPTION> 
196       ./regcc     <a|l|d|c|x> 
197 
198       OPTIONS      FUNCTION
199          a         list all files in the current directory .
200          l         list all executable files in the current directory .
201          d         delete all executable files with a same name of c source files(*.c) in current directory .
202          c         compile all files of c source files in current directory .
203          x         list files in the current directory;
204                         delete files with a same name within source files(*.c) ;
205                         generate exe files by (*.c) .
206 
207       EXAMPLE    OPTIONS      FUNCTIONS
208       ./regcc      l          list all executable files in current directory .
209       ./regcc      d          delete all executable with a same name of c source files in current directory .
210 
211   == REGCC help: end 
212 
213 
214 
215 
216   == List Executable Files of C : start ==
217       LIST_EXE_NAME_1: all_test
218       LIST_EXE_NAME_2: comb
219       LIST_EXE_NAME_3: getin
220       LIST_EXE_NAME_4: macro_test
221       LIST_EXE_NAME_5: regcc
222       LIST_EXE_NAME_6: regcc-backup
223       LIST_EXE_NAME_7: return_struct
224   == List Executable Files of C : over ==
225 
226 
227 
228 
229   == Delete Executable Files of C : start ==
230       DELETE_EXE_NAME_1: all_test
231       DELETE_EXE_NAME_2: getin
232       DELETE_EXE_NAME_3: macro_test
233       DELETE_EXE_NAME_4: return_struct
234   == Delete Executable Files of C : over ==
235 
236 
237 
238   == List All Files: Start ==
239       List_Name_1:     all_test.c
240       List_Name_2:     comb
241       List_Name_3:     getin.c
242       List_Name_4:     macro_test.c
243       List_Name_5:     regcc
244       List_Name_6:     regcc-backup
245       List_Name_7:     return_struct.c
246   == List All Files: END ==
247 
248 
249 
250   == Compile *.c Files: start ==
251   == Compile *.c Files: over ==
252 
253 
254 
255   == List All Files: Start ==
256       List_Name_1:     all_test
257       List_Name_2:     all_test.c
258       List_Name_3:     comb
259       List_Name_4:     getin
260       List_Name_5:     getin.c
261       List_Name_6:     macro_test
262       List_Name_7:     macro_test.c
263       List_Name_8:     regcc
264       List_Name_9:     regcc-backup
265       List_Name_10:     return_struct
266       List_Name_11:     return_struct.c
267   == List All Files: END ==
268 
269 [root@rocky c]# 
270 [root@rocky c]# 
271 [root@rocky c]# ls -l
272 total 136
273 -rwxr-xr-x 1 root root 25928 Dec 24 23:36 all_test
274 -rw-r--r-- 1 root root   837 Dec 24 19:00 all_test.c
275 drwxr-xr-x 2 root root    46 Dec 24 22:27 comb
276 -rwxr-xr-x 1 root root 25904 Dec 24 23:36 getin
277 -rw-r--r-- 1 root root   279 Dec 19 00:46 getin.c
278 -rwxr-xr-x 1 root root 25888 Dec 24 23:36 macro_test
279 -rw-r--r-- 1 root root   201 Dec 24 17:24 macro_test.c
280 -rwx--x--x 1 root root  2480 Dec 24 23:26 regcc
281 -rwx--x--x 1 root root  2480 Dec 24 23:26 regcc-backup
282 -rwxr-xr-x 1 root root 26024 Dec 24 23:36 return_struct
283 -rw-r--r-- 1 root root   905 Dec 24 19:01 return_struct.c
284 [root@rocky c]# 
285 [root@rocky c]# 
286 [root@rocky c]# 

 

 

 

 

 

 

posted on 2022-12-24 18:55  lnlidawei  阅读(61)  评论(0编辑  收藏  举报