how to check if a path is actual or symbolic link

佐手、 提交于 2020-01-05 09:16:12

问题


I am writing my own shell program. I am currently implementing the cd command using chdir. I want to implement the cd with the below options :

  • -P Do not follow symbolic links
  • -L Follow symbolic links (default)

When a given path is entered on the shell, how to figure out if the path is a symbolic link or an absolute path progamatically?

Thanks


回答1:


Check out the lstat() function , you need to use S_ISLNK on the st_mode field.




回答2:


if [ -L /path/to/file ]; then
  echo "is a symlink!"
else
  echo "not a symlink! maybe a directory or regular file, or does not exist"
end


来源:https://stackoverflow.com/questions/3032897/how-to-check-if-a-path-is-actual-or-symbolic-link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!