您的位置:首页 > 运维架构 > Shell

shell自动编译简单入门

2020-07-19 04:59 1456 查看

动机

用于测试的一组程序比较多,每次编译命令麻烦,写个shell自动执行。

示例

简单示例

# clean
echo "************"
echo "clean project"
rm ./bin/*
echo "clean complete"
echo "************"
# compile link
echo "************************"
echo "gcc start"
gcc fork_parent_first.c -o ./bin/fork_parent_first
gcc fork_child_first.c -o ./bin/fork_child_first
echo "gcc complete"
echo "************************"

简单项目示例1

test.c

/*test.c*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

int main() {

init_menu();
start_menu();

return 0;
}

menu.c

/*menu.c*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

#define MENU_COUNT  5
#define MENU_PROMPT  9

char* menu[MENU_PROMPT];
char* separator;
char* prompt;

int init_menu() {

menu[0] = "************************************\n";
menu[1] = "*      This is my shel demol. Please try it.   *\n";
menu[2] = "*      1 : File                                *\n";
menu[3] = "*      2 : Edit                                *\n";
menu[4] = "*      3 : View                                *\n";
menu[5] = "*      4 : Tool                                *\n";
menu[6] = "*      0 : Quit                                *\n";
menu[7] = "************************************\n";
menu[8] = "*      You can use \"0: Quit\" to exit.\n";

separator = "************************************\n";
prompt  = "my shell >>> ";

return 0;
}

int start_menu() {

int user_choice = 0;
int quit = 0;

while(1) {
user_choice = display_menu();
quit = dispatch_task(user_choice);
if (quit)
break;
}

return 0;
}

int display_menu() {

int i;
for ( i=0; i<MENU_PROMPT; i++ ) {
printf(menu[i]);
}

printf(prompt);

int choice = 0;
scanf("%d", &choice);

return choice;
}

util.c

/*util.c*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>

int dispatch_task(int choice) {

int result_quit = 0;
switch (choice) {
case 1:
func1();
break;
case 2:
func2();
break;
case 3:
func3();
break;
case 4:
func4();
break;
case 0:
quit();
result_quit = 1;
break;
}

return result_quit;
}

int func1() {
printf("********** Task is processing. **********\n");
printf("This is function1 File. \n");
printf("This is function1 File. \n");
printf("This is function1 File. \n");
printf("*********** Task is completed. **********\n");
}

int func2() {
printf("********** Task is processing. **********\n");
printf("This is function2 Edit. \n");
printf("This is function2 Edit. \n");
printf("This is function2 Edit. \n");
printf("*********** Task is completed. **********\n");
}

int func3() {
printf("********** Task is processing. **********\n");
printf("This is function3 View. \n");
printf("This is function3 View. \n");
printf("This is function3 View. \n");
printf("*********** Task is completed. **********\n");
}

int func4() {
printf("********** Task is processing. **********\n");
printf("This is function4 Tool. \n");
printf("This is function4 Tool. \n");
printf("This is function4 Tool. \n");
printf("*********** Task is completed. **********\n");
}

int quit() {
printf("************************************\n");
printf("The Program demo is over. \n");
printf("************************************\n");
}

mymake.sh

# clean
echo "************"
echo "clean project"
rm ./bin/*
echo "clean complete"
echo "************"
# compile link
echo "************************"
echo "gcc start"
gcc test.c menu.c util.c -g -o ./bin/test
echo "gcc complete"
echo "************************"

简单项目示例2

# clean
echo "************"
echo "clean project"
rm ./obj/*
rm ./bin/*
echo "clean complete"
echo "************"
# compile link
echo "************************"
echo "gcc start"
gcc -c util.c -o ./obj/util.o
gcc -c menu.c -o ./obj/menu.o
gcc -c test.c -o ./obj/test.o
gcc ./obj/test.o ./obj/menu.o ./obj/util.o -o ./bin/test
echo "gcc complete"
echo "************************"

示例中的命令及参数说明

echo
命令
rm
命令
gcc
命令
-c
参数
-o
参数
-g
参数

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: