shell脚本中如何判断一个目录是不是另一个目录的子目录
发表于 : 2012-01-13 18:04
rt,比如/var/cache目录是/var/././cache/man/zh_CN目录的间接父目录,这种父子关系怎么判断?
第一次写shell,求指教
第一次写shell,求指教

代码: 全选
$ realpath
bash: realpath: 未找到命令
代码: 全选
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <limits.h>
int main(int argc, char* argv[])
{
int ret = 0;
#ifdef PATH_MAX
static int const sc_path_max = PATH_MAX;
#else
static int const sc_path_max = pathconf(path, _PC_PATH_MAX);
if (sc_path_max <= 0) {
sc_path_max = 4096;
}
#endif
if (1 < argc) {
char* p_realpath = NULL;
p_realpath = (char*)malloc(sc_path_max);
ret = (NULL == realpath(argv[1], p_realpath)) ? (-1) : 0;
if (-1 == ret) {
printf("[ERROR] get realpath failed.\n");
} else {
printf("%s", p_realpath);
}
free(p_realpath);
} else {
printf("usage: realpath pathname\n");
}
return ret;
}
恩,EE正解,朕就是这么干的eexpress 写了:realpath不是应该得到../../xxx 这样的嘛。这怎么判断父子关系呢。
应该都获取绝对路径,然后字符串比较前级。