[SFS] bash - things you can't do with backticks
David L. Willson
DLWillson@TheGeek.NU
Mon, 24 Mar 2014 09:24:08 -0600 (MDT)
--=_95b987d7-1a22-42ce-931c-667cb561cb0a
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
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 in 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.
--=_95b987d7-1a22-42ce-931c-667cb561cb0a
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable
<html><head><style type=3D'text/css'>p { margin: 0; }</style></head><body><=
div style=3D'font-family: Times New Roman; font-size: 12pt; color: #000000'=
>I always use something like this "$( expression )" to get "the output of e=
xpression", rather than backticks like this "` expression `"<span> and teac=
h others to do likewise for two reasons: It's more readable and it's nestab=
le. 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 dir=
ectory, except the newest three.<br><br><br>rm $( ls -tr | head -n $(( $( l=
s -tr | wc -l ) - 3 )))<br><br></span><br><span id=3D"1db0e1d5-cb88-49a4-8e=
2f-e90f3b6109a8"><span id=3D"1db0e1d5-cb88-49a4-8e2f-e90f3b6109a8">It has s=
ome big bugs and I cannot lie, but it does demonstrate nesting.<br><br></sp=
an><span name=3D"x"></span>--<br>David L. Willson<br>Teacher, Engineer, Eva=
ngelist<br>RHCE+Satellite CCAH Network+ A+ Linux+ LPIC-1 UbuntuCP NovellCLA=
<br>Mobile 720-333-LANS(5267)<br>http://sofree.us<br><br>This is a good tim=
e for a r3VOLution.<span name=3D"x"></span><br></span><br></div></body></ht=
ml>
--=_95b987d7-1a22-42ce-931c-667cb561cb0a--