|
From: | Steven W. Orr |
Subject: | Re: equivalent of Linux readlink -f in pure bash? |
Date: | Tue, 09 Aug 2011 09:50:49 -0400 |
User-agent: | Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 |
On 8/9/2011 5:29 AM, Bernd Eggink wrote:
On 09.08.2011 03:44, Jon Seymour wrote:Has anyone ever come across an equivalent to Linux's readlink -f that is implemented purely in bash?You can find my version here: http://sudrala.de/en_d/shell-getlink.html As it contains some corrections from Greg Wooledge, it should handle even pathological situations. ;) Bernd
I'd just like to make a couple of suggestions for your script (I hope these are welcome):
*) You reset OPTIND to 1 but you didn't declare it local. This will cause any caller of getlink which uses getopts to reset its variable to 1. (I mention this because it cost me a couple of hours a while back.)
When calling getopts, especially from a function that is intended to not be used at a top level for processing command line options, you should declare local copies of OPTIND, OPTARG and OPTERR.
*) To remove the trailing slashes, instead of while [[ $file == */ ]] do file=${file%/} done file=${file##*/} # file name just say file="${file%${file##*[!/]}}" *) Instead of [[ ! -d $dir ]] && { ret=1 break } how about this for slightly cleaner? [[ -d $dir ]] || { ret=1 break } -- Time flies like the wind. Fruit flies like a banana. Stranger things have .0. happened but none stranger than this. Does your driver's license say Organ ..0 Donor?Black holes are where God divided by zero. Listen to me! We are all- 000 individuals! What if this weren't a hypothetical question? steveo at syslang.net
[Prev in Thread] | Current Thread | [Next in Thread] |