Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.tele.dk!feed118.news.tele.dk!news.tele.dk!small.news.tele.dk!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Jens Schweikhardt Newsgroups: comp.unix.shell,comp.unix.programmer,comp.programming.contests Subject: Re: The First Pure Shell Contest (PUSH): relativepath Date: 21 Aug 2011 10:43:17 GMT Lines: 33 Sender: Jens Schweikhardt Message-ID: <9bc5q4FkltU1@mid.individual.net> References: <9b7kg7F3njU1@mid.individual.net> X-Trace: individual.net aK6gnR56xod17fy4CcJhGg48x/cBYBHRbF3MJMILmP4MEc82BP X-Orig-Path: not-for-mail Cancel-Lock: sha1:TpNYUtuFn+PrBveon+vZdYFVjRA= User-Agent: tin/1.9.6-20101126 ("Burnside") (UNIX) (FreeBSD/9.0-CURRENT (i386)) Xref: x330-a1.tempe.blueboxinc.net comp.unix.shell:1766 comp.unix.programmer:1172 comp.programming.contests:20 In comp.programming.contests Stephane CHAZELAS wrote: # 2011-08-19, 17:23(+00), Jens Schweikhardt: # [...] #> Write a POSIX shell function named 'relativepath' which takes as #> arguments two absolute canonicalized pathnames, and stores the relative #> path from the first to the second in the variable "result" or "." when #> the arguments are the same. Example: relativepath /foo/bar /foo/baz sets #> result to "../baz". # [...] # # In the not-answering-the-question category, what about: # # printf '%s\n' "$1//$2/"|sed -e:1 -e'$!{N;b1' -e} -e' # s|/////*|///|;s|\(.*/\)\(.*/\)\1|\2|;:2 # s|^[^/]*/\(\(.*/\)*/\)|\1../|;t2 # s|^/||;s|/$||;s/^$/./' Cool! Since semicolons are free, what about using them as pattern delimiters: printf '%s\n' "$1//$2/"|sed -e:1 -e'$!{N;b1' -e} -e' s;/////*;///;;s;\(.*/\)\(.*/\)\1;\2;;:2 s;^[^/]*/\(\(.*/\)*/\);\1../;;t2 s;^/;;;s;/$;;;s/^$/./' Note to contestants: Not a valid entry due to sed, which cannot assumed to be a builtin of the contest evaluation machine's shell :-) Regards, Jens -- Jens Schweikhardt http://www.schweikhardt.net/ SIGSIG -- signature too long (core dumped)