Enable Pipelines
Linux Commands - Shells

The shell will allow users to set up pipes since it knows how to use them.   The pipeline which is symbolized by "|", takes the output of one command as the input to a second command.  It is an easy way to accomplish one goal using two or more steps.


Lesson 6 / Lesson 8


Here is an example:

You are collecting data in your logs and you have captured a number of IP Addresses that are important to you.  You can use echo to send them to a line.

     echo "192.168.7.26 192.168.3.34 192.168.4.2"
192.168.7.26 192.168.3.34 192.168.4.2

However to be more useful you need them all on separate lines so that you can create a script that reads one line at a time.  As a result you use a "|" to send the output of the first command to the second command.

    echo "192.168.7.26 192.168.3.34 192.168.4.2" | tr " " "\n"
192.168.7.26
192.168.3.34
192.168.4.2

But now you would like to create an ordered list that is from smallest to largest IP Address.  Again pipe to another command.

    echo "192.168.7.26 192.168.3.34 192.168.4.2" | tr " " "\n" | sort
192.168.7.26
192.168.3.34
192.168.4.2

If you need to display output that scrolls off of the top, you can't scroll back to see what you missed.  So what do you do if you want to see all of the output?  Easy, just pipe the output into the "less" utility.

For example, if you want to see a detailed listing of all of the files in your current directory, just enter:

 ls -la | less


Now, you'll be able to use the "Page Up" and "Page Down" keys to see all of the output.  You'll also be able to use the less utility's search function.  Just hit the / key, followed by the string of text that you're searching for.  If you want to search for lines that begin with a certain text string, enter /^ followed by the string of text.  If you want to search for lines with a certain string at the end of a line, enter / followed by the string of text with a $ at the end.

/^mytext

/mytext$

There is one slight trade-off, though.  By looking at the ls output in unadorned bash, you'll have a nice color-coded display, with a different color for each type of file.  When you pipe the output through less, you'll lose the color-coding.

Note:  Same Linux distros aren't set up for color-coded bash display.

The ls utility isn't the only thing that you can do this trick with.  You can use it with just about any utility that produces a screen output.

How you open your terminal emulator will again depend on which distro of Linux that you're working with.  In most cases, you'll open it from a selection on your application menu.  Different distros will have it in different sub-menus.  Usually, you'll look for it either under the "System Tools" sub-menu, or the "Accessories" sub-menu.

With bash, you'll have a fairly comprehensive set of commands at your disposal.  Some are internal commands, which are built into the bash executable.  Others are external commands, which are separate executables unto themselves.  

 


Copyright CyberMontana Inc. and BeginLinux.com
All rights reserved. Cannot be reproduced without written permission. Box 1262 Trout Creek, MT 59874