shosei

lnptr

May 11th, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.18 KB | None | 0 0
  1. #!/bin/sh
  2. #######################################
  3. ## Hand in a symlink, get back its (end) target
  4. ## Usage: lnptr FILE [FILE...]
  5. ## lnptr 0.1, Copyright (c) 2003 by Adam Katz <[email protected]>, GPL
  6. #######################################
  7.  
  8. # ensure we have a string and it is a file or directory
  9. [ -z "$1" ] && exit 0
  10. [ -f "$1" ] || [ -d "$1" ] || if echo $1 |grep '^-' >/dev/null
  11.   then grep '^## ' `which $0 2>/dev/null || echo $0` |sed 's/^...//' && exit 0
  12.   else echo "`echo $0|sed 's:.*/::g'`: $1: No such file or directory" && exit 1
  13. fi  # (solaris 5.9's /bin/sh doesn't like `[ -e filename ]')
  14. pointer="$1"
  15.  
  16. while true; do
  17.  
  18.   this="$pointer"
  19.  
  20.   pointer=`ls -l "$this" |awk '{print $11}'` # link target (empty if not a link)
  21.   [ -z "$pointer" ] && break                 # if not a link, we're done
  22.  
  23.   if echo "$pointer" |grep -v '^/' >/dev/null; then
  24.     pdir=`dirname "$this"`
  25.     pointer="$pdir/$pointer"                 # make absolute
  26.   fi
  27.  
  28. done
  29.  
  30. cd `dirname "$this"`                         # get rid of ../.. etc w/out sed
  31.  
  32. echo "`pwd`/`echo $this |sed 's:^.*/::g'`"   # the answer!
  33.  
  34. [ -n "$2" ] && shift && "$0" "$@"            # call this on remaining arguments
Advertisement
Add Comment
Please, Sign In to add comment