Add an absolute path prefix only to relative paths

Add an absolute path prefix only to relative paths

Post by Janis Papanagno » Sat, 16 Oct 2004 08:34:19



I was confronted with the problem to add an absolute path prefix if the
given path does not begin with a slash. The solution should be ksh93 and
should not contain any if statement but only _a single substitution_.

I thought I share this sick looking one-liner with you...

fullpath="${path/#([!\/]*)/$PWD/\1}"

Did not find a simpler way to achieve that.

Janis

 
 
 

Add an absolute path prefix only to relative paths

Post by Norm Dresne » Sun, 17 Oct 2004 00:34:50



Quote:> I was confronted with the problem to add an absolute path prefix if the
> given path does not begin with a slash. The solution should be ksh93 and
> should not contain any if statement but only _a single substitution_.

> I thought I share this sick looking one-liner with you...

> fullpath="${path/#([!\/]*)/$PWD/\1}"

> Did not find a simpler way to achieve that.

This is obviously homework and since I won't get credit for doing it, I
won't do it.

 
 
 

Add an absolute path prefix only to relative paths

Post by Janis Papanagno » Sun, 17 Oct 2004 05:04:15




>>This is obviously homework and since I won't get credit for doing it, I
>>won't do it.

> It certainly looks like homework, but note that the OP gave an answer,
> just to share, and didn't really ask for any alternative answers.

Thank's Paul for pointing that out; my response would have been harsh.

I don't know what qualifies a posting as "looking like homework".

In this case the question came from a script professional (it's not from
me) who asked me for an, as I think, in ksh93 non-apparent solution.

The type of "problem" in question is quite common, and most solutions
use a straightforward clumsy conditional clause. The interesting point
here is, IMO, to use a simple substitution. Well, simple means in this
case that I nevertheless had to use a few quite new features of recent
ksh93 versions; the substitute from begin of pattern, and the grouping
of subexpressions with backreference (which was already well known from
other contexts than substitiutions), both present in this terse one-liner.
I thought it could be of interest in this group.

Since my solution is very cryptic and heavily using new features, I hoped
to get, from the readers who are more interested than Norm Dresner was,
a more conservative solution than mine, if one is existing, which I could
have overseen.

Janis

 
 
 

Add an absolute path prefix only to relative paths

Post by Paul Ja » Sun, 17 Oct 2004 08:57:27



> I don't know what qualifies a posting as "looking like homework".

The "don't use if" requirement looks rather artificial.  Using just an
expansion might be aesthetically preferable, but it wouldn't really be
*required* in real-world code.

Quote:> The type of "problem" in question is quite common, and most solutions
> use a straightforward clumsy conditional clause.

Straightforward == easy to read and maintain.  The expansion-only
method is clever, but cleverness is usually an obstacle to
maintainability, so personally, I'd prefer the conditional unless
there's some other significant advantage to the expansion.

paul

 
 
 

Add an absolute path prefix only to relative paths

Post by Janis Papanagno » Sun, 17 Oct 2004 09:37:41




>>I don't know what qualifies a posting as "looking like homework".

> The "don't use if" requirement looks rather artificial.

The other possibility is an "academical question" which applies here.
(I have fun solving such small puzzles.)

Quote:>>The type of "problem" in question is quite common, and most solutions
>>use a straightforward clumsy conditional clause.

> Straightforward == easy to read and maintain.  The expansion-only
> method is clever, but cleverness is usually an obstacle to
> maintainability, so personally, I'd prefer the conditional unless
> there's some other significant advantage to the expansion.

Agreed.  Indeed, that has also been my first response to the requestor
before I presented him the solution he wanted.

Janis

 
 
 

Add an absolute path prefix only to relative paths

Post by Norm Dresne » Mon, 18 Oct 2004 00:00:57





> >>This is obviously homework and since I won't get credit for doing it, I
> >>won't do it.

> > It certainly looks like homework, but note that the OP gave an answer,
> > just to share, and didn't really ask for any alternative answers.

> Thank's Paul for pointing that out; my response would have been harsh.

> I don't know what qualifies a posting as "looking like homework".

The requirement stated as
    "The solution should be ksh93 and should not contain any if statement
but only _a single substitution_."
sounds very much like the way a problem is phrased in a textbook or an
instructor's homework assignment.

A more general approach which said that a one-liner was desirable and gave
some justification for the required absence of conditionals which otherwise
sounds very artificial.

If it's not homework, I apologize, but I'm going to maintain that it
*sounded* very much like it.

    Norm

 
 
 

Add an absolute path prefix only to relative paths

Post by Kenny McCorma » Mon, 18 Oct 2004 02:38:09



...

