#include <unistd.h>
#include <stdio.h>
int main(int argc , char* argv[])
{
char buf[1024] = { 0 };
int n;
n = readlink("/proc/self/exe" , buf , sizeof(buf));
if( n > 0 && n < sizeof(buf))
{
printf("%s/n" , buf);
}
}
连接符: /proc/self/exe 代表当前路径
如程序在/home/workspace/test中,运行输出:/home/workspace/tests
shell 脚本中获取当前脚本的绝对路径
baseDirForScriptSelf=$(cd "$(dirname "$0")"; pwd)
echo "full path to currently executed script is : ${baseDirForScriptSelf}"