由于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>
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中,如下
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/
|