2018-11-11 12:38:52 +00:00
|
|
|
---
|
|
|
|
layout: page
|
|
|
|
author: Domenico Votta
|
2018-11-11 16:47:25 +00:00
|
|
|
category-page: advanced
|
|
|
|
category-title: Advanced commands
|
|
|
|
tags: pipes redirect output input
|
2018-11-16 12:47:48 +00:00
|
|
|
title: Pipes
|
2018-11-11 12:38:52 +00:00
|
|
|
---
|
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
The <code>pipes</code> that in the shell are representend with the symbol | ,
|
|
|
|
they are used to join two commands on the terminal, taking the output of the first
|
|
|
|
command and using it as input of the second.<br>
|
|
|
|
It is usually common to see the command <code>grep</code> and <code>ps</code> together,
|
|
|
|
an example below.<br>
|
2018-11-11 12:38:52 +00:00
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
Example: lists all processes with your username
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:38:52 +00:00
|
|
|
ps aux | grep user
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-11 12:38:52 +00:00
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
Example: count the lines of a file, using the
|
|
|
|
<a href="{{ site.baseurl }}/pages/cmd/advanced/nl.html">nl</a> command
|
2018-11-11 16:47:25 +00:00
|
|
|
<pre>
|
2018-11-11 12:38:52 +00:00
|
|
|
cat example1.txt | nl
|
2018-11-14 08:56:17 +00:00
|
|
|
1 Car
|
|
|
|
2 Computer
|
|
|
|
3 Robot
|
|
|
|
4 Smartphone
|
|
|
|
5 Videogame
|
2018-11-11 16:47:25 +00:00
|
|
|
</pre>
|
2018-11-11 12:38:52 +00:00
|
|
|
|
2018-11-14 08:56:17 +00:00
|
|
|
Example: passing expressions to <a href="{{ site.baseurl }}/pages/cmd/advanced/bc.html">bc</a>
|
|
|
|
to do calculations
|
|
|
|
<pre>
|
|
|
|
echo "(12 / 2 - 3) * 3 + 1.5" | bc
|
|
|
|
10.5
|
|
|
|
</pre>
|
|
|
|
|
|
|
|
You can use the commands that you learned and put them together, always
|
|
|
|
respecting the logic of the command.
|