Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 26 Oct 2022 13:14:05 +0100
From:      Nuno Teixeira <eduardo@freebsd.org>
To:        Eugene Grosbein <eugen@grosbein.net>
Cc:        FreeBSD Mailing List <freebsd-ports@freebsd.org>
Subject:   Re: sh: getting <category>/<portname> from pwd
Message-ID:  <CAFDf7ULzh-kw8CLU0Pv5pHUTpxsncwizaxqapp4QZJatkkAGUA@mail.gmail.com>
In-Reply-To: <69442eed-b2e1-5be3-39f1-aef413606fcb@grosbein.net>
References:  <CAFDf7UJp9PkrhfMmJRKO7QNTmGQYu7BQYyqczoz-Dy7hP3h74g@mail.gmail.com> <d06cd71d-bb8b-192b-d927-0314ae544a11@grosbein.net> <69442eed-b2e1-5be3-39f1-aef413606fcb@grosbein.net>

next in thread | previous in thread | raw e-mail | index | archive | help
--000000000000b579e605ebeef6f7
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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
=C3=A0(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=3D$(pwd)
> > echo ${cd##*/ports/}
>
> echo ${cwd##*/ports/}
>
> > This is documented in sh(1) manual page, section "Parameter Expansion".
> > No need to call external tools.
>
>
>

--=20
Nuno Teixeira
FreeBSD Committer (ports)

--000000000000b579e605ebeef6f7
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div dir=3D"ltr"><div>Hi Eugene,</div><div><br></div><div>=
&gt; echo ${cwd##*/ports/}</div><div><br></div><div>Really nice without ext=
ernal tools.</div><div><br></div><div>sh: Parameter Expansion</div><div>---=
</div><div>${parameter#word}<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0Remove Smallest Prefix Pattern.=C2=A0 The word is expanded to produce=
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0a pattern.=C2=A0 The pa=
rameter expansion then results in parameter,<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0=
 =C2=A0 =C2=A0 =C2=A0with the smallest portion of the prefix matched by the=
 pattern<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0deleted.<br><br=
>${parameter##word}<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Remo=
ve Largest Prefix Pattern.=C2=A0 The word is expanded to produce a<br>=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0pattern.=C2=A0 The parameter e=
xpansion then results in parameter, with<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=
=A0 =C2=A0 =C2=A0the largest portion of the prefix matched by the pattern d=
eleted.</div><div>---<br></div><div><br></div><div>Thanks!</div><div><br></=
div><div>Cheers<br></div></div><br><div class=3D"gmail_quote"><div dir=3D"l=
tr" class=3D"gmail_attr">Eugene Grosbein &lt;<a href=3D"mailto:eugen@grosbe=
in.net">eugen@grosbein.net</a>&gt; escreveu no dia quarta, 26/10/2022 =C3=
=A0(s) 05:32:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0p=
x 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">26=
.10.2022 11:31, Eugene Grosbein wrote:<br>
<br>
&gt; 26.10.2022 5:14, Nuno Teixeira wrote:<br>
&gt;&gt; Hello all,<br>
&gt;&gt;<br>
&gt;&gt; I&#39;m writing a shell script to feed poudriere testports and I n=
eed to get &lt;category&gt;/&lt;portname&gt; from ports tree:<br>
&gt;&gt;<br>
&gt;&gt; `pwd`:<br>
&gt;&gt; /home/user/work/freebsd/ports/&lt;category&gt;/&lt;portname&gt;<br=
>
&gt;&gt;<br>
&gt;&gt; `pwd | rev | cut -d / -f1,2 | rev` do the trick but it is so uggly=
!<br>
&gt;&gt;<br>
&gt;&gt; Is there a shell option to get it?<br>
&gt; <br>
&gt; Naturally, there is.<br>
&gt; <br>
&gt; cwd=3D$(pwd)<br>
&gt; echo ${cd##*/ports/}<br>
<br>
echo ${cwd##*/ports/}<br>
<br>
&gt; This is documented in sh(1) manual page, section &quot;Parameter Expan=
sion&quot;.<br>
&gt; No need to call external tools.<br>
<br>
<br>
</blockquote></div><br clear=3D"all"><br>-- <br><div dir=3D"ltr" class=3D"g=
mail_signature"><div dir=3D"ltr"><span style=3D"color:rgb(102,102,102)">Nun=
o Teixeira<br>FreeBSD Committer (ports)</span></div></div></div>

--000000000000b579e605ebeef6f7--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAFDf7ULzh-kw8CLU0Pv5pHUTpxsncwizaxqapp4QZJatkkAGUA>