[SFS] Re: bash - things you can't do with backticks

Jeffrey S. Haemer jeffrey.haemer@gmail.com
Mon, 24 Mar 2014 09:42:54 -0600


--001a11c20810f3d3e204f55c17f3
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Idioms like this, on the command line:

cd $(dirname $(which <program-of-your-choice))

or  this, in programs:

PATH+=3D:$(dirname $(readlink -f $0))

are pretty common for me.



For your problem, simpler would be this:

rm -rf $(ls -rt | head --lines=3D-3)

Except, of course, this gets directories, too (as does yours).
A better sol'n, thus, would be this:

rm $(find . -type f -maxdepth 1 | ls -rt | head --lines=3D-3)

HTH!


On Mon, Mar 24, 2014 at 9:24 AM, David L. Willson <DLWillson@thegeek.nu>wro=
te:

> I always use something like this "$( expression )" to get "the output of
> expression", rather than backticks like this "` expression `" and teach
> others to do likewise for two reasons: It's more readable and it's
> nestable. But, when teaching, I can never come up with a good example of
> nesting, so today, I happen to have one. I want to delete all the files i=
n
> this directory, except the newest three.
>
>
> rm $( ls -tr | head -n $(( $( ls -tr | wc -l ) - 3 )))
>
>
> It has some big bugs and I cannot lie, but it does demonstrate nesting.
>
> --
> David L. Willson
> Teacher, Engineer, Evangelist
> RHCE+Satellite CCAH Network+ A+ Linux+ LPIC-1 UbuntuCP NovellCLA
> Mobile 720-333-LANS(5267)
> http://sofree.us
>
> This is a good time for a r3VOLution.
>
>


--=20
Jeffrey Haemer <jeffrey.haemer@gmail.com>
720-837-8908 [cell], http://seejeffrun.blogspot.com [blog],
http://www.youtube.com/user/goyishekop [vlog]
*=D7=A4=D7=A8=D7=99=D7=99=D7=94=D7=99=D7=99=D7=98? =D7=93=D7=90=D7=A1 =D7=
=90=D7=99=D7=96 =D7=99=D7=90=D6=B7=D7=A0=D7=92 =D7=93=D7=99=D7=A0=D7=A2=D7=
=9F =D7=95=D7=95=D7=90=D6=B8=D7=A8=D7=98.*

--001a11c20810f3d3e204f55c17f3
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><div><font face=3D"arial, helvetica, sans-serif">Idioms li=
ke this, on the command line:</font></div><font face=3D"courier new, monosp=
ace"><div><font face=3D"courier new, monospace"><br></font></div>cd $(dirna=
me $(which &lt;program-of-your-choice))</font><div>

<br></div><div>or =C2=A0this, in programs:</div><div><br></div><div><font f=
ace=3D"courier new, monospace">PATH+=3D:$(dirname $(readlink -f $0))</font>=
</div><div><br></div><div>are pretty common for me.</div><div><br></div><di=
v><br>

</div><div><br></div><div>For your problem, simpler would be this:</div><di=
v><br></div><div><font face=3D"courier new, monospace">rm -rf $(ls -rt | he=
ad --lines=3D-3)</font></div><div><br></div><div>Except, of course, this ge=
ts directories, too (as does yours).</div>

<div>A better sol&#39;n, thus, would be this:</div><div><br></div><div><fon=
t face=3D"courier new, monospace">rm $(find . -type f -maxdepth 1 | ls -rt =
| head --lines=3D-3)</font></div><div><br></div><div>HTH!</div></div><div c=
lass=3D"gmail_extra">

<br><br><div class=3D"gmail_quote">On Mon, Mar 24, 2014 at 9:24 AM, David L=
. Willson <span dir=3D"ltr">&lt;<a href=3D"mailto:DLWillson@thegeek.nu" tar=
get=3D"_blank">DLWillson@thegeek.nu</a>&gt;</span> wrote:<br><blockquote cl=
ass=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;p=
adding-left:1ex">

<div><div style=3D"font-size:12pt;font-family:Times New Roman">I always use=
 something like this &quot;$( expression )&quot; to get &quot;the output of=
 expression&quot;, rather than backticks like this &quot;` expression `&quo=
t;<span> and teach others to do likewise for two reasons: It&#39;s more rea=
dable and it&#39;s nestable. But, when teaching, I can never come up with a=
 good example of nesting, so today, I happen to have one. I want to delete =
all the files in this directory, except the newest three.<br>

<br><br>rm $( ls -tr | head -n $(( $( ls -tr | wc -l ) - 3 )))<br><br></spa=
n><br><span><span>It has some big bugs and I cannot lie, but it does demons=
trate nesting.<br><br></span><span name=3D"x"></span>--<br>David L. Willson=
<br>

Teacher, Engineer, Evangelist<br>RHCE+Satellite CCAH Network+ A+ Linux+ LPI=
C-1 UbuntuCP NovellCLA<br>Mobile 720-333-LANS(5267)<br><a href=3D"http://so=
free.us" target=3D"_blank">http://sofree.us</a><br><br>This is a good time =
for a r3VOLution.<span name=3D"x"></span><br>

</span><br></div></div></blockquote></div><br><br clear=3D"all"><div><br></=
div>-- <br><div dir=3D"ltr">Jeffrey Haemer &lt;<a href=3D"mailto:jeffrey.ha=
emer@gmail.com" target=3D"_blank">jeffrey.haemer@gmail.com</a>&gt;<br>720-8=
37-8908 [cell],=C2=A0<a href=3D"http://seejeffrun.blogspot.com" target=3D"_=
blank">http://seejeffrun.blogspot.com</a> [blog],<div>

<a href=3D"http://www.youtube.com/user/goyishekop" target=3D"_blank">http:/=
/www.youtube.com/user/goyishekop</a> [vlog]</div><div><div style=3D"margin-=
top:15px;margin-right:0px;margin-bottom:5px;margin-left:5px"><div dir=3D"rt=
l" style=3D"font-family:arial,sans-serif;font-size:13px;zoom:1">

<span style=3D"font-family:garamond,serif;line-height:17px;font-size:small"=
><i><span style=3D"border-collapse:collapse;font-family:arial,sans-serif;fo=
nt-size:14px;line-height:18px;font-style:normal"><span title=3D""><font fac=
e=3D"&#39;times new roman&#39;, serif"><span style=3D"font-size:small">=D7=
=A4=D7=A8=D7=99=D7=99=D7=94=D7=99=D7=99=D7=98?=C2=A0</span></font></span><s=
pan title=3D""><font face=3D"&#39;times new roman&#39;, serif"><span style=
=3D"font-size:small">=D7=93=D7=90=D7=A1 =D7=90=D7=99=D7=96 =D7=99=D7=90=D6=
=B7=D7=A0=D7=92 =D7=93=D7=99=D7=A0=D7=A2=D7=9F =D7=95=D7=95=D7=90=D6=B8=D7=
=A8=D7=98.</span></font></span></span></i></span></div>

</div></div></div>
</div>

--001a11c20810f3d3e204f55c17f3--