1

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

hits_lucky
  • 327
  • 2
  • 9
  • 18
  • Both Unknown and zed_0xff are correct because your question is posed oddly. In a shell script you use the `test` command (a.k.a. `[`) if you are **writing** a shell, you use `stat()`. – msw Jun 13 '10 at 16:19

2 Answers2

3

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

Unknown
  • 5,722
  • 5
  • 43
  • 64
3
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
zed_0xff
  • 32,417
  • 7
  • 53
  • 72