Quote:>The requirement stated as
>    "The solution should be ksh93 and should not contain any if statement
>but only _a single substitution_."
>sounds very much like the way a problem is phrased in a textbook or an
>instructor's homework assignment.

>A more general approach which said that a one-liner was desirable and gave
>some justification for the required absence of conditionals which otherwise
>sounds very artificial.

My how times have changed.

There are (at least) 3 general categories of "problems" that are posted to
the net (i.e., newsgroups) in search of answers:

        1) "Hey guys (peers), here's an interesting problem.  Can you solve
this problem - subject to these assumptions/constraints?"  Note that the
communication here is peer-to-peer.  And this is as it should be.

        2) "Hey guys (gods/experts), I'm a working shmo in over my head.
If I don't solve this problem (which is almost assuredly asked 3 times
a week as well as being covered in all the FAQs) by 5 PM today, my boss
will fire me."  Please, please, have pity.  And note that I don't care
a fig about the elegance or beauty of the solution - I just have to get
this SOB off my back.

        3) "Hey guys (gods/experts), I'm a student (probably in some
god-forsaken 3rd world country) who doesn't have a clue how to do my
homework.  Please, please, have pity.  And note that I don't care a fig
about the elegance or beauty of the solution - I just want to pass this
class so I can get back to drinking and partying.

In the early days of the net, the problems were all type 1s.

It is a real shame that the 2s and 3s have all but squeezed the 1s out of
existence.  Speaking for myself, I shall continue to assume that all
problems posted here are in category #1, unless/until it is proven
conclusively otherwise.

 
 
 

Add an absolute path prefix only to relative paths

Post by Janis Papanagno » Mon, 18 Oct 2004 10:05:09







>>>>This is obviously homework and since I won't get credit for doing it, I
>>>>won't do it.

Norm, two points I want to tell you...
a) why did you make a null-content posting at all? <just rhetorical>!
b) given your past postings in c.u.s it is clear that you'll never get
credit, because you didn't contribute anything which is worth reading.

Moreover I suggest interested people to inspect all the 17(!) thread
hits when searching for your name with comp.unix.shell in Google; every
posting of you speaks for itself. Some examples...

http://tinyurl.com/4b22v  you fuzzy about your 20 year old program
http://tinyurl.com/3kboo  I helped you with a problem
http://tinyurl.com/6r467  yet another null-content reply
http://tinyurl.com/4ct8t  (no comment)
http://tinyurl.com/5qcsb  (no comment)
http://tinyurl.com/6bklo  you admit you don't know much about sh
http://tinyurl.com/59w7t  (just strange)

Quote:>>>It certainly looks like homework, but note that the OP gave an answer,
>>>just to share, and didn't really ask for any alternative answers.

>>Thank's Paul for pointing that out; my response would have been harsh.

[ it got harsh ]

Quote:> If it's not homework, I apologize, but I'm going to maintain that it
> *sounded* very much like it.

So what?  If you have nothing to contribute, no need to play the moron.

(Apologies to the group; my intention is to not continue this thread.)

 
 
 

Add an absolute path prefix only to relative paths

Post by Norm Dresne » Tue, 19 Oct 2004 00:35:33



There is a concept in my country called "free speech" and I intend to
exercise it whenever and wherever I choose to -- that's my constitutional
right.

You also have the absolute right not to read anything I post in this or any
other newsgroup.  That's your right too.

In the mean time, shut the * up.

 
 
 

Add an absolute path prefix only to relative paths

Post by j.. » Tue, 19 Oct 2004 02:04:25





> There is a concept in my country called "free speech" and I intend
> to exercise it whenever and wherever I choose to -- that's my
> constitutional right.

Correction, you don't have a constitutional right to free speech. The
first ammendment only limits what Congress can do, it doesn't
guarantee anyone anything.

Quote:> You also have the absolute right not to read anything I post in this
> or any other newsgroup.  That's your right too.

> In the mean time, shut the * up.

Kind of an odd way to end an assertion of free speech :-)

Joe
--
Nothing cures like time and love
  - Laura Nyro

 
 
 

Add an absolute path prefix only to relative paths

Post by Norm Dresne » Tue, 19 Oct 2004 06:31:56






> > There is a concept in my country called "free speech" and I intend
> > to exercise it whenever and wherever I choose to -- that's my
> > constitutional right.

> Correction, you don't have a constitutional right to free speech. The
> first ammendment only limits what Congress can do, it doesn't
> guarantee anyone anything.

> > You also have the absolute right not to read anything I post in this
> > or any other newsgroup.  That's your right too.

> > In the mean time, shut the * up.

> Kind of an odd way to end an assertion of free speech :-)

