Re: sh: getting <category>/<portname> from pwd

From: Nuno Teixeira <eduardo_at_freebsd.org>
Date: Wed, 26 Oct 2022 12:14:05 UTC
Hi Eugene,

> echo ${cwd##*/ports/}

Really nice without external tools.

sh: Parameter Expansion
---
${parameter#word}
             Remove Smallest Prefix Pattern.  The word is expanded to
produce
             a pattern.  The parameter expansion then results in parameter,
             with the smallest portion of the prefix matched by the pattern
             deleted.

${parameter##word}
             Remove Largest Prefix Pattern.  The word is expanded to
produce a
             pattern.  The parameter expansion then results in parameter,
with
             the largest portion of the prefix matched by the pattern
deleted.
---

Thanks!

Cheers

Eugene Grosbein <eugen@grosbein.net> escreveu no dia quarta, 26/10/2022
à(s) 05:32:

> 26.10.2022 11:31, Eugene Grosbein wrote:
>
> > 26.10.2022 5:14, Nuno Teixeira wrote:
> >> Hello all,
> >>
> >> I'm writing a shell script to feed poudriere testports and I need to
> get <category>/<portname> from ports tree:
> >>
> >> `pwd`:
> >> /home/user/work/freebsd/ports/<category>/<portname>
> >>
> >> `pwd | rev | cut -d / -f1,2 | rev` do the trick but it is so uggly!
> >>
> >> Is there a shell option to get it?
> >
> > Naturally, there is.
> >
> > cwd=$(pwd)
> > echo ${cd##*/ports/}
>
> echo ${cwd##*/ports/}
>
> > This is documented in sh(1) manual page, section "Parameter Expansion".
> > No need to call external tools.
>
>
>

-- 
Nuno Teixeira
FreeBSD Committer (ports)