Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!gegeweb.org!eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Rainer Weikusat Newsgroups: comp.unix.shell,comp.unix.programmer,comp.programming.contests Subject: Re: The First Pure Shell Contest (PUSH): relativepath Date: Sun, 21 Aug 2011 15:14:56 +0100 Lines: 20 Message-ID: <87fwkuzw9r.fsf@sapphire.mobileactivedefense.com> References: <9b7kg7F3njU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: individual.net tXh1ziWzJYCuddb/uHvaTQGuGGLnceJGBWCnKiDR0CjJlRGo0= Cancel-Lock: sha1:vesb8GfDr9YVbF0pkleptFqPiyY= sha1:QNLs5rDBZKmG8kmKTIiVa/O9o50= User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.2 (gnu/linux) Xref: x330-a1.tempe.blueboxinc.net comp.unix.shell:1771 comp.unix.programmer:1173 comp.programming.contests:21 Stephane CHAZELAS writes: > 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/^$/./' It's a nice demonstration that some people really excel in the art of creating complicated solutions to simple problems.