编写自己的linux bin文件

15 篇文章 0 订阅

一个简单的小程序模拟一条 linux命令 生成的一种方法。


my_bin.c

#include "stdio.h"

#define MAX_SIZE 10
#define TRUE    1
#define FALSE   0

void usage()
{
    printf("********************* usage *********************\n");
    /*
     * usage:
     */
	 
    printf("\n");
    return ;
}

int main(int argc, void **argv)
{
    char c;
    
    printf("Welcome to my bin.\n");
    
    while (TRUE)
    {
        c = getchar();
        if ('\n' == c)
            continue;
            
        switch (c)
        {
            case 'p':
            {
                printf("hello\n\n");
                break;
            }
            case 'h':
            {
                usage();
                break;
            }
            case 'q':
            {
                printf("bye bye\n\n");
                return 0;
                break;
            }
            default:
            {
                printf("your input is wrong!\n");
                usage();
                break;
            }
        }
    }
    
    return 0;
}


makefile文件:

CC=gcc
INSTDIR=/usr/local/bin

my_bin : my_bin.o
	$(CC) -o my_bin my_bin.o

my_bin.o : my_bin.c public.h
	$(CC) -c my_bin.c

install : my_bin
	@if [ -d $(INSTDIR) ]; then \
		cp my_bin $(INSTDIR); \
		chmod a+x $(INSTDIR)/my_bin; \
		echo "install to $(INSTDIR) success"; \
	else \
		echo "sorry, $(INSTDIR) is not exist"; \
	fi    
    
clean:
	-rm my_bin my_bin.o $(INSTDIR)/my_bin

编译安装之前:

linux:/mnt/hgfs/vmware-share # my_bin
bash: /usr/local/bin/my_bin: 没有那个文件或目录

编译并且安装:

linux:/mnt/hgfs/vmware-share # make install
gcc -c my_bin.c
gcc -o my_bin my_bin.o
install to /usr/local/bin/ success

运行结果:

linux:/mnt/hgfs/vmware-share # my_bin
Welcome to my bin.
p
hello

a
your input is wrong!
********************* usage *********************

h
********************* usage *********************

q
bye bye

linux:/mnt/hgfs/vmware-share #


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值