Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #!/bin/bash
  2. ###############################################################################
  3. # fullpath.sh - Find the absolute path to the given file or directory.
  4. #
  5. # updated: 2017-03-23
  6. # created: 2005-05-25
  7. # author: Kevin C. Krinke <kevin@krinke.ca>
  8. # license: LGPL-2.1
  9. ###############################################################################
  10.  
  11. #
  12. # Global Variables
  13. #
  14.  
  15. SCRIPT_VERSION=1.0
  16. SCRIPT_NAME=$(basename "$0")
  17.  
  18. V_NORMAL=1
  19. V_QUIET=0
  20. V_DEBUG=2
  21.  
  22. G_VERBOSITY=$V_NORMAL
  23. G_NULL_TERMINATE=0
  24. declare -A TARGET_PATHS
  25.  
  26. #
  27. # Helper Functions
  28. #
  29.  
  30. function STDOUT () { while [ $# -gt 0 ]; do echo "$1"; shift; done; }
  31. function STDERR () { while [ $# -gt 0 ]; do echo "$1" 1>&2; shift; done; }
  32. function ERROR () { while [ $# -gt 0 ]; do STDERR "ERROR: $1"; shift; done; }
  33. function DEBUG () { [ $G_VERBOSITY -ge $V_DEBUG ] && STDERR "$@"; }
  34. function NOTIFY () { [ $G_VERBOSITY -ge $V_NORMAL ] && STDOUT "$@"; }
  35. function DIE () { ERROR "$@"; exit 1; }
  36.  
  37. function display_usage() {
  38. [ $# -gt 0 ] && ERROR "$@"
  39. STDERR "usage: ${SCRIPT_NAME} [options] <path>"
  40. STDERR ""
  41. STDERR "options:"
  42. STDERR " -h --help this helpful screen"
  43. STDERR " -v --verbose increase verbosity"
  44. #STDERR " -q --quiet silence normal output"
  45. STDERR " -z --null null terminated instead of newlines"
  46. exit 1
  47. }
  48.  
  49. export EXPANDED_PATH=
  50. function expand_abspath() {
  51. this_path=$(echo "$1" | perl -pe "s#^\./#$(pwd)/#;s#^([^/])#${PWD}/\1#;")
  52. root_path=$(echo "${this_path}" | perl -pe 's/^\///')
  53. declare -a all_pieces
  54. typeset -i I
  55. I=0
  56. orig_ifs="${IFS}"
  57. IFS='/'
  58. for piece in $root_path
  59. do
  60. all_pieces[$I]="${piece}"
  61. I=$((I+1))
  62. done
  63. IFS="${orig_ifs}"
  64. full_path=""
  65. for piece in "${all_pieces[@]}"
  66. do
  67. [ "${piece}" == "." ] && continue
  68. if [ "${piece}" == ".." ]
  69. then
  70. pushd "${full_path}/.." 2>&1 > /dev/null
  71. full_piece=$(pwd -P)
  72. popd 2>&1 > /dev/null
  73. DEBUG "${full_piece} is a relative parent (..)"
  74. full_path="${full_piece}"
  75. continue
  76. fi
  77. full_piece="${full_path}/${piece}"
  78. if [ -L "${full_piece}" ]
  79. then
  80. if [ -d "${full_piece}" ]
  81. then
  82. pushd "${full_piece}" 2>&1 > /dev/null
  83. link_path=$(pwd -P)
  84. popd 2>&1 > /dev/null
  85. DEBUG "${full_piece} is a path symlink to ${link_path}"
  86. full_path="${link_path}"
  87. else
  88. link_path=$(stat --format="%N" "${full_piece}" \
  89. | perl -pe 's!^.+?\-\> ‘(.+?)’\s*$!$1!;' )
  90. DEBUG "${full_piece} is a file symlink to ${link_path}"
  91. full_path="${link_path}"
  92. fi
  93. continue
  94. fi
  95. if [ -d "${full_piece}" ]
  96. then
  97. full_path="${full_piece}"
  98. DEBUG "${full_path} is a directory"
  99. continue
  100. fi
  101. if [ -f "${full_piece}" ]
  102. then
  103. full_path="${full_piece}"
  104. DEBUG "${full_path} is a file"
  105. continue
  106. fi
  107. # from this point forward, things don't exist
  108. DEBUG "${full_piece} does not exist"
  109. full_path="${full_piece}"
  110. done
  111. export EXPANDED_PATH="${full_path}"
  112. }
  113.  
  114. declare -a OUTPUT_QUEUE
  115. function queue_output() {
  116. IDX=${#OUTPUT_QUEUE[@]}
  117. OUTPUT_QUEUE[${IDX}]="$1"
  118. }
  119.  
  120. function output_queue() {
  121. for item in "${OUTPUT_QUEUE[@]}"
  122. do
  123. if [ $G_NULL_TERMINATE -eq 1 ]
  124. then
  125. printf '%s%b' "${item}" '\0'
  126. else
  127. echo "${item}"
  128. fi
  129. done
  130. }
  131.  
  132. #
  133. # Main Function
  134. #
  135.  
  136. function main() {
  137. typeset -i I
  138. for ((I=0; I<${#TARGET_PATHS[@]}; I++))
  139. do
  140. this_path="${TARGET_PATHS[${I}]}"
  141. DEBUG "Processing: ${this_path}"
  142. expand_abspath "${this_path}"
  143. queue_output "${EXPANDED_PATH}"
  144. done
  145. output_queue
  146. return 0
  147. }
  148.  
  149. #
  150. # Command Line Processing
  151. #
  152.  
  153. IDX=0
  154. while [ $# -gt 0 ]
  155. do
  156. OPTION="$1"
  157. OPTARG="$2"
  158. case "${OPTION}" in
  159. "-z"|"--null") G_NULL_TERMINATE=1;;
  160. #"-q"|"--quiet") G_VERBOSITY=$V_QUIET;;
  161. "-v"|"--verbose") G_VERBOSITY=$((G_VERBOSITY+1));;
  162. "-h"|"--help") display_usage;;
  163. *)
  164. # options start with a '-'
  165. echo "${OPTION}" | egrep -q '^\s*\-'
  166. [ $? -eq 0 ] && display_usage "Unknown option \"${OPTION}\""
  167. # assume everything else is the path?
  168. TARGET_PATHS[$IDX]=${OPTION}
  169. IDX=$((IDX+1))
  170. ;;
  171. esac
  172. shift
  173. done
  174.  
  175. # Kickoff
  176. main ; exit $?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement