Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Fedora - Command line quick tips: Using pipes to connect tools

#1
Command line quick tips: Using pipes to connect tools

<div><p>One of the most powerful concepts of Linux is carried on from its predecessor, UNIX. Your Fedora system has a bunch of useful, single-purpose utilities available for all sorts of simple operations. Like building blocks, you can attach them in creative and complex ways. <em>Pipes</em> are key to this concept.</p>
<p> <span id="more-22818"></span> </p>
<p>Before you hear about pipes, though, it’s helpful to know the basic concept of input and output. Many utilities in your Fedora system can operate against files. But they can often take input not stored on a disk. You can think of input flowing freely into a process such as a utility as its <em>standard input</em> (also sometimes called <em>stdin</em>).</p>
<p>Similarly, a tool or process can display information to the screen by default. This is often because its default output is connected to the terminal. You can think of the free-flowing output of a process as its <em>standard output</em> (or <em>stdout</em> — go figure!).</p>
<h2>Examples of standard input and output</h2>
<p>Often when you run a tool, it outputs to the terminal. Take for instance this simple sequence command using the <em>seq</em> tool:</p>
<pre class="wp-block-preformatted">$ <strong>seq 1 6</strong>
1
2
3
4
5
6</pre>
<p>The output, which is simply to count integers up from 1 to 6, one number per line, comes to the screen. But you could also send it to a file using the <strong>&gt;</strong> character. The shell interpreter uses this character to mean “redirect <em>standard output</em> to a file whose name follows.” So as you can guess, this command puts the output into a file called <em>six.txt:</em></p>
<pre class="wp-block-preformatted">$ seq 1 6 &gt; six.txt</pre>
<p>Notice nothing comes to the screen. You’ve sent the ouptut into a file instead. If you run the command <em>cat six.txt</em> you can verify that.</p>
<p>You probably remember the simple use of the <em>grep</em> command <a href="https://fedoramagazine.org/command-line-quick-tips-searching-with-grep/">from a previous article</a>. You could ask <em>grep</em> to search for a pattern in a file by simply declaring the file name. But that’s simply a convenience feature in <em>grep</em>. Technically it’s built to take <em>standard input</em>, and search that.</p>
<p>The shell uses the <strong>&lt;</strong> character similarly to mean “redirect <em>standard input</em> from a file whose name follows.” So you could just as well search for the number <strong>4</strong> in the file <em>six.txt</em> this way:</p>
<pre class="wp-block-preformatted">$ <strong>grep 4 &lt; six.txt</strong>
4</pre>
<p>Of course the output here is, by default, the content of any line with a match. So <em>grep</em> finds the digit <strong>4</strong> in the file and outputs that line to <em>standard output</em>.</p>
<h2>Introducing pipes</h2>
<p>Now imagine: what if you took the standard output of one tool, and instead of sending it to the terminal, you sent it into another tool’s standard input? This is the essence of the pipe.</p>
<p>Your shell uses the vertical bar character <strong>|</strong> to represent a pipe between two commands. You can find it on most keyboard above the backslash <strong>\</strong> character. It’s used like this:</p>
<pre class="wp-block-preformatted">$ command1 | command2</pre>
<p>For most simple utilities, you wouldn’t use an output filename option on <em>command1</em>, nor an input file option on <em>command2</em>. (You might use other options, though.) Instead of using files, you’re sending the output of <em>command1</em> directly into <em>command2</em>. You can use as many pipes in a row as needed, creating complex pipelines of several commands in a row.</p>
<p>This (relatively useless) example combines the commands above:</p>
<pre class="wp-block-preformatted">$ <strong>seq 1 6 | grep 4</strong>
4</pre>
<p>What happened here? The <em>seq</em> command outputs the integers 1 through 6, one line at a time. The <em>grep</em> command processes that output line by line, searching for a match on the digit <strong>4</strong>, and outputs any matching line.</p>
<p>Here’s a slightly more useful example. Let’s say you want to find out if TCP port 22, the <em>ssh</em> port, is open on your system. You could find this out using the <em>ss</em> command* by looking through its copious output. Or you could figure out its filter language and use that. Or you could use pipes. For example, pipe it through <em>grep</em> looking for the <em>ssh</em> port label:</p>
<pre class="wp-block-preformatted">$ <strong>ss -tl | grep ssh</strong>
LISTEN 0 128 0.0.0.0Confusedsh 0.0.0.0:* LISTEN 0 128 [::]Confusedsh [::]:*</pre>
<p><small><em>* Those readers familiar with the venerable </em>netstat<em> command may note it is mostly obsolete, as stated in its <a href="https://linux.die.net/man/8/netstat">man page</a>.</em></small></p>
<p>That’s a lot easier than reading through many lines of output. And of course, you can combine redirectors and pipes, for instance:</p>
<pre class="wp-block-preformatted">$ ss -tl | grep ssh &gt; ssh-listening.txt</pre>
<p>This is barely scratching the surface of pipes. Let your imagination run wild. Have fun piping!</p>
<hr class="wp-block-separator" />
</div>


https://www.sickgaming.net/blog/2019/08/...ect-tools/
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016