Applicable to anyone whose words explicitly deny me the right to my
constitutional right to free speech.  Yes, I know the usual rules about
yelling 'Fire' and such -- but in the current idiomatic parlance, I (and you
if you're here too) do have a constitutional right to free speech.  Not
every right is absolute -- which is what I think you were trying to imply --
but it's still a "right".

    Norm

 
 
 

Add an absolute path prefix only to relative paths

Post by j.. » Tue, 19 Oct 2004 06:43:10





> Applicable to anyone whose words explicitly deny me the right to my
> constitutional right to free speech.  Yes, I know the usual rules
> about yelling 'Fire' and such -- but in the current idiomatic
> parlance, I (and you if you're here too) do have a constitutional
> right to free speech.  Not every right is absolute -- which is what
> I think you were trying to imply -- but it's still a "right".

There isn't any such right granted in the first ammendement at all,
relative or absolute. Read it. The first ammendment simply specifies
some things that congress is not allowed to do.

Joe
--
Nothing cures like time and love
  - Laura Nyro

 
 
 

Add an absolute path prefix only to relative paths

Post by Norm Dresne » Tue, 19 Oct 2004 06:55:39






> > Applicable to anyone whose words explicitly deny me the right to my
> > constitutional right to free speech.  Yes, I know the usual rules
> > about yelling 'Fire' and such -- but in the current idiomatic
> > parlance, I (and you if you're here too) do have a constitutional
> > right to free speech.  Not every right is absolute -- which is what
> > I think you were trying to imply -- but it's still a "right".

> There isn't any such right granted in the first ammendement at all,
> relative or absolute. Read it. The first ammendment simply specifies
> some things that congress is not allowed to do.

I've read it -- many times.  We are, of course, arguing over semantics -- as
all arguments eventually come to.  If the constitution says that they can't
abridge my speech  then it is implicitly granting me a right to use my
speech any way I want (subject to the usual norms of society of course).
The absence of a restriction is the equivalent to a right.

    Norm

 
 
 

Add an absolute path prefix only to relative paths

Post by j.. » Tue, 19 Oct 2004 08:47:41


[...]

Quote:> If the constitution says that they can't abridge my speech
> is implicitly granting me a right to use my speech any way I want
> (subject to the usual norms of society of course).  The absence of a
> restriction is the equivalent to a right.

I'm not sure who you're thinking of when you say "they" here, but what
the first ammendment says is Congress. It doesn't say that states
can't pass such laws.

As you point out, "subject to the usual norms of society". Where does
the constitution define the usual norms of society? It doesn't. Which
is to say this isn't a constitutional right as you claimed. It's up to
us to vote for people in our states who will do what we want them to.

Joe
--
Nothing cures like time and love
  - Laura Nyro

 
 
 

Add an absolute path prefix only to relative paths

Post by Norm Dresne » Tue, 19 Oct 2004 20:54:08







> [...]

> > If the constitution says that they can't abridge my speech
> > is implicitly granting me a right to use my speech any way I want
> > (subject to the usual norms of society of course).  The absence of a
> > restriction is the equivalent to a right.

> I'm not sure who you're thinking of when you say "they" here, but what
> the first ammendment says is Congress. It doesn't say that states
> can't pass such laws.

> As you point out, "subject to the usual norms of society". Where does
> the constitution define the usual norms of society? It doesn't. Which
> is to say this isn't a constitutional right as you claimed. It's up to
> us to vote for people in our states who will do what we want them to.

The Supreme Court has long held that the First Amendment also applies to
states -- and most state constitutions have similar clauses anyway.

And if you've been reading any Supreme Court decisions in recent years,
you'd have seen that Societal Norms are very important in the definition of
individual (and states) rights.

The Constitution isn't the only "law of the land" -- there are other
mechanisms by which rights get promulgated as well.

    Norm

 
 
 

1. Relative path to Absolute path

I'm looking for a C algorithm to convert relative paths to an absolute
path.

For instance, I want to resolve "/usr/bin/X11/../ls" into "/usr/bin/ls."
But I would also like to generally clean up a path as well; eg.
"/usr/bin//ls" to "/usr/bin/ls," and so on.

Years ago I saw an algorithm to do this, posted here, but I have since
lost the darn thing.

Thanks for any ideas,
RH

2. ELJ looking for more authors

3. Relative path to Absolute Path

4. Linux (suse 7.3) and ATI Radeon 7500

5. Symbolic Links - Absolute Paths to Relative Paths

6. Trust Soundexpert 128

7. C: get absolute path to a file from relative path

8. Bug report: Xfree86 clobbers fonts

9. add in the PATH a path if not present in the PATH

10. ksh: add path to $PATH only when ot yet in $PATH

11. How to convert a relative path into an absolute one?

12. tarring absolute paths for relative untar

13. Make relative tar path absolute at extraction