modified: tasks/common.wdl
[GalaxyCodeBases.git] / tools / etc / FindBin.sh
blob473dc364ef3d515db338110709d740b22ae7c031
1 #!/bin/bash
3 # This is a shell script to find the 'RealBin' and 'RealScript' for the
4 # given input command
6 # Change History:
7 # 1.0.1 - Basic release.
12 # Prints the absolute path of the given input directory.
13 # Usage: abs=`abs_path <dirName>`
14 function abs_path() {
15 local dir=$1
16 "cd" "$dir"
17 if [ "$?" = "0" ]; then
18 /bin/pwd
19 "cd" - &>/dev/null
23 # Finds the file from the 'PATH' environmental variable
24 # and then prints it's dir name.
25 # Usage: fromPATH <file>
26 function fromPATH() {
27 local oldIfs=$IFS
28 local file=$1
29 local temp=""
30 IFS=":"
31 for dir in $PATH; do
32 temp=$dir/$file
33 if [ -r "$temp" ]; then
34 temp=`/usr/bin/dirname "$temp"`
35 abs_path "$temp"
36 break
38 done
39 IFS=$oldIfs
42 # Equivalent to perl's FindBin::RealBin
43 # Usage: RealBin <file>
44 function RealBin() {
45 local file=$1
46 local temp=""
47 if [ -r "$file" ]; then
48 temp=`/usr/bin/dirname "$file"`
49 abs_path "$temp"
50 else
51 fromPATH $file
55 # Equivalaent to perl's FindBin::RealScript
56 # Usage: RealScript <file>
57 function RealScript() {
58 local file=$1
59 local temp=`RealBin "$file"`
60 temp=$temp/`/bin/basename "$file"`
61 temp=`/usr/bin/readlink -f "$temp"`
62 if [ "$temp" != "" ]; then
63 /bin/basename "$temp"
67 # help and exit
68 function showHelp() {
69 echo " FindBin.sh - Bash equivalent for Perl's FindBin.
70 USAGE:
71 FindBin.sh [-h, -v] [-bin | -script] <file>
72 -h Print this help and exit.
73 -v Print version information and exit.
74 -bin Print the value of FindBin::RealBin.
75 -script Print the value of FindBin::RealScript.
76 <file> The file whose FindBin::RealBin & FindBin::RealScript
77 are needed."
78 exit 0
81 # version info
82 function showVersion() {
83 echo "FindBin.sh - v1.0.1"
84 exit 0
88 cmd="RealBin"
89 if [ "$1" = "-h" ]; then
90 showHelp
91 elif [ "$1" = "-v" ]; then
92 showVersion
93 elif [ "$1" = "-bin" ]; then
94 cmd="RealBin"
95 shift
96 elif [ "$1" = "-script" ]; then
97 cmd="RealScript"
98 shift
100 "$cmd" "$1"