64ecc4e6c3
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@172 a672b425-5310-4d7a-af5c-997e18724b81
81 lines
1.8 KiB
HTML
81 lines
1.8 KiB
HTML
---
|
|
layout: page
|
|
category-page: scripts
|
|
category-title: Scripting
|
|
tags: redirect output input
|
|
author: Dario Rasic
|
|
title: Redirection
|
|
---
|
|
<p>
|
|
|
|
<h3>output as input</h3>
|
|
<br>
|
|
To redirect a certain output of the command-line we have to use the symbol ">".<br>
|
|
|
|
In this case, we will use a file named <i>hello.txt</i>, in which we want to insert
|
|
a certain output ("Sun" in this case):<br>
|
|
|
|
<pre>
|
|
echo "Sun" > hello.txt
|
|
</pre>
|
|
|
|
So if we print the content of the file in our command-line, the output will be the following:
|
|
|
|
<pre>
|
|
cat hello.txt
|
|
Sun
|
|
</pre>
|
|
|
|
If we want to append a certain output to an existing file,
|
|
we just have to use ">>" symbols:
|
|
|
|
<pre>
|
|
cat hello.txt
|
|
Sun
|
|
echo "Moon" >> hello.txt
|
|
cat hello.txt
|
|
Sun
|
|
Moon
|
|
</pre>
|
|
|
|
<!-- Input redirection text -->
|
|
<h3>input as output</h3>
|
|
<br>
|
|
To redirect an input from a file for a command, the symbol "<" is used.
|
|
|
|
<pre>
|
|
echo < $(cat hello.txt)
|
|
Sun
|
|
Moon
|
|
</pre>
|
|
<p>This is particularly useful when chaining commands.</p>
|
|
<br>
|
|
<!-- Chaining command text -->
|
|
<h3>chaining (or piping)</h3>
|
|
<br>
|
|
Chaining, also called Piping because it works with the pipe symbol "|", takes the output of a certain command-line and feeds it to another command in a direct way.
|
|
<!-- Table for the example of the piping command -->
|
|
<pre>
|
|
cat hello.txt | grep Mo
|
|
Moon
|
|
</pre>
|
|
|
|
<!-- A simple example -->
|
|
<h3>simple example</h3>
|
|
<br>
|
|
Now let's say that we want to combine those commands in a more complex operation. Let's say we want to take some contents from a certain file and put it into another file.<br>
|
|
|
|
We want to sort the animals whose name begins with letter "d" from the file <i>animals.txt</i> and put it into <i>d_animals.txt</i>.<br>
|
|
|
|
<pre>
|
|
grep d < animals.txt > d_animals.txt
|
|
<br>
|
|
cat d_animals.txt
|
|
Deer
|
|
Dog
|
|
Dolphin
|
|
...
|
|
</pre>
|
|
|
|
|
|
</p>
|