Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Understanding Angle Brackets in Bash

#1
Understanding Angle Brackets in Bash

<div style="margin: 5px 5% 10px 5%;"><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/understanding-angle-brackets-in-bash.jpg" width="1600" height="788" title="" alt="" /></div><div><div><img src="http://www.sickgaming.net/blog/wp-content/uploads/2019/01/understanding-angle-brackets-in-bash.jpg" class="ff-og-image-inserted" /></div>
<p><a href="https://www.linux.com/blog/2019/1/bash-shell-utility-reaches-50-milestone">Bash</a> provides many important built-in commands, like <code>ls</code>, <code>cd</code>, and <code>mv</code>, as well as regular tools such as <code>grep</code>, <code>awk,</code> and <code>sed</code>. But, it is equally important to know the punctuation marks — <a href="https://www.linux.com/blog/learn/2019/1/linux-tools-meaning-dot">the glue in the shape of dots</a>, commas, brackets. and quotes — that allow you to transform and push data from one place to another. Take angle brackets (<code>&lt; &gt;</code>), for example.</p>
<h3>Pushing Around</h3>
<p>If you are familiar with other programming and scripting languages, you may have used <code>&lt;</code> and <code>&gt;</code> as logical operators to check in a condition whether one value is larger or smaller than another. If you have ever written HTML, you have used angle brackets to enclose tags.</p>
<p>In shell scripting, you can also use brackets to push data from place to place, for example, to a file:</p>
<pre>
ls &gt; dir_content.txt
</pre>
<p>In this example, instead of showing the contents of the directory on the command line, <code>&gt;</code> tells the shell to copy it into a file called <i>dir_content.txt</i>. If <i>dir_content.txt</i> doesn’t exist, Bash will create it for you, but if <i>dir_content.txt</i> already exists and is not empty, you will overwrite whatever it contained, so be careful!</p>
<p>You can avoid overwriting existing content by tacking the new stuff onto the end of the old stuff. For that you use <code>&gt;&gt;</code> (instead of <code>&gt;</code>):</p>
<pre>
ls $HOME &gt; dir_content.txt; wc -l dir_content.txt &gt;&gt; dir_content.txt
</pre>
<p>This line stores the list of contents of your home directory into <i>dir_content.txt</i>. You then count the number of lines in <i>dir_content.txt</i> (which gives you the number of items in the directory) with <a href="https://linux.die.net/man/1/wc"><code>wc -l</code></a> and you tack that value onto the end of the file.</p>
<p>After running the command line on my machine, this is what my <i>dir_content.txt</i> file looks like:</p>
<pre>
Applications bin cloud Desktop Documents Downloads Games ISOs lib logs Music OpenSCAD Pictures Public Templates test_dir Videos 17 dir_content.txt
</pre>
<p>The mnemonic here is to look at <code>&gt;</code> and <code>&gt;&gt;</code> as arrows. In fact, the arrows can point the other way, too. Say you have a file called <i>CBActors</i> containing some names of actors and the number of films by the Coen brothers they have been in. Something like this:</p>
<pre>
John Goodman 5
John Turturro 3
George Clooney 2
Frances McDormand 6
Steve Buscemi 5
Jon Polito 4
Tony Shalhoub 3
James Gandolfini 1
</pre>
<p>Something like</p>
<pre>
<b>sort &lt; CBActors</b> # Do this
Frances McDormand 6 # And you get this
George Clooney 2 James Gandolfini 1 John Goodman 5 John Turturro 3 Jon Polito 4 Steve Buscemi 5 Tony Shalhoub 3
</pre>
<p>Will <a href="https://linux.die.net/man/1/sort"><code>sort</code></a> the list alphabetically. But then again, you don’t need <code>&lt;</code> here since <code>sort</code> already expects a file anyway, so <code>sort CBActors</code> will work just as well.</p>
<p>However, if you need to see who is the Coens’ favorite actor, you can check with :</p>
<pre>
while read name surname films; do echo $films $name $surname &gt; filmsfirst.txt; done &lt; CBActors
</pre>
<p>Or, to make that a bit more readable:</p>
<pre>
while read name surname films;\ do echo $films $name $surname &gt;&gt; filmsfirst;\ done &lt; CBActors
</pre>
<p>Let’s break this down, shall we?</p>
<ul>
<li>the <a href="http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html"><code>while ...; do ... done</code></a> structure is a loop. The instructions between <code>do</code> and <code>done</code> are repeatedly executed while a condition is met, in this case…</li>
<li>… the <a href="https://linux.die.net/man/2/read"><code>read</code></a> instruction has lines to read. <code>read</code> reads from the standard input and will continue reading until there is nothing more to read…</li>
<li>… And as standard input is fed in via <code>&lt;</code> and comes from <i>CBActors</i>, that means the <code>while</code> loop will loop until the last line of <i>CBActors</i> is piped into the loop.</li>
<li>Getting back to <code>read</code> for a sec, the tool is clever enough to see that there are three distinct fields separated by spaces on each line of the file. That allows you to put the first field from each line in the <code>name</code> variable, the second in <code>surname</code> and the third in <code>films</code>. This comes in handy later, on the line that says <code>echo $films $name $surname &gt;&gt; filmsfirst;\</code>, allowing you to reorder the fields and push them into a file called <i>filmsfirst</i>.</li>
</ul>
<p>At the end of all that, you have a file called <i>filmsfirst</i> that looks like this:</p>
<pre>
5 John Goodman 3 John Turturro 2 George Clooney 6 Frances McDormand 5 Steve Buscemi 4 Jon Polito 3 Tony Shalhoub 1 James Gandolfini
</pre>
<p>which you can now use with <code>sort</code>:</p>
<pre>
sort -r filmsfirst
</pre>
<p>to see who is the Coens’ favorite actor. Yes, it is Frances McDormand. (The <a href="https://linux.die.net/man/1/sort"><code>-r</code></a> option reverses the sort, so McDormand ends up on top).</p>
<p>We’ll look at more angles on this topic next time!</p>
</div>
Reply



Forum Jump:


Users browsing this thread:
3 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016