Best web hosting site - 49Chapter 2Running Commands from the ShellPiping CommandsThe pipe

49Chapter 2Running Commands from the ShellPiping CommandsThe pipe (|) metacharacter connects the output from one command to the input ofanother command. This lets you have one command work on some data and thenhave the next command deal with the results. Here is an example of a command linethat includes pipes: $ cat /etc/password | sort | lessThis command lists the contents of the /etc/passwordfile and pipes the output tothe sortcommand. The sortcommand takes the usernames that begin each lineof the /etc/passwordfile, sorts them alphabetically, and pipes the output to thelesscommand (to page through the output). Pipes are an excellent illustration of how UNIX, the predecessor of Linux, was createdas an operating system made up of building blocks. A standard practice in UNIXwas to connect utilities in different ways to get different jobs done. For example, before the days of graphical word processors, users created plain-text files thatincluded macros to indicate formatting. To see how the document really appeared, they would use a command such as the following: $ gunzip < /usr/share/man/man1/grep.1.gz | nroff -c -man | lessIn this example, the contents of the grepman page (grep.1.gz) are directed to thegunzipcommand to be unzipped. The output from gunzipis piped to the nroffcommand to format the man page using the manual macro (-man). The output ispiped to the lesscommand to display the output. Because the file being displayedis in plain text, you could have substituted any number of options to work with thetext before displaying it. You could sort the contents, change or delete some of thecontent, or bring in text from other documents. The key is that, instead of all thosefeatures being in one program, you get results from piping and redirecting input andoutput between multiple commands. Sequential CommandsSometimes you may want a sequence of commands to run, with one command completing before the next command begins. You can do this by typing severalcommands on the same command line and separating them with semicolons (;): $ date ; troff -me verylargedocument | lpr ; dateIn this example, I was formatting a huge document and wanted to know how longitwould take. The first command (date) showed the date and time before the for- matting started. The troffcommand formatted the document and then piped theoutput to the printer. When the formatting was done, the date and time was printedagain (so I knew how long the troffcommand took to complete).
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services

Leave a Reply