Sunday, 14 April 2013

Shell Scripting ||| Convert "relative" in "absolute" path name

In shell scripts it is often necessary to convert a  relative path name, i.e. "../usr/../lib/somefile" to
 an absolute path name starting with a slash "/", i.e. "/lib/somefile". The following code fragment does exactly this:

     D=`dirname "$relpath"`
     B=`basename "$relpath"`
      
abspath="`cd \"$D\" 2>/dev/null && pwd || echo \"$D\"`/$B"

     

0 blogger-disqus:

Post a Comment