Macbook M1 自己编译realpath命令

由于mac系统不自带realpath命令,调用出错

zsh: command not found: realpath

网上说要brew install coreutils,但好像没有m1的源

jesse@JessedeMacBook-Air ~ % brew install coreutils
==> Searching for similarly named formulae...
Error: No similarly named formulae found.
Error: No available formula or cask with the name "coreutils".
==> Searching for a previously deleted formula (in the last month)...
Error: No previously deleted formula found.
==> Searching taps on GitHub...
Error: No formulae found in taps.

所以打算自己编一个realpath

realpath.c源文件

#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

/*
gcc -MD -o realpath realpath.c
*/
int main(int argc, char *argv[]) {
    if(argc < 2) {
        printf("usage: $0 path [path...]\n");
        return 1;
    }

    int i;
    char *rpath;
    for(i=1; i<argc; i++) {
        rpath = realpath(argv[i], NULL);
        if(rpath) {
            printf("%s\n", rpath);
            free(rpath);
        } else {
            fprintf(stderr, "realpath %s error: %d, %s\n", argv[i], errno, strerror(errno));
        }
    }
    return 0;
}

运行gcc编译命令,生成realpath可执行文件

jesse@JessedeMacBook-Air realpath % gcc -MD -o realpath realpath.c
jesse@JessedeMacBook-Air realpath % ls
realpath   realpath.c realpath.d

把可执行文件复制到path路径内即可

由于mac系统的bin,sbin目录不可以修改,我是在用户目录下建立个bin目录存放一下自己的命令

mkdir ~/bin
cp realpath ~/bin

修改vim ~/.bash_profile把bin目录加入path中,如下

# 用户bin
export USERBIN=/Users/jesse/bin
export PATH=$PATH:$USERBIN

最后执行一下source ~/.bash_profile就可以愉快的使用realpath了

jesse@JessedeMacBook-Air realpath % source ~/.bash_profile 
jesse@JessedeMacBook-Air realpath % realpath
/tmp/realpath/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值