code review for everyone
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@189 a672b425-5310-4d7a-af5c-997e18724b81
This commit is contained in:
parent
70487a851a
commit
c10fb333d6
27 changed files with 818 additions and 854 deletions
site/pages
cmd
advanced
basic
interm
fs
scripts
|
@ -6,163 +6,178 @@ category-title: Advanced commands
|
|||
tags: difference lines diff file separating
|
||||
title: diff
|
||||
---
|
||||
<p>
|
||||
The command <code>diff</code> analyzes two files and prints the lines that are different.
|
||||
Essentially, it outputs a set of instructions for how to change one file to
|
||||
make it identical to the second file.<br>
|
||||
|
||||
<h3>How to use</h3>
|
||||
|
||||
To activate the diff command, we must take as parameters two or more
|
||||
files that we want to compare and it will give us the output of
|
||||
different lines.<br>
|
||||
For example let's say we have two files, file1.txt and file2.txt.<br>
|
||||
If <b>file1.txt</b> contains the following four lines of text:
|
||||
|
||||
{% highlight bash %}
|
||||
1. I need to buy apples.
|
||||
2. I need to run around the park.
|
||||
3. I need to clean the dog.
|
||||
4. I need to get the car detailed.
|
||||
{% endhighlight %}
|
||||
|
||||
and <b>file2.txt</b> contains these four lines:
|
||||
|
||||
<p> diff analyzes two files and prints the lines that are different.
|
||||
Essentially, it outputs a set of instructions for how to change one file to
|
||||
make it identical to the second file.</p>
|
||||
{% highlight bash %}
|
||||
1. I need to do the laundry.
|
||||
2. I need to clean the dog.
|
||||
4. I need to get the dog detailed.
|
||||
{% endhighlight %}
|
||||
|
||||
then we can use diff to automatically display for us which lines differ
|
||||
between the two files with this command:
|
||||
|
||||
<pre>
|
||||
diff file1.txt file2.txt
|
||||
</pre>
|
||||
|
||||
and the output will be:
|
||||
|
||||
<pre>
|
||||
2,4c2,4
|
||||
|
||||
< I need to run the laundry.
|
||||
< I need to wash the dog.
|
||||
< I need to get the car detailed.
|
||||
---
|
||||
> I need to do the laundry.
|
||||
> I need to wash the car.
|
||||
> I need to get the dog detailed.
|
||||
</pre>
|
||||
|
||||
In our output, "2,4c2,4" means: "Lines 2 through 4 in the first
|
||||
file need to be changed to match lines 2 through 4 in the second file.
|
||||
|
||||
<h3>How to use</h3>
|
||||
|
||||
<p> To activate the diff command, we must take as parameters two or more
|
||||
files that we want to compare and it will give us the output of
|
||||
different lines. <br>
|
||||
For example let's say we have two files, file1.txt and file2.txt.<br>
|
||||
<br> If <b>file1.txt</b> contains the following four lines of text: </p>
|
||||
|
||||
<ol><code><li>I need to buy apples.</li>
|
||||
<li>I need to run around the park.</li>
|
||||
<li>I need to wash the dog.</li>
|
||||
<li>I need to get the car detailed.</li></code></ol>
|
||||
|
||||
<p> ...and <b>file2.txt</b> contains these four lines: </p>
|
||||
|
||||
<ol><code><li> I need to buy apples.</li>
|
||||
<li>I need to do the laundry.</li>
|
||||
<li>I need to wash the dog.</li>
|
||||
<li>I need to get the dog detailed.</li></ol></code>
|
||||
|
||||
<p>...then we can use diff to automatically display for us which lines differ
|
||||
between the two files with this command:</p>
|
||||
|
||||
<pre>diff file1.txt file2.txt</pre>
|
||||
|
||||
<p> ...and the output will be:</p>
|
||||
|
||||
<pre> 2,4c2,4 <br>
|
||||
< I need to run the laundry.<br>
|
||||
< I need to wash the dog.<br>
|
||||
< I need to get the car detailed.<br>
|
||||
---<br>
|
||||
> I need to do the laundry.<br>
|
||||
> I need to wash the car.<br>
|
||||
> I need to get the dog detailed.</pre>
|
||||
|
||||
<p> In our output above, "2,4c2,4" means: "Lines 2 through 4 in the first
|
||||
file need to be changed to match lines 2 through 4 in the second file.</p>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<h3>Flags</h3>
|
||||
<ul>
|
||||
<li><h4> -c:</h4> <br>To view differences in context mode, use the -c option.
|
||||
For instance, let's say <b>file1.txt</b> and <b>file2.txt</b> contain
|
||||
the following:<br><br>
|
||||
<h3>Context mode</h3>
|
||||
To view differences in context mode, use the -c option.
|
||||
For instance, let's say <i>file1.txt</i> and <i>file2.txt</i> contain
|
||||
the following:<br>
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th> File1.txt </th> <th>File2.txt</th></tr>
|
||||
<tr><td> apples </td> <td>apples</td></tr>
|
||||
<tr><td>oranges</td> <td>kiwis</td></tr>
|
||||
<tr><td>kiwis</td> <td>carrots</td></tr>
|
||||
<tr><td>carrots</td> <td>grapefruits</td></tr></table>
|
||||
<tr>
|
||||
<th>File1.txt</th>
|
||||
<th>File2.txt</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>apples</td>
|
||||
<td>apples</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>oranges</td>
|
||||
<td>kiwis</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>kiwis</td>
|
||||
<td>carrots</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>carrots</td>
|
||||
<td>grapefruits</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
<pre>
|
||||
diff -c file1.txt file2.txt
|
||||
</pre>
|
||||
|
||||
Our output will be like the following:
|
||||
|
||||
<pre>
|
||||
*** file1.txt 2014-08-21 17:58:29.764656635 -0400
|
||||
--- file2.txt 2014-08-21 17:58:50.768989841 -0400
|
||||
***************
|
||||
*** 1,4 ****
|
||||
apples
|
||||
- oranges
|
||||
kiwis
|
||||
carrots
|
||||
--- 1,4 ----
|
||||
apples
|
||||
kiwis
|
||||
carrots
|
||||
+ grapefruits
|
||||
</pre>
|
||||
|
||||
The first two lines of this output show us information about our "from"
|
||||
file (file 1) and our "to" file (file 2). It lists the file name,
|
||||
modification date, and modification time of each of our files, one per
|
||||
line. The "from" file is indicated by "***", and the "to" file is
|
||||
indicated by "---"..<br>
|
||||
|
||||
The line "***************" is just a separator.<br>
|
||||
The next line has three asterisks ("***") followed by a line
|
||||
range from the first file (in this case lines 1 through 4, separated by
|
||||
a comma). Then four asterisks ("****").<br>
|
||||
Then it shows us the contents of those lines. If the line is unchanged,
|
||||
it's prefixed by two spaces. If the line is changed, however, it's prefixed
|
||||
by an indicative character and a space. The character meanings are as
|
||||
follows:
|
||||
|
||||
<table>
|
||||
<tr>
|
||||
<th>Character</th>
|
||||
<th>Meaning</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><b>!</b></td>
|
||||
<td>
|
||||
Indicates that this line is part of a group of one or more lines that
|
||||
needs to change. There is a corresponding group of lines prefixed with
|
||||
"!" in the other file's context as well.
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><b>+</b></td>
|
||||
<td>Indicates a line in the second file that needs to be added to the first file.</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><b>-</b></td>
|
||||
<td>Indicates a line in the first file that needs to be deleted.</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
After the lines from the first file, there are three dashes ("---"),
|
||||
then a line range, then four dashes ("----"). This indicates the
|
||||
line range in the second file that will sync up with our changes
|
||||
in the first file.
|
||||
|
||||
If there is more than one section that needs to change, diff will
|
||||
show these sections one after the other. Lines from the first file will
|
||||
still be indicated with "***", and lines from the second file
|
||||
with "---".
|
||||
|
||||
<pre> diff -c file1.txt file2.txt </pre>
|
||||
<h3>Unified mode</h3>
|
||||
Unified mode (the -u option) is similar to context mode,
|
||||
but it doesn't display any redundant information. Here's an example,
|
||||
using the same input files as our last example:
|
||||
|
||||
Our output will be like the following:
|
||||
<pre> *** file1.txt 2014-08-21 17:58:29.764656635 -0400<br>
|
||||
--- file2.txt 2014-08-21 17:58:50.768989841 -0400<br>
|
||||
***************<br>
|
||||
*** 1,4 ****<br>
|
||||
apples<br>
|
||||
- oranges<br>
|
||||
kiwis<br>
|
||||
carrots<br>
|
||||
--- 1,4 ----<br>
|
||||
apples<br>
|
||||
kiwis<br>
|
||||
carrots<br>
|
||||
+ grapefruits< </pre>
|
||||
<pre>
|
||||
diff -u file1.txt file2.txt
|
||||
</pre>
|
||||
|
||||
The first two lines of this output show us information about our "from"
|
||||
file (file 1) and our "to" file (file 2). It lists the file name,
|
||||
modification date, and modification time of each of our files, one per
|
||||
line. The "from" file is indicated by "***", and the "to" file is
|
||||
indicated by "---"..<br>
|
||||
<br> The line "***************" is just a separator.
|
||||
<br><br> The next line has three asterisks ("***") followed by a line
|
||||
range from the first file (in this case lines 1 through 4, separated by
|
||||
a comma). Then four asterisks ("****").<br>
|
||||
<br> Then it shows us the contents of those lines. If the line is unchanged,
|
||||
it's prefixed by two spaces. If the line is changed, however, it's prefixed
|
||||
by an indicative character and a space. The character meanings are as
|
||||
follows:<br><br>
|
||||
will output:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<table> <tr>
|
||||
<th>Character</th>
|
||||
<th>Meaning</th></tr>
|
||||
<tr> <td> <b>!</b></td>
|
||||
<td>Indicates that this line is part of a group of one or more lines that
|
||||
needs to change. There is a corresponding group of lines prefixed with
|
||||
"!" in the other file's context as well.</td></tr>
|
||||
<tr><td><b>+</b></td>
|
||||
<td>Indicates a line in the second file that needs to be added to the
|
||||
first file.</td></tr>
|
||||
<tr><td><b>-</b></td>
|
||||
<td>Indicates a line in the first file that needs to be deleted.</td>
|
||||
</tr>
|
||||
</table><br>
|
||||
|
||||
After the lines from the first file, there are three dashes ("---"),
|
||||
then a line range, then four dashes ("----"). This indicates the
|
||||
line range in the second file that will sync up with our changes
|
||||
in the first file.<br><br>
|
||||
|
||||
If there is more than one section that needs to change, diff will
|
||||
show these sections one after the other. Lines from the first file will
|
||||
still be indicated with "***", and lines from the second file
|
||||
with "---".
|
||||
|
||||
</li>
|
||||
|
||||
<li><h4> -u</h4>:Unified mode (the -u option) is similar to context mode,
|
||||
but it doesn't display any redundant information. Here's an example,
|
||||
using the same input files as our last example:
|
||||
<pre>diff -u file1.txt file2.txt</pre>
|
||||
will output:<br>
|
||||
<pre> --- file1.txt 2014-08-21 17:58:29.764656635 -0400<br>
|
||||
+++ file2.txt 2014-08-21 17:58:50.768989841 -0400<br>
|
||||
@@ -1,4 +1,4 @@<br>
|
||||
apples<br>
|
||||
-oranges<br>
|
||||
kiwis<br>
|
||||
carrots<br>
|
||||
+grapefruits</pre>
|
||||
<pre>
|
||||
--- file1.txt 2014-08-21 17:58:29.764656635 -0400
|
||||
+++ file2.txt 2014-08-21 17:58:50.768989841 -0400
|
||||
@@ -1,4 +1,4 @@
|
||||
apples
|
||||
-oranges
|
||||
kiwis
|
||||
carrots
|
||||
+grapefruits
|
||||
</pre>
|
||||
|
||||
The output is similar to above, but as you can see, the differences are
|
||||
"unified" into one set.
|
||||
"unified" into one set.
|
||||
</p>
|
||||
|
|
|
@ -3,7 +3,7 @@ layout: page
|
|||
author: Alessandro Luini
|
||||
category-page: advanced
|
||||
category-title: Advanced commands
|
||||
tags: echo calculator print
|
||||
tags: echo print text
|
||||
title: echo
|
||||
---
|
||||
|
||||
|
@ -18,7 +18,7 @@ To use this simple command just type "echo" with the topic we want to
|
|||
print. The computer will return your argument as a string
|
||||
|
||||
<pre>
|
||||
echo "hello world"
|
||||
echo "hello world"
|
||||
hello world
|
||||
</pre>
|
||||
</p>
|
||||
|
|
|
@ -7,33 +7,37 @@ tags: advanced text editor
|
|||
title: emacs
|
||||
---
|
||||
|
||||
<p>Emacs is one of the oldest and most versatile text editors available for
|
||||
UNIX-based systems. It's been around for a long time (more than twenty years
|
||||
for GNU emacs) and is well known for its powerful and rich editing features.
|
||||
Emacs is also more than just a text editor; it can be customized and
|
||||
extended with different "modes", enabling it to be used like an
|
||||
Integrated Development Environment (IDE) for programming languages
|
||||
like Java, C or Python.<br><br>
|
||||
|
||||
<p>
|
||||
Emacs is one of the oldest and most versatile text editors available for
|
||||
UNIX-based systems. It's been around for a long time (more than twenty years
|
||||
for GNU emacs) and is well known for its powerful and rich editing features.<br>
|
||||
Emacs is also more than just a text editor; it can be customized and
|
||||
extended with different "modes", enabling it to be used like an
|
||||
Integrated Development Environment (IDE) for programming languages
|
||||
like Java, C or Python.<br>
|
||||
|
||||
For those who have used both the ubiquitous vi and the user-friendly nano,
|
||||
emacs would come as an interesting cross-between. Its strengths and features
|
||||
would resemble those of vi while its menus, help files and easy-to-remember
|
||||
command-keys would compare with nano.<br><br>
|
||||
command-keys would compare with nano.<br>
|
||||
|
||||
In this article, we will see how we can install emacs
|
||||
in a Linux system and use it for basic text editing. Emacs
|
||||
is also available for graphical window managers for Linux like
|
||||
GNOME; however we will only cover the "text based" version here.</p>
|
||||
is also available for graphical window managers, however we will only cover the
|
||||
"text based" version here.
|
||||
|
||||
<h3>How to install</h3>
|
||||
<p> running the following command:
|
||||
</p><pre>emacs</pre>
|
||||
<p>else:</p>
|
||||
<pre>install emacs</pre>
|
||||
<h3>How to install</h3>
|
||||
|
||||
<br><br>
|
||||
<p>Open a file, or create it like this:</p>
|
||||
<pre> emacs file1.txt</pre>
|
||||
<br>
|
||||
<p> Follow the simple manual that will be open.</p>
|
||||
Run the following command:
|
||||
<pre>
|
||||
install emacs
|
||||
</pre>
|
||||
|
||||
<h3>Open a file, or create it like this:</h3>
|
||||
|
||||
<pre>
|
||||
emacs file1.txt
|
||||
</pre>
|
||||
|
||||
And then follow the simple manual that will be open.
|
||||
</p>
|
||||
|
|
|
@ -6,7 +6,8 @@ category-title: Advanced commands
|
|||
tags: search for occurencies
|
||||
title: grep
|
||||
---
|
||||
<p>The <code>grep</code> is a command that permits to search occurences of a
|
||||
<p>
|
||||
The <code>grep</code> is a command that permits to search occurences of a
|
||||
keyword or more in a file or more. Through some flags you can decide the search criteria.
|
||||
The command grep is case sensitive (robot is different from Robot), but we will see how
|
||||
to ignore it.<br>
|
||||
|
|
|
@ -3,70 +3,84 @@ layout: page
|
|||
author: Alessandro Luini
|
||||
category-page: advanced
|
||||
category-title: Advanced commands
|
||||
tags: Vi advanced text editor
|
||||
tags: advanced text editor hell quit help escape sos
|
||||
title: Vi
|
||||
---
|
||||
<p>
|
||||
The default editor that comes with the UNIX operating system is called
|
||||
vi (visual editor).</p>
|
||||
|
||||
<p>The default editor that comes with the UNIX operating system is called
|
||||
vi (visual editor).</p>
|
||||
<br><br>
|
||||
<p>The UNIX vi editor is a full screen editor and has two modes of
|
||||
operation:</p>
|
||||
<br><br>
|
||||
<ol><li>Command mode commands which cause action to be taken on the file,
|
||||
and</li>
|
||||
<li>Insert mode in which entered text is inserted into the file.</li>
|
||||
</ol>
|
||||
<br><br>
|
||||
<p>In the command mode, every character typed is a command that does
|
||||
something to the text file being edited; a character typed in the
|
||||
command mode may even cause the vi editor to enter the insert mode.
|
||||
In the insert mode, every character typed is added to the text in the
|
||||
file; pressing the {Esc} (Escape) key turns off the Insert mode.</p>
|
||||
<p>While there are a number of vi commands, just a handful of these
|
||||
is usually sufficient for beginning vi users. To assist such users,
|
||||
this Web page contains a sampling of basic vi commands. The most
|
||||
basic and useful commands are marked with an asterisk (* or star)
|
||||
in the tables below. With practice, these commands should become
|
||||
automatic.</p>
|
||||
The UNIX vi editor is a full screen editor and has two modes of operation:
|
||||
<ol>
|
||||
<li>Command mode commands which cause action to be taken on the file</li>
|
||||
<li>Insert mode in which entered text is inserted into the file.</li>
|
||||
</ol>
|
||||
|
||||
<p>Both UNIX and vi are <b>case-sensitive</b>. Be sure not to use a
|
||||
capital letter in place of a lowercase letter; the
|
||||
results will not be what you expect.</p><br><br>
|
||||
In the command mode, every character typed is a command that does
|
||||
something to the text file being edited; a character typed in the
|
||||
command mode may even cause the vi editor to enter the insert mode.<br>
|
||||
|
||||
<h3>How to start <code> vi </code>
|
||||
</h3>
|
||||
<p>To use vi on a file, type in vi filename. If the file named
|
||||
filename exists, then the first page (or screen) of the file
|
||||
will be displayed; if the file does not exist, then an empty
|
||||
file and screen are created into which you may enter text.</p>
|
||||
<pre>vi filename.txt</pre>
|
||||
<p>To edit filename starting at line 1</p>
|
||||
<br><br>
|
||||
<h3>How to exit <code>vi</code></h3>
|
||||
<p>Usually the new or modified file is saved when you leave vi.
|
||||
However, it is also possible to quit vi without saving the file.
|
||||
</p>
|
||||
<p>The cursor moves to bottom of screen whenever a colon (:) is
|
||||
typed. This type of command is completed by hitting the
|
||||
<Return> (or <Enter>) key.<br><br>
|
||||
<pre>:q<Return></pre>
|
||||
<p>quit (or exit). <code>vi</code></p>
|
||||
<pre>:x<Return></pre>
|
||||
<p>quit vi, writing out modified file to
|
||||
file named in original invocationt modified file to file
|
||||
named in original </p>
|
||||
<pre>:wq<Return></pre>
|
||||
<p>quit vi, writing out modified file to file named in
|
||||
original invocation.</p>
|
||||
In the insert mode, every character typed is added to the text in the
|
||||
file; pressing the {Esc} (Escape) key turns off the Insert mode.<br>
|
||||
|
||||
<pre>:q!<Return></pre>
|
||||
<p>quit vi even though latest changes have not been
|
||||
saved for this vi call</p>
|
||||
While there are a number of vi commands, just a handful of these
|
||||
is usually sufficient for beginning vi users. To assist such users,
|
||||
this Web page contains a sampling of basic vi commands. The most
|
||||
basic and useful commands are marked with an asterisk (* or star)
|
||||
in the tables below. With practice, these commands should become
|
||||
automatic.<br>
|
||||
|
||||
Both UNIX and vi are <b>case-sensitive</b>. Be sure not to use a
|
||||
capital letter in place of a lowercase letter; the
|
||||
results will not be what you expect.<br>
|
||||
|
||||
<h3>How to start <code>vi</code></h3>
|
||||
To use vi on a file, type in vi filename. If the file named
|
||||
filename exists, then the first page (or screen) of the file
|
||||
will be displayed; if the file does not exist, then an empty
|
||||
file and screen are created into which you may enter text.</p>
|
||||
|
||||
<pre>
|
||||
vi filename.txt
|
||||
</pre>
|
||||
|
||||
<h3>How to exit <code>vi</code></h3>
|
||||
|
||||
Usually the new or modified file is saved when you leave vi.
|
||||
However, it is also possible to quit vi without saving the file.<br>
|
||||
|
||||
The cursor moves to bottom of screen whenever a colon (:) is
|
||||
typed. This type of command is completed by hitting the
|
||||
<Return> (or <Enter>) key.<br>
|
||||
|
||||
<pre>
|
||||
:q
|
||||
</pre>
|
||||
|
||||
Quit vi without saving
|
||||
|
||||
<pre>
|
||||
:x
|
||||
</pre>
|
||||
|
||||
Quit vi, writing out modified file to
|
||||
file named in original invocationt modified file to file named in origin.
|
||||
|
||||
<pre>
|
||||
:wq
|
||||
</pre>
|
||||
|
||||
Quit vi, writing out modified file to file named in original invocation.
|
||||
|
||||
<pre>
|
||||
:q!
|
||||
</pre>
|
||||
|
||||
Quit vi even though latest changes have not been saved for this vi call.
|
||||
|
||||
|
||||
<h3>Flags</h3>
|
||||
<h4>-r:</h4>
|
||||
<p>Use -r to recover filename that was being edited when system
|
||||
crashed.</p>
|
||||
<pre>vi -r filename.txt</pre>
|
||||
<h3>Recover from a crash</h3>
|
||||
|
||||
Open vi using the <code>-r</code> flag to recover a file that was being edited when a crash
|
||||
happened.
|
||||
</p>
|
||||
|
|
|
@ -6,43 +6,66 @@ category-title: Advanced commands
|
|||
tags: word count lines
|
||||
title: wc
|
||||
---
|
||||
<p>
|
||||
The program reads either standard input or a list of files and
|
||||
generates one or more of the following statistics: newline count,
|
||||
word count, and byte count. If a list of files is provided, both
|
||||
individual file and total statistics follow.
|
||||
|
||||
<p>The program reads either standard input or a list of files and
|
||||
generates one or more of the following statistics: newline count,
|
||||
word count, and byte count. If a list of files is provided, both
|
||||
individual file and total statistics follow.</p>
|
||||
<h3>How to use</h3>
|
||||
Sample execution of wc:
|
||||
|
||||
<h3> How to use</h3>
|
||||
<p>Sample execution of wc:</p>
|
||||
<pre> wc file1.txt file2.txt</pre>
|
||||
<p>will output:</p>
|
||||
<pre>
|
||||
40 149 947 file1.txt<br>
|
||||
2294 16638 97724 file2.txt<br>
|
||||
2334 16787 98671 total</pre>
|
||||
<pre>
|
||||
wc file1.txt file2.txt
|
||||
</pre>
|
||||
|
||||
<p>The first column is the count of newlines, meaning that the text file
|
||||
<code>file1.txt</code> has 40 newlines while bar has 2294 newlines-
|
||||
resulting in a total
|
||||
of 2334 newlines. The second column indicates the number of words in each
|
||||
text file showing that there are 149 words in <code>file1.txt</code>
|
||||
and 16638 words in
|
||||
<code>file2.txt</code> – giving a total of 16787 words. The last
|
||||
column indicates the
|
||||
number of characters in each text file, meaning that the file
|
||||
<code>file1.txt</code> has
|
||||
947 characters while bar has 97724 characters – 98671 characters all
|
||||
in all.</p>
|
||||
will output:
|
||||
|
||||
<h3>Flags</h3>
|
||||
<pre>wc -l file1.txt</pre>
|
||||
<p>prints the line count (note that if the last line does not have \n,
|
||||
it will not be counted).<p>
|
||||
<pre>wc -c file1.txt</pre>
|
||||
<p>prints the byte count.</p>
|
||||
<pre>wc -m file1.txt</pre>
|
||||
<p> prints the character count</p>
|
||||
<pre>wc -L file1.txt</pre>
|
||||
<p>prints the length of longest line (GNU extension)</p>
|
||||
<pre>wc -w file1.txt</pre>
|
||||
<p> prints the word count.</p>
|
||||
<pre>
|
||||
40 149 947 file1.txt
|
||||
2294 16638 97724 file2.txt
|
||||
2334 16787 98671 total
|
||||
</pre>
|
||||
|
||||
The first column is the count of newlines, meaning that the text file
|
||||
<code>file1.txt</code> has 40 newlines while bar has 2294 newlines-
|
||||
resulting in a total of 2334 newlines. The second column indicates the
|
||||
number of words in each text file showing that there are 149 words in <code>file1.txt</code>
|
||||
and 16638 words in <code>file2.txt</code> – giving a total of 16787 words.<br>
|
||||
The last column indicates the number of characters in each text file, meaning that the file
|
||||
<code>file1.txt</code> has 947 characters while bar has 97724 characters – 98671
|
||||
characters all in all.<br>
|
||||
|
||||
<h3>Flags</h3>
|
||||
|
||||
<pre>
|
||||
wc -l file1.txt
|
||||
</pre>
|
||||
|
||||
Prints the line count (note that if the last line does not have <code>\n</code>,
|
||||
it will not be counted).
|
||||
|
||||
<pre>
|
||||
wc -c file1.txt
|
||||
</pre>
|
||||
|
||||
Prints the byte count.
|
||||
|
||||
<pre>
|
||||
wc -m file1.txt
|
||||
</pre>
|
||||
|
||||
Prints the character count.
|
||||
|
||||
<pre>
|
||||
wc -L file1.txt
|
||||
</pre>
|
||||
|
||||
Prints the length of longest line (GNU extension)
|
||||
|
||||
<pre>
|
||||
wc -w file1.txt
|
||||
</pre>
|
||||
|
||||
Prints the word count.
|
||||
</p>
|
||||
|
|
|
@ -1,41 +0,0 @@
|
|||
---
|
||||
layout: page
|
||||
category-title: Basic commands
|
||||
category-page: basic
|
||||
tags: directory change navigate
|
||||
author: Alessandro Marinelli
|
||||
title: cd
|
||||
previous-page: pages/cmd/basic/open.html
|
||||
next-page: pages/cmd/basic/ls.html
|
||||
---
|
||||
<p>
|
||||
The <code>cd</code> command is used to change the working directory<br>
|
||||
The name stands for <i>Change Directory</i>.<br>
|
||||
|
||||
The default cd command syntax is:
|
||||
|
||||
<pre>
|
||||
cd [flags] [path]
|
||||
</pre>
|
||||
|
||||
Where [flags] are the cd flags, read below for more info,and [path] is the
|
||||
path (absolute or relative), of the directory which we want to make as working directory.<br><br>
|
||||
|
||||
<h3>Change the working directory</h3>
|
||||
Let's see how to use the command <code>cd</code> in order to change the working directory
|
||||
|
||||
<pre>
|
||||
pwd
|
||||
~
|
||||
cd Desktop/multimedia
|
||||
pwd
|
||||
~/Desktop/multimedia
|
||||
</pre>
|
||||
|
||||
As you can see, we changed the working directory from ~ (which stands for HOME),
|
||||
to "multimedia". Now our Shell will work on the directory "multimedia"
|
||||
until a new <code>cd</code> will occour.<br>
|
||||
|
||||
<b> Notice:</b> If you want to move to a directory which is not contained in the
|
||||
current working directory, you <u>MUST</u> use the absolute path.
|
||||
</p>
|
|
@ -1,58 +0,0 @@
|
|||
---
|
||||
layout: page
|
||||
category-title: Basic commands
|
||||
category-page: basic
|
||||
tags: directory list
|
||||
author: Alessandro Marinelli
|
||||
title: mkdir
|
||||
previous-page: pages/cmd/basic/ls.html
|
||||
next-page: pages/cmd/basic/info.html
|
||||
---
|
||||
<p>
|
||||
The <code>mkdir</code> command is used to create new directories<br>
|
||||
The name stands for <i>MaKe DIRectory</i>.<br>
|
||||
|
||||
The default ls command syntax is:
|
||||
|
||||
<pre>
|
||||
mkdir [flags] [-m mode] directory_name ...
|
||||
</pre>
|
||||
|
||||
Where [flags] are the mkdir <code>-p</code> and <code>-v</code> - flags,
|
||||
read below for more info, and <i>directory_name</i> is the
|
||||
name of the new directory we are going to create.<br><br>
|
||||
|
||||
<h3>Create a new directory</h3>
|
||||
Let's see how to create a new directory:
|
||||
|
||||
<pre>
|
||||
ls
|
||||
|
||||
mkdir test_directory
|
||||
ls
|
||||
test_directory
|
||||
</pre>
|
||||
|
||||
<h3>Create a path of directories</h3>
|
||||
Using the flag <code>-p</code> we can create a path of directories, allowing us
|
||||
to build more than a directory at once.
|
||||
|
||||
<pre>
|
||||
mkdir -p test_directory/subdir/subsubdir
|
||||
</pre>
|
||||
|
||||
As you can see, we are now creating two directories: one named <i>subdir</i> and
|
||||
another one, included in this, named <i>subsubdir</i><br>
|
||||
The -p flag is necessary to allow the shell to create intermediate
|
||||
directories as required.<br><br>
|
||||
|
||||
<h3>Create directories with specified permissions</h3>
|
||||
The <code>-m mode</code> option allows us to set permissions at the new directory
|
||||
that we are now creating.
|
||||
|
||||
<pre>
|
||||
mkdir -m 777 test_free_directory
|
||||
</pre>
|
||||
|
||||
Our new directory will now have read,write and execute permissions for user, group and others.
|
||||
</p>
|
|
@ -1,6 +1,8 @@
|
|||
---
|
||||
layout: page
|
||||
tags: toolbar
|
||||
category-page: basic
|
||||
category-title: Basic commands
|
||||
author: Marzio Lunghi
|
||||
title: MacOS terminal toolbar
|
||||
<!--next-page: pages/cmd/basic/ --> <!-- The sequence must be defined -->
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
layout: page
|
||||
category_title: Basic commands
|
||||
category-page: basic
|
||||
category-title: Basic commands
|
||||
tags: Whatis
|
||||
author: Marzio Lunghi
|
||||
title: whatis
|
||||
|
|
|
@ -8,49 +8,50 @@ title: Base64
|
|||
previous-page:
|
||||
next-page:
|
||||
---
|
||||
<p><i>Base64</i> is an encoding system that allows the translation of binary
|
||||
data into ASCII text strings, basing upon 64 different ASCII characters.
|
||||
<p>
|
||||
<i>Base64</i> is an encoding system that allows the translation of binary
|
||||
data into ASCII text strings, basing upon 64 different ASCII characters.<br>
|
||||
Each <i>Base64</i> digit represents exactly 6 bits of data so, for example,
|
||||
six bytes (a total of 48 bits) can be represented by eight <i>Base64</i> digits.</p>
|
||||
six bytes (a total of 48 bits) can be represented by eight <i>Base64</i> digits.
|
||||
|
||||
<p>By writing the command <code>base64</code> you can encode and decode Base64 data.
|
||||
The command follows the structure:</p>
|
||||
By writing the command <code>base64</code> you can encode and decode Base64 data.
|
||||
The command follows the structure:
|
||||
|
||||
<pre>
|
||||
Device-name:Current-position username$ base64 [flag] [-i input item] [-o output item]
|
||||
base64 [flag] [-i input item] [-o output item]
|
||||
</pre>
|
||||
|
||||
<h3>Defining input and output files</h3>
|
||||
<p>In this case, flag such as <code>-i</code> and <code>-o</code> are fundamental
|
||||
to define respectevely from which file take the stream and into which to store
|
||||
the result. Naturally, the path and the name of the files must be specified.</p>
|
||||
In this case, flag such as <code>-i</code> and <code>-o</code> are fundamental
|
||||
to define respectevely from which file take the stream and into which to store
|
||||
the result. Naturally, the path and the name of the files must be specified.
|
||||
|
||||
<pre>
|
||||
Device-name:Current-position username$ base64 -i path/input.file -o path/output.file
|
||||
base64 -i path/input.file -o path/output.file
|
||||
</pre>
|
||||
|
||||
<h3>Insert line breaks</h3>
|
||||
<p>Using the <code>-b</code> (which stands for <i>break</i>) flag followed by a
|
||||
number, line breaks are added every specified "number" characters.</p>
|
||||
Using the <code>-b</code> (which stands for <i>break</i>) flag followed by a
|
||||
number, line breaks are added every specified "number" characters.
|
||||
|
||||
<pre>
|
||||
Device-name:Current-position username$ base64 -b number path/input.file path/output.file
|
||||
base64 -b number path/input.file path/output.file
|
||||
</pre>
|
||||
|
||||
<p>By leaving the number field empty, an unbroken stream will be generated.</p>
|
||||
By leaving the number field empty, an unbroken stream will be generated.<
|
||||
|
||||
<h3>Decode Base64</h3>
|
||||
<p>Using the <code>-D</code> flag (which stands for decode), you obtain as
|
||||
output the input message decoded into binary data.</p>
|
||||
Using the <code>-D</code> flag (which stands for decode), you obtain as
|
||||
output the input message decoded into binary data.
|
||||
|
||||
<pre>
|
||||
Device-name:Current-position username$ base64 -D [-i input item] [-o output item]
|
||||
base64 -D [-i input item] [-o output item]
|
||||
</pre>
|
||||
|
||||
<h3>Summarize the tools</h3>
|
||||
<p>Using the <code>-h</code> (which stands for <i>help</i>) a short paragraph
|
||||
in which are listed the flags with the respective paragraph will be displayed.</p>
|
||||
Using the <code>-h</code> (which stands for <i>help</i>) a short paragraph
|
||||
in which are listed the flags with the respective paragraph will be displayed.
|
||||
|
||||
<pre>
|
||||
Device-name:Current-position username$ base64 -h
|
||||
base64 -h
|
||||
</pre>
|
||||
|
|
|
@ -8,9 +8,10 @@ title: chroot
|
|||
previous-page: pages/cmd/interm/
|
||||
next-page: pages/cmd/interm/
|
||||
---
|
||||
<p>The <code>chroot</code> utility allows you to change its root directory to
|
||||
the one indicated in newroot and, if given, executes the command.</p>
|
||||
<p>
|
||||
The <code>chroot</code> utility allows you to change its root directory to
|
||||
the one indicated in newroot and, if given, executes the command.</p>
|
||||
|
||||
<pre>
|
||||
Device-name:Current-position username$ chroot newroot [command]
|
||||
chroot newroot [command]
|
||||
</pre>
|
||||
|
|
|
@ -2,81 +2,76 @@
|
|||
layout: page
|
||||
category-page: intermediate
|
||||
category-title: Intermediate commands
|
||||
tags: curl download http client crawler request online
|
||||
tags: curl download http client crawler request online
|
||||
author: Claudio Maggioni
|
||||
title: curl
|
||||
---
|
||||
<p>
|
||||
The <code>curl</code> command is a fast and versatile shell program that can
|
||||
request online content using various protocols, including <em>HTTP</em>,
|
||||
<em>FTP</em> and <em>POP3</em>. Some example use cases for this utility are
|
||||
the need to automate some remote call to a server, testing <em>REST</em> or
|
||||
<em>SOAP</em> apis or simply download or display the <em>HTML</em> code of a
|
||||
website.
|
||||
</p>
|
||||
The <code>curl</code> command is a fast and versatile shell program that can
|
||||
request online content using various protocols, including <i>HTTP</i>,
|
||||
<i>FTP</i> and <i>POP3</i>. Some example use cases for this utility are
|
||||
the need to automate some remote call to a server, testing <i>REST</i> or
|
||||
<i>SOAP</i> apis or simply download or display the <i>HTML</i> code of a
|
||||
website.
|
||||
|
||||
<p>
|
||||
The name, as mentioned in the <a href="https://ec.haxx.se/curl-name.html">
|
||||
<em>Evertything curl</em></a> online book, stands for <em>"client URL"</em>
|
||||
and the right pronunciation rhymes with "earl" or "girl". By this fact, the
|
||||
reader is able to tell the author of this page to stop pronuncing it as
|
||||
<em>"cooorl"</em>, like if the name is some sort of word in the
|
||||
<a href="https://en.wikipedia.org/wiki/Bergamasque_dialect"><em>Bergamasque
|
||||
dialect</em></a>.
|
||||
</p>
|
||||
The name, as mentioned in the <a href="https://ec.haxx.se/curl-name.html">
|
||||
<i>Evertything curl</i></a> online book, stands for <i>"client URL"</i>
|
||||
and the right pronunciation rhymes with "earl" or "girl". By this fact, the
|
||||
reader is able to tell the author of this page to stop pronuncing it as
|
||||
<i>"cooorl"</i>, like if the name is some sort of word in the
|
||||
<a href="https://en.wikipedia.org/wiki/Bergamasque_dialect"><i>Bergamasque
|
||||
dialect</i></a>.
|
||||
|
||||
<h3>Usage</h3>
|
||||
|
||||
<p>
|
||||
<code>curl</code> is a very complex command: if you want to learn it deeply,
|
||||
please refer to the previously mentioned guide called
|
||||
<a href="https://ec.haxx.se/"><em>Evertything curl</em></a>. Some basic
|
||||
ways to use this command are shown below:
|
||||
</p>
|
||||
<code>curl</code> is a very complex command: if you want to learn it deeply,
|
||||
please refer to the previously mentioned guide called
|
||||
<a href="https://ec.haxx.se/"><i>Evertything curl</i></a>. Some basic
|
||||
ways to use this command are shown below:
|
||||
|
||||
<h4>Download the contents of the page "example.com" to "file.html"</h4>
|
||||
Example: Download the contents of the page "example.com" to "file.html"
|
||||
|
||||
<pre>
|
||||
curl example.com -o file.html
|
||||
</pre>
|
||||
|
||||
<h4>Download the contents of the page "en.wikipedia.org/wiki/Curl" to
|
||||
a file with the same name as the last element in the path (here "Curl")</h4>
|
||||
Example: Download the contents of the page "en.wikipedia.org/wiki/Curl" to
|
||||
a file with the same name as the last element in the path (here "Curl")
|
||||
|
||||
<pre>
|
||||
curl -O en.wikipedia.org/wiki/Curl
|
||||
</pre>
|
||||
|
||||
<h4>Download the contents of the page "example.com" to "file.html"</h4>
|
||||
Example: Download the contents of the page "example.com" to "file.html"
|
||||
|
||||
<pre>
|
||||
curl example.com -o file.html
|
||||
</pre>
|
||||
|
||||
<h4>Download the contents of the page "example.com" following location
|
||||
redirects (<code>-L</code> flag)</h4>
|
||||
Example: Download the contents of the page "example.com" following location
|
||||
redirects (<code>-L</code> flag)
|
||||
|
||||
<pre>
|
||||
curl -O -L example.com
|
||||
curl -O -L example.com
|
||||
</pre>
|
||||
|
||||
<h4>Download the contents of the page "example.com" resuming a previous attempt
|
||||
(<code>-C</code> flag)</h4>
|
||||
Example: Download the contents of the page "example.com" resuming a previous attempt
|
||||
(<code>-C</code> flag)
|
||||
|
||||
<pre>
|
||||
curl -O -C example.com
|
||||
curl -O -C example.com
|
||||
</pre>
|
||||
|
||||
|
||||
<h4>Do an HTTP DELETE request to "example.com" with a custom header
|
||||
(<code>-H</code> and <code>-X</code> flags)</h4>
|
||||
Example: Do an HTTP DELETE request to "example.com" with a custom header
|
||||
(<code>-H</code> and <code>-X</code> flags)
|
||||
|
||||
<pre>
|
||||
curl -H 'User-Agent: Tamagotchi' -X DELETE example.com
|
||||
</pre>
|
||||
|
||||
<h4>Print to <em>stdout</em> "example.com" making a request with HTTP basic
|
||||
authentication (<code>-u</code> flag)</h4>
|
||||
Example: Print to <i>stdout</i> "example.com" making a request with HTTP basic
|
||||
authentication (<code>-u</code> flag)
|
||||
|
||||
<pre>
|
||||
curl -u bob:P@ssw0rd example.com
|
||||
|
|
|
@ -8,49 +8,49 @@ title: git
|
|||
---
|
||||
|
||||
<p>
|
||||
If you want to use <i>Git</i> on the shell you need to know how this command works (usually it is preinstalled on Mac and Linux).
|
||||
The <code>git</code> command providesa set of high-level operations and full access to internals.
|
||||
If you want to use <i>Git</i> on the shell you need to know how this command works (usually it is preinstalled on Mac and Linux).<br>
|
||||
The <code>git</code> command providesa set of high-level operations and full access to internals.
|
||||
|
||||
Here is the basic syntax but sometimes you will may have some more complicated:
|
||||
Here is the basic syntax but sometimes you will may have some more complicated:
|
||||
|
||||
<pre>
|
||||
git [flags] [path]
|
||||
</pre>
|
||||
<pre>
|
||||
git [flags] [path]
|
||||
</pre>
|
||||
|
||||
First of all, you need to know if you have the right version of git with:
|
||||
First of all, you need to know if you have the right version of git with:
|
||||
|
||||
<pre>
|
||||
git --version
|
||||
</pre>
|
||||
<pre>
|
||||
git --version
|
||||
</pre>
|
||||
|
||||
Now that we know that we have the right version, we can move to the common <code>git</code> flags used.
|
||||
I will divide commands in different type of operation.<br>
|
||||
Now that we know that we have the right version, we can move to the common <code>git</code> flags used.
|
||||
I will divide commands in different type of operation.<br>
|
||||
|
||||
<h3>Start a working area</h3>
|
||||
<ul>
|
||||
<li>-clone: is used to clone a repository in a new directory.</li>
|
||||
<li>-init: this onw is used to create an empty Git repository or reinitialize an existing one.</li>
|
||||
</ul>
|
||||
<h3>Start a working area</h3>
|
||||
<ul>
|
||||
<li>-clone: is used to clone a repository in a new directory.</li>
|
||||
<li>-init: this onw is used to create an empty Git repository or reinitialize an existing one.</li>
|
||||
</ul>
|
||||
|
||||
<h3>Work on the current changes</h3>
|
||||
<ul>
|
||||
<li>-add: add a file contents to the index.</li>
|
||||
<li>-mv: move or renamea file, directory or a symlink (a symbolic link).</li>
|
||||
<li>-rm: remove files from the working tree and from the index.</li>
|
||||
</ul>
|
||||
<h3>Work on the current changes</h3>
|
||||
<ul>
|
||||
<li>-add: add a file contents to the index.</li>
|
||||
<li>-mv: move or renamea file, directory or a symlink (a symbolic link).</li>
|
||||
<li>-rm: remove files from the working tree and from the index.</li>
|
||||
</ul>
|
||||
|
||||
<h3>History and state</h3>
|
||||
<ul>
|
||||
<li>-log: show commit logs.</li>
|
||||
<li>-status: show the working tree status.</li>
|
||||
</ul>
|
||||
<h3>History and state</h3>
|
||||
<ul>
|
||||
<li>-log: show commit logs.</li>
|
||||
<li>-status: show the working tree status.</li>
|
||||
</ul>
|
||||
|
||||
<h3>Managing modification</h3>
|
||||
<ul>
|
||||
<li>-commit: record changes to the repository.</li>
|
||||
<li>-diff: show changes between commits.</li>
|
||||
<li>-merge: merge two or more development histories together.</li>
|
||||
<li>-pull: fetch from and update your local directory with the repository.</li>
|
||||
<li>-push: update the remote repository with your changes.</li>
|
||||
</ul>
|
||||
<h3>Managing modification</h3>
|
||||
<ul>
|
||||
<li>-commit: record changes to the repository.</li>
|
||||
<li>-diff: show changes between commits.</li>
|
||||
<li>-merge: merge two or more development histories together.</li>
|
||||
<li>-pull: fetch from and update your local directory with the repository.</li>
|
||||
<li>-push: update the remote repository with your changes.</li>
|
||||
</ul>
|
||||
</p>
|
||||
|
|
|
@ -7,104 +7,90 @@ author: Claudio Maggioni
|
|||
title: pass
|
||||
---
|
||||
<p>
|
||||
The <code>pass</code> command, also known as <em>Password Store</em>, is an
|
||||
entirely offline password manager that uses <em>GPG</em> for encryption with
|
||||
the ability to sync the (entirely encrypted) passwords using <code>git</code>.
|
||||
<code>pass</code> works on <em>MacOS</em> (avaliable via
|
||||
<a href="https://brew.sh">Homebrew</a>), <em>Linux</em> and on
|
||||
<em>Android</em> (with a GUI app). Think of it as an entirely FLOSS
|
||||
alternative to services like <em>Keypass</em> or <em>Dashlane</em>.
|
||||
</p>
|
||||
The <code>pass</code> command, also known as <i>Password Store</i>, is an
|
||||
entirely offline password manager that uses <i>GPG</i> for encryption with
|
||||
the ability to sync the (entirely encrypted) passwords using <code>git</code>.
|
||||
<code>pass</code> works on <i>MacOS</i> (avaliable via
|
||||
<a href="https://brew.sh">Homebrew</a>), <i>Linux</i> and on
|
||||
<i>Android</i> (with a GUI app). Think of it as an entirely FLOSS
|
||||
alternative to services like <i>Keypass</i> or <i>Dashlane</i>.<br>
|
||||
|
||||
<p>
|
||||
Excluding the initial setup (that requires the creation of a <em>GPG key</em>),
|
||||
<code>pass</code> is very easy and straightforward to use: instead of
|
||||
printing passwords to <em>stdout</em>, <code>pass</code> copies them in the
|
||||
system clipboard, erasing them after a certain number of seconds (usually
|
||||
<em>45</em>).
|
||||
</p>
|
||||
Excluding the initial setup (that requires the creation of a <i>GPG key</i>),
|
||||
<code>pass</code> is very easy and straightforward to use: instead of
|
||||
printing passwords to <i>stdout</i>, <code>pass</code> copies them in the
|
||||
system clipboard, erasing them after a certain number of seconds (usually
|
||||
<i>45</i>).<br>
|
||||
|
||||
<p>
|
||||
<code>pass</code> has many unofficial GUI clients and migration scripts from
|
||||
other password managers. For more information, check out
|
||||
<a href=https://www.passwordstore.org/#other">the official website</a>.
|
||||
</p>
|
||||
<code>pass</code> has many unofficial GUI clients and migration scripts from
|
||||
other password managers. For more information, check out
|
||||
<a href=https://www.passwordstore.org/#other">the official website</a>.<br>
|
||||
|
||||
<h3>Setup</h3>
|
||||
|
||||
<p>
|
||||
An accurate walkthrough through the setup of <code>pass</code>, in addition
|
||||
to some other useful sets of commands (such as how to migrate the password
|
||||
repository to another computer) is provided
|
||||
in this <a href="https://gist.github.com/flbuddymooreiv/a4f24da7e0c3552942ff">
|
||||
GitHub Gist</a> by <a href="https://github.com/flbuddymooreiv">
|
||||
<em>flbuddymooreiv</em></a>.
|
||||
</p>
|
||||
An accurate walkthrough through the setup of <code>pass</code>, in addition
|
||||
to some other useful sets of commands (such as how to migrate the password
|
||||
repository to another computer) is provided
|
||||
in this <a href="https://gist.github.com/flbuddymooreiv/a4f24da7e0c3552942ff">
|
||||
GitHub Gist</a> by <a href="https://github.com/flbuddymooreiv">
|
||||
<i>flbuddymooreiv</i></a>.<br>
|
||||
|
||||
<p>
|
||||
For more detailed explainations on the setup process or on any commands
|
||||
please check out the online version of the
|
||||
<a href="https://git.zx2c4.com/password-store/about/">man page</a>, which is
|
||||
surprisingly more readable that most of the man pages for other utilities.
|
||||
</p>
|
||||
For more detailed explainations on the setup process or on any commands
|
||||
please check out the online version of the
|
||||
<a href="https://git.zx2c4.com/password-store/about/">man page</a>, which is
|
||||
surprisingly more readable that most of the man pages for other utilities.
|
||||
|
||||
<h3>Usage</h3>
|
||||
|
||||
<p>
|
||||
Common <code>pass</code> commands are shown below. For more information refer
|
||||
to the documentation linked above.
|
||||
</p>
|
||||
Common <code>pass</code> commands are shown below. For more information refer
|
||||
to the documentation linked above.<br>
|
||||
|
||||
<h4>Initialize the password repository with a GPG key with id "0DEADBEEF"</h4>
|
||||
Example: Initialize the password repository with a GPG key with id "0DEADBEEF"
|
||||
|
||||
<pre>
|
||||
pass init 0DEADBEEF
|
||||
</pre>
|
||||
|
||||
<h4>Insert a password for <em>example.com</em> with username <em>bob</em> in
|
||||
the password repository interactively</h4>
|
||||
Example: Insert a password for <i>example.com</i> with username <i>bob</i> in
|
||||
the password repository interactively
|
||||
|
||||
<pre>
|
||||
pass insert example.com/bob
|
||||
</pre>
|
||||
|
||||
<p>
|
||||
Please note that the password <em>name</em> here follows the most common
|
||||
naming convention in <code>pass</code>, which is
|
||||
<code>{website}/{username}</code>. Passwords can be stored in
|
||||
hierarchical structures (i.e. in nested folders), but the naming is up to the
|
||||
user.
|
||||
</p>
|
||||
Please note that the password <i>name</i> here follows the most common
|
||||
naming convention in <code>pass</code>, which is
|
||||
<code>{website}/{username}</code>. Passwords can be stored in
|
||||
hierarchical structures (i.e. in nested folders), but the naming is up to the
|
||||
user.<br>
|
||||
|
||||
<h4>Generate a password for <em>zombo.com</em> of 16 characters and copy it in
|
||||
the clipboard</h4>
|
||||
Example: Generate a password for <i>zombo.com</i> of 16 characters and copy it in
|
||||
the clipboard
|
||||
|
||||
<pre>
|
||||
pass generate -c zombo.com/bob 16
|
||||
</pre>
|
||||
|
||||
<h4>Retrieve the password for <em>google.com</em> and copy it in the system
|
||||
clipboard (<code>-c</code> flag)</h4>
|
||||
Example: Retrieve the password for <i>google.com</i> and copy it in the system
|
||||
clipboard (<code>-c</code> flag)
|
||||
|
||||
<pre>
|
||||
pass -c google.com/bob@gmail.com
|
||||
</pre>
|
||||
|
||||
<h4>Edit the password for <em>facebook.com</em> using the default editor</h4>
|
||||
Example: Edit the password for <i>facebook.com</i> using the default editor
|
||||
|
||||
<pre>
|
||||
pass edit facebook.com/bob
|
||||
</pre>
|
||||
|
||||
<h4>Convert the password repository to a git repository for synchronization</h4>
|
||||
Edit: Convert the password repository to a git repository for synchronization
|
||||
|
||||
<pre>
|
||||
pass git init
|
||||
</pre>
|
||||
|
||||
<p>
|
||||
Every <code>git</code> command on the password repository must be given with
|
||||
the prefix <code>pass git</code> (e.g. <code>pass git push</code>). An
|
||||
automatic commit is performed whenever a password is created, edited or
|
||||
deleted.
|
||||
Every <code>git</code> command on the password repository must be given with
|
||||
the prefix <code>pass git</code> (e.g. <code>pass git push</code>). An
|
||||
automatic commit is performed whenever a password is created, edited or
|
||||
deleted.
|
||||
</p>
|
||||
|
|
|
@ -2,60 +2,73 @@
|
|||
layout: page
|
||||
category-title: Intermediate commands
|
||||
category-page: intermediate
|
||||
tags:
|
||||
tags:
|
||||
author: Andrea Brites Marto
|
||||
title: ps
|
||||
---
|
||||
|
||||
<p>
|
||||
The <code>ps</code> command stands for "process status" and it is used to provide various information about the currently running processes.
|
||||
The <code>ps</code> command stands for "process status" and it is used to provide
|
||||
various information about the currently running processes.<br>
|
||||
|
||||
Every process is an executing instance of a program which is assigned a unique PID (process identification numbers) by the system.
|
||||
Every process is an executing instance of a program which is assigned a unique PID
|
||||
(process identification numbers) by the system.<br>
|
||||
|
||||
The basic syntax of <code>ps</code> is:
|
||||
The basic syntax of <code>ps</code> is:
|
||||
|
||||
<pre>
|
||||
ps [options]
|
||||
</pre>
|
||||
<pre>
|
||||
ps [options]
|
||||
</pre>
|
||||
|
||||
This command can be used without any option and by doing this you will get the standard output, which is the display monitor by default with four items of information for at least two processes currently on the system: the shell and ps.
|
||||
This command can be used without any option and by doing this you will get the standard output,
|
||||
which is the display monitor by default with four items of information for at least two
|
||||
processes currently on the system: the shell and ps.<br>
|
||||
|
||||
Since we have already saw PID before, the TTY information that stands for terminal type (originally teletype) is the console (or terminal) in which the user logged into.
|
||||
Since we have already saw PID before, the TTY information that stands for terminal type
|
||||
(originally teletype) is the console (or terminal) in which the user logged into.<br>
|
||||
|
||||
TIME is very simple: is the amount of CPU time in minutes and seconds that the process has been running. CMD is simply the name of the command that launched the process.
|
||||
TIME is very simple: is the amount of CPU time in minutes and seconds that the process
|
||||
has been running. CMD is simply the name of the command that launched the process.<br>
|
||||
|
||||
Here you will find some common option combinations
|
||||
Here you will find some common option combinations
|
||||
|
||||
<pre>
|
||||
ps -aux | less
|
||||
</pre>
|
||||
<pre>
|
||||
ps -aux | less
|
||||
</pre>
|
||||
|
||||
<ul>
|
||||
<li>The -a option is used to list the processes of all users on the system.</li>
|
||||
<li>The -u option tells ps to provide detailed information about each process.</li>
|
||||
<li>The -x option adds to the list, processes that have no controlling terminal (programs launched during booting).</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>The -a option is used to list the processes of all users on the system.</li>
|
||||
<li>The -u option tells ps to provide detailed information about each process.</li>
|
||||
<li>The -x option adds to the list, processes that have no controlling terminal
|
||||
(programs launched during booting).
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
This can be piped to the less command ( see out section pipe), which let us to view all processes in one screenfull at a time.
|
||||
This can be piped to the less command ( see out section pipe), which let us to
|
||||
view all processes in one screenfull at a time.<br>
|
||||
|
||||
Another way to view all processes running on the system is:
|
||||
Another way to view all processes running on the system is:
|
||||
|
||||
<div>
|
||||
<pre>
|
||||
ps -ef | less
|
||||
</pre>
|
||||
</div>
|
||||
<pre>
|
||||
ps -ef | less
|
||||
</pre>
|
||||
|
||||
Where:
|
||||
Where:
|
||||
|
||||
<ul>
|
||||
<li>The -e option is used to generate a list of information about every process (currently running).</li>
|
||||
<li>The -f option providesa list that contains some information for each process.</li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li>The -e option is used to generate a list of information about every process
|
||||
(currently running).
|
||||
</li>
|
||||
<li>The -f option providesa list that contains some information for each process.</li>
|
||||
</ul>
|
||||
|
||||
This is very useful, it can be used for example to control UID information (username of the account that owns the process) and STIME to know when the process started, or the starting date.
|
||||
This is very useful, it can be used for example to control UID information (username of
|
||||
the account that owns the process) and STIME to know when the process started, or
|
||||
the starting date.<br>
|
||||
|
||||
|
||||
In the end the <code>ps</code> is very powerful if we knoe how to use it. If you need to kill a process you can list all process in various ways and search for the process you need to kill by reading its PID or by UID for example.
|
||||
|
||||
</p>
|
||||
In the end the <code>ps</code> is very powerful if we knoe how to use it.
|
||||
If you need to kill a process you can list all process in various ways
|
||||
and search for the process you need to kill by reading its PID or by UID for example.
|
||||
\
|
||||
</p>
|
||||
|
|
|
@ -8,12 +8,14 @@ title: sync
|
|||
previous-page: pages/cmd/interm/
|
||||
next-page: pages/cmd/interm/
|
||||
---
|
||||
<p>The <code>sync</code> utility force the completion of pending disk writes.</p>
|
||||
<p>
|
||||
The <code>sync</code> utility force the completion of pending disk writes.</p>
|
||||
|
||||
<pre>
|
||||
Device-name:Current-position username$ sync
|
||||
sync
|
||||
</pre>
|
||||
|
||||
<p>This command can be used to verify that there are no pending disk writes,
|
||||
This command can be used to verify that there are no pending disk writes,
|
||||
so that all the operations are completed, before the processor is stopped by other
|
||||
commands.</p>
|
||||
commands.
|
||||
</p>
|
||||
|
|
|
@ -2,39 +2,35 @@
|
|||
layout: page
|
||||
category-page: intermediate
|
||||
category-title: Intermediate commands
|
||||
tags: watch repeat track analyse seconds
|
||||
tags: watch repeat track analyse seconds
|
||||
author: Claudio Maggioni
|
||||
title: watch
|
||||
---
|
||||
<p>
|
||||
The <code>watch</code> command is a system utility able to execute a command
|
||||
every <em>n</em> seconds by clearing the screen and displaying the
|
||||
output of the command each time the interval is over. This command is very
|
||||
simple to use and very effective: it can be used to check the size in bytes
|
||||
of a file while downloading it, or it can be used on <em>Linux</em> to jiggle
|
||||
around the mouse every ~5 minutes with the command <code>xdotool</code> in
|
||||
order to keep the computer awake.
|
||||
</p>
|
||||
The <code>watch</code> command is a system utility able to execute a command
|
||||
every <i>n</i> seconds by clearing the screen and displaying the
|
||||
output of the command each time the interval is over. This command is very
|
||||
simple to use and very effective: it can be used to check the size in bytes
|
||||
of a file while downloading it, or it can be used on <i>Linux</i> to jiggle
|
||||
around the mouse every ~5 minutes with the command <code>xdotool</code> in
|
||||
order to keep the computer awake.
|
||||
|
||||
<h3>Usage</h3>
|
||||
|
||||
<p>
|
||||
<code>watch</code> has a very simple syntax: the amount in seconds to wait
|
||||
between executions is specified in seconds using the <code>-n</code> flag
|
||||
and the default value is <em>2</em>. Also, differences between older and
|
||||
newer outputs are highlighted if the <code>-d</code> flag is used.
|
||||
</p>
|
||||
<code>watch</code> has a very simple syntax: the amount in seconds to wait
|
||||
between executions is specified in seconds using the <code>-n</code> flag
|
||||
and the default value is <i>2</i>. Also, differences between older and
|
||||
newer outputs are highlighted if the <code>-d</code> flag is used.
|
||||
|
||||
<h4>Watch the size of "ubuntu.iso" every 5 seconds</h4>
|
||||
Example: Watch the size of "ubuntu.iso" every 5 seconds
|
||||
|
||||
<pre>
|
||||
watch -n 5 du -h ubuntu.iso
|
||||
watch -n 5 du -h ubuntu.iso
|
||||
</pre>
|
||||
|
||||
<h4>Watch the contents of "site.html" every 2 seconds and highlight the
|
||||
differencies between iterations</h4>
|
||||
Example: Watch the contents of "site.html" every 2 seconds and highlight the
|
||||
differencies between iterations
|
||||
|
||||
<pre>
|
||||
watch -d cat site.html
|
||||
</pre>
|
||||
|
||||
|
|
|
@ -3,51 +3,40 @@ layout: page
|
|||
category-page: fs
|
||||
category-title: FileSystem
|
||||
tags: change directory
|
||||
author: Mirko Ponzio
|
||||
author: Alessandro Marinelli
|
||||
title: cd
|
||||
---
|
||||
<p> In the operative system Unix and Unix-like, and in general in the system
|
||||
POSIX, cd (change directory) is an internal command of shell that change the
|
||||
corrent directory. <br><br>
|
||||
|
||||
The command is only an internal command of shell, it can't be an external program,
|
||||
because one process can modified only the own corrent directory and not that of
|
||||
other processes.
|
||||
So the Shell has to implement this function autonomously.
|
||||
<!-- Co-authored with Mirko Ponzio -->
|
||||
|
||||
|
||||
La new setting of the corrent directory it has been inherited from the started
|
||||
programs.
|
||||
<br> <br>
|
||||
|
||||
After the change of the directory, the variable pwd it is automatically updated
|
||||
with the pathname of the new directory.
|
||||
</p> <br> <br>
|
||||
|
||||
<h2> Some usefull command for CD<h2>
|
||||
<ul>
|
||||
<li> cd by itself or cd ~ will always put you in your home directory.</li>
|
||||
<li> cd . will leave you in the same directory you are currently in (i.e. your current directory won't change).
|
||||
This can be useful if your shell's internal code can't deal with the directory you are in being recreated; running cd . will place your
|
||||
shell in the recreated directory. </li>
|
||||
<li> cd ~username will put you in username's home directory. </li>
|
||||
<li> cd dir (without a /) will put you in a subdirectory; for example, if you are in /usr, typing cd bin will put you in /usr/bin, while cd /bin puts you in /bin.</li>
|
||||
<li> cd .. will move you up one directory. So, if you are /usr/bin/tmp, cd .. moves you to /usr/bin, while cd ../.. moves you to /usr (i.e. up two levels).
|
||||
You can use this indirection to access subdirectories too. So, from /usr/bin/tmp, you can use cd ../../local to go to /usr/local.</li>
|
||||
<li> cd - will switch you to the previous directory. For example, if you are in /usr/bin/tmp, and go to /etc,
|
||||
\you can type cd - to go back to /usr/bin/tmp. You can use this to toggle back and forth between two directories. </li>
|
||||
</ul>
|
||||
|
||||
<h2> sintax of cd </h2>
|
||||
<p>
|
||||
La sintassi generale di cd è la seguente:
|
||||
The <code>cd</code> command is used to change the working directory<br>
|
||||
The name stands for <i>Change Directory</i>.<br>
|
||||
|
||||
cd [opzioni] [--] [dir]
|
||||
Il parametro facoltativo dir indica la nuova directory corrente. Se non è specificato, viene usato al suo posto il valore della variabile d'ambiente HOME,
|
||||
che normalmente indica la home directory dell'utente. Se invece è un trattino ("-"), allora viene usata la directory corrente precedente.
|
||||
The default cd command syntax is:
|
||||
|
||||
Se il parametro dir è un pathname relativo che non ha come primo elemento "." o "..", allora esso viene unito a ciascuna delle directory
|
||||
elencate nella variabile d'ambiente CDPATH: se il risultato corrisponde ad una directory esistente, allora essa diviene la nuova directory corrente.
|
||||
Altrimenti dir viene considerato relativo alla directory corrente.
|
||||
<pre>
|
||||
cd [flags] [path]
|
||||
</pre>
|
||||
|
||||
Il doppio trattino -- (facoltativo) indica che i parametri successivi non sono da considerarsi opzioni.</p>
|
||||
Where [flags] are the cd flags, read below for more info,and [path] is the
|
||||
path (absolute or relative), of the directory which we want to make as working directory.<br><br>
|
||||
|
||||
<h3>Change the working directory</h3>
|
||||
Let's see how to use the command <code>cd</code> in order to change the working directory
|
||||
|
||||
<pre>
|
||||
pwd
|
||||
~
|
||||
cd Desktop/multimedia
|
||||
pwd
|
||||
~/Desktop/multimedia
|
||||
</pre>
|
||||
|
||||
As you can see, we changed the working directory from ~ (which stands for HOME),
|
||||
to "multimedia". Now our Shell will work on the directory "multimedia"
|
||||
until a new <code>cd</code> will occour.<br>
|
||||
|
||||
<b> Notice:</b> If you want to move to a directory which is not contained in the
|
||||
current working directory, you <u>MUST</u> use the absolute path.
|
||||
</p>
|
||||
|
|
|
@ -7,35 +7,52 @@ author: Mirko Ponzio
|
|||
title: mkdir
|
||||
---
|
||||
|
||||
The command <code>mkdir</code> stands for "MaKeDIRectory"</br>
|
||||
<!-- Co-authored with Alessandro Marinelli -->
|
||||
|
||||
<p>
|
||||
The mkdir utility creates the directories named as operands, in the order specified, using mode rwxrwxrwx (0777) as modified by the current
|
||||
umask(2). </p>
|
||||
The <code>mkdir</code> command is used to create new directories<br>
|
||||
The name stands for <i>MaKe DIRectory</i>.<br>
|
||||
|
||||
<h2> options </h2>
|
||||
<p>
|
||||
-m mode
|
||||
The default ls command syntax is:
|
||||
|
||||
Set the file permission bits of the final created directory to the specified mode.
|
||||
The mode argument can be in any of the formats specified to the chmod(1) command.
|
||||
If a symbolic mode is specified, the operation characters ``+'' and ``-'' are interpreted relative to an initial mode of ``a=rwx''.
|
||||
<pre>
|
||||
mkdir [flags] [-m mode] directory_name ...
|
||||
</pre>
|
||||
|
||||
-p
|
||||
Where [flags] are the mkdir <code>-p</code> and <code>-v</code> - flags,
|
||||
read below for more info, and <i>directory_name</i> is the
|
||||
name of the new directory we are going to create.<br><br>
|
||||
|
||||
Create intermediate directories as required. If this option is not specified, the full path prefix of each operand must already exist.
|
||||
On the other hand, with this option specified, no error will be reported if a directory given as an operand already exists.
|
||||
Intermediate directories are created with permission bits of rwxrwxrwx (0777) as modified by the current umask, plus write and search permission for the owner.
|
||||
<h3>Create a new directory</h3>
|
||||
Let's see how to create a new directory:
|
||||
|
||||
-v
|
||||
<pre>
|
||||
ls
|
||||
|
||||
Be verbose when creating directories, listing them as they are created.
|
||||
mkdir test_directory
|
||||
ls
|
||||
test_directory
|
||||
</pre>
|
||||
|
||||
The user must have write permission in the parent directory.
|
||||
</p>
|
||||
<h3>Create a path of directories</h3>
|
||||
Using the flag <code>-p</code> we can create a path of directories, allowing us
|
||||
to build more than a directory at once.
|
||||
|
||||
<pre>
|
||||
mkdir -p test_directory/subdir/subsubdir
|
||||
</pre>
|
||||
|
||||
<h2> syntax </h2>
|
||||
<p>
|
||||
mkdir [-pv] [-m mode] directory_name ...
|
||||
</p>
|
||||
As you can see, we are now creating two directories: one named <i>subdir</i> and
|
||||
another one, included in this, named <i>subsubdir</i><br>
|
||||
The -p flag is necessary to allow the shell to create intermediate
|
||||
directories as required.<br><br>
|
||||
|
||||
<h3>Create directories with specified permissions</h3>
|
||||
The <code>-m mode</code> option allows us to set permissions at the new directory
|
||||
that we are now creating.
|
||||
|
||||
<pre>
|
||||
mkdir -m 777 test_free_directory
|
||||
</pre>
|
||||
|
||||
Our new directory will now have read,write and execute permissions for user, group and others.
|
||||
|
|
|
@ -7,6 +7,7 @@ author: Marco Tereh
|
|||
title: Arrays
|
||||
---
|
||||
|
||||
<p>
|
||||
<!-- add link to page on variables once it exists -->
|
||||
There is a special kind of <a href="#">variable</a>, called an array.<br>
|
||||
|
||||
|
@ -102,4 +103,5 @@ is equivalent to <code>"Mario" "Luigi"</code>, while
|
|||
counts the number of elements in the array, in this case 3.<br>
|
||||
|
||||
<br>
|
||||
Further reading: <a href="http://www.gnu.org/software/bash/manual/bashref.html#Arrays">the bash reference manual</a>
|
||||
Further reading: <a href="http://www.gnu.org/software/bash/manual/bashref.html#Arrays">the bash reference manual</a>
|
||||
</p>
|
||||
|
|
|
@ -8,49 +8,49 @@ title: For Loop
|
|||
---
|
||||
|
||||
<p>
|
||||
|
||||
<!-- Introduction -->
|
||||
The second type of loops are for loops.
|
||||
They follow this sintax:
|
||||
<pre>
|
||||
{% highlight bash %}
|
||||
for [variable] in [list] do
|
||||
[code]
|
||||
done
|
||||
</pre>
|
||||
{% endhighlight %}
|
||||
|
||||
Their purpose is to <i>iterate</i> over a list. Also while loops could do this, you might argue... <br>
|
||||
Of course they could, but for loops are specifically meant to do this. Therefore, for instance,
|
||||
you don't have to decleare your counter variable outside the loop. Most importantly,
|
||||
this variable can be accessed from inside the loop. <br>
|
||||
<br>
|
||||
|
||||
For loops take this form:
|
||||
<pre>
|
||||
{% highlight bash %}
|
||||
for VARIABLE in 1 2 3 4 5 .. N
|
||||
do
|
||||
command1
|
||||
command2
|
||||
commandN
|
||||
done
|
||||
</pre>
|
||||
{% endhighlight %}
|
||||
<!-- End of Introduction -->
|
||||
|
||||
|
||||
<!-- First Example -->
|
||||
Here is a simple example:
|
||||
<pre>
|
||||
{% highlight bash %}
|
||||
for i in 1 2 3 4 5
|
||||
do
|
||||
echo "Welcome $i times"
|
||||
done
|
||||
</pre>
|
||||
{% endhighlight %}
|
||||
|
||||
This first example of code simply displays a welcome message 5 times.
|
||||
The output of this piece of code is:
|
||||
<pre>
|
||||
Welcome 1 times
|
||||
Welcome 2 times
|
||||
Welcome 3 times
|
||||
Welcome 4 times
|
||||
Welcome 5 times
|
||||
Welcome 1 times
|
||||
Welcome 2 times
|
||||
Welcome 3 times
|
||||
Welcome 4 times
|
||||
Welcome 5 times
|
||||
</pre>
|
||||
<!-- End of First Example -->
|
||||
|
||||
|
|
|
@ -8,34 +8,35 @@ title: If Statement
|
|||
---
|
||||
<!-- Introduction -->
|
||||
<p>
|
||||
If statements allow us to make decisions in our Bash scripts. They allow us to whether run or not a piece
|
||||
of code based on a condition that we set. <br>
|
||||
If statements take this form:
|
||||
<pre>
|
||||
if [condition]
|
||||
then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
fi
|
||||
</pre>
|
||||
If statements allow us to make decisions in our Bash scripts. They allow us to whether run or not a piece
|
||||
of code based on a condition that we set. <br>
|
||||
|
||||
If statements take this form:
|
||||
{% highlight bash %}
|
||||
if [condition]; then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
fi
|
||||
{% endhighlight %}
|
||||
|
||||
Anything between <code>then</code> and <code>fi</code> will be executed only if the condtion
|
||||
evaluates to true. <br>
|
||||
Here is a simple example:
|
||||
|
||||
<pre>
|
||||
i=210;
|
||||
{% highlight bash %}
|
||||
i=210;
|
||||
|
||||
if [$i -ge 200]
|
||||
then
|
||||
echo You chose a big number.
|
||||
fi
|
||||
</pre>
|
||||
if [$i -ge 200]; then
|
||||
echo "You chose a big number."
|
||||
fi
|
||||
{% endhighlight %}
|
||||
|
||||
In this first example we evaluate a varibale <code>i</code> to 105. The <i> if statement </i> will print "You chose a big number"
|
||||
only if the number contained in our varibale <code>i</code> is <b>G</b>reater or <b>E</b>qual to 200. <br>
|
||||
In this first example we evaluate a varibale <code>i</code> to 105.
|
||||
The <i> if statement </i> will print "You chose a big number"
|
||||
only if the number contained in our varibale <code>i</code> is <b>G</b>reater or
|
||||
<b>E</b>qual to 200. <br>
|
||||
This is our case, therefore the output of this piece of code will be:
|
||||
<pre>
|
||||
You chose a big number.
|
||||
|
@ -46,39 +47,42 @@ This is our case, therefore the output of this piece of code will be:
|
|||
<!-- If Else -->
|
||||
<h3> If Else </h3>
|
||||
|
||||
Sometimes we want to perform a certain set of actions, if our condtion evaluates to true and another set
|
||||
of actions if our condition evaluates to false. We can do this with the <i> if else </i> statement.
|
||||
<i> If else </i> sattements take this form:
|
||||
<pre>
|
||||
if [condition]
|
||||
then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
else
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
fi
|
||||
</pre>
|
||||
Sometimes we want to perform a certain set of actions, if our condtion evaluates to
|
||||
true and another set of actions if our condition evaluates to false. We can do this with
|
||||
the <i> if else </i> statement.
|
||||
<i> if else </i> sattements take this form:
|
||||
{% highlight bash %}
|
||||
if [condition]; then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
else
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
fi
|
||||
{% endhighlight %}
|
||||
|
||||
Here is a simple example:
|
||||
<pre>
|
||||
i=50;
|
||||
{% highlight bash %}
|
||||
i=50;
|
||||
|
||||
if [$i -ge 200]
|
||||
then
|
||||
echo You chose a big number.
|
||||
else
|
||||
echo You chose a small number.
|
||||
fi
|
||||
</pre>
|
||||
In this example, that is just an extention of the previuous example, we evealuate a variable <code>i</code>
|
||||
to 50. If <code>i</code> is gretaer or equal to 200, you print out "You chose a big number", otherwise,
|
||||
(if <code>i</code> is not gretaer or equal to 200), just like in this case, you print out "You chose a small number".
|
||||
if [$i -ge 200]; then
|
||||
echo "You chose a big number."
|
||||
else
|
||||
echo "You chose a small number."
|
||||
fi
|
||||
{% endhighlight %}
|
||||
|
||||
In this example, that is just an extention of the previuous example, we
|
||||
evealuate a variable <code>i</code> to 50. If <code>i</code> is gretaer or equal to
|
||||
200, you print out "You chose a big number", otherwise,
|
||||
(if <code>i</code> is not gretaer or equal to 200), just like in this case, you print out
|
||||
"You chose a small number".
|
||||
Therefore the output of this piece of code is:
|
||||
|
||||
<pre>
|
||||
You chose a small number.
|
||||
</pre>
|
||||
|
@ -91,64 +95,46 @@ Sometimes, in programming, it is necessary to have a series of condtions that le
|
|||
We can accomodate this need with the <i>if else elif</i> mechanism.
|
||||
The <i>if else elif</i> mechanism takes this form:
|
||||
|
||||
<pre>
|
||||
if [condition]
|
||||
then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
elif [condition]
|
||||
then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
else
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
fi
|
||||
</pre>
|
||||
{% highlight bash %}
|
||||
if [condition]; then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
elif [condition]; then
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
else
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
fi
|
||||
{% endhighlight %}
|
||||
|
||||
Here is a simple example:
|
||||
|
||||
<pre>
|
||||
i=150;
|
||||
{% highlight bash %}
|
||||
i=150;
|
||||
|
||||
if [$i -ge 200]
|
||||
then
|
||||
echo You chose a big number.
|
||||
elif [$i == 150]
|
||||
then
|
||||
echo You chose 150.
|
||||
else
|
||||
echo You chose a small number
|
||||
fi
|
||||
</pre>
|
||||
if [$i -ge 200]; then
|
||||
echo "You chose a big number."
|
||||
elif [$i == 150]; then
|
||||
echo "You chose 150".
|
||||
else
|
||||
echo "You chose a small number"
|
||||
fi
|
||||
{% endhighlight %}
|
||||
|
||||
In this example, that is just an extention of the previuous example, we evealuate a
|
||||
variable <code>i</code> to 150. If <code>i</code> is gretaer or equal to 200,
|
||||
you print out "You chose a big number", if <code>i</code> is equal to 150 you print out
|
||||
"You chose 150" otherwise you print out "You chose a small number".
|
||||
Therefore the output of this piece of code is:
|
||||
|
||||
In this example, that is just an extention of the previuous example, we evealuate a variable <code>i</code>
|
||||
to 150. If <code>i</code> is gretaer or equal to 200, you print out "You chose a big number", if <code>i</code>
|
||||
is equal to 150 you print out "You chose 150" otherwise you print out "You chose a small number".
|
||||
Therefore the output of this piece of code is:
|
||||
<pre>
|
||||
You chose 150.
|
||||
</pre>
|
||||
<!-- End of If Elif Else -->
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</p>
|
||||
</p>
|
||||
|
|
|
@ -53,7 +53,8 @@ echo < $(cat hello.txt)
|
|||
<!-- 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.
|
||||
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
|
||||
|
@ -63,13 +64,15 @@ Moon
|
|||
<!-- 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>
|
||||
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>
|
||||
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
|
||||
|
|
|
@ -8,13 +8,15 @@ title: Script Special Variables
|
|||
---
|
||||
<!-- Intro -->
|
||||
<p>
|
||||
As previously said, there are certain characters that we can not use in the variable-naming process.
|
||||
<br>
|
||||
In this page we will see what actually are those characters, and what's their purpose.
|
||||
<br>
|
||||
As previously said, there are certain characters that we can not
|
||||
use in the variable-naming process.<br>
|
||||
|
||||
In this page we will see what actually are those characters,
|
||||
and what's their purpose.<br>
|
||||
<!-- list of commands-->
|
||||
<h4>$$</h4>
|
||||
To begin, we will see the simplest variable, which is the dollar sign ($). This command simply gives us the process ID number of the current shell.<br>
|
||||
To begin, we will see the simplest variable, which is the dollar sign ($).
|
||||
This command simply gives us the process ID number of the current shell.<br>
|
||||
|
||||
<pre>
|
||||
echo $$
|
||||
|
@ -25,7 +27,8 @@ To begin, we will see the simplest variable, which is the dollar sign ($). This
|
|||
This variable will simply give us the filename of the current script.
|
||||
|
||||
<h4>$n</h4>
|
||||
This variable corresponds to the arguments with which a script was invoked. Here n is a positive number corresponding to the position of an argument.
|
||||
This variable corresponds to the arguments with which a script was invoked.
|
||||
Here n is a positive number corresponding to the position of an argument.
|
||||
|
||||
<h4>$#</h4>
|
||||
This variable gives us the number of arguments supplied to a script.
|
||||
|
|
|
@ -8,12 +8,14 @@ title: Script Variables
|
|||
---
|
||||
<!-- Intro -->
|
||||
<p>
|
||||
A variable is simply a string to which we assign a certain type of data, which could be a text, a number, a filename and other types of data.
|
||||
A variable is simply a string to which we assign a certain type of data,
|
||||
which could be a text, a number, a filename and other types of data.
|
||||
<br>
|
||||
<!-- How to name a variable - text -->
|
||||
<h3>Naming a variable</h3>
|
||||
<!-- Explaination -->
|
||||
To name a variable in Unix we have to use only letters, numbers or the underscore character (_).<br>
|
||||
To name a variable in Unix we have to use only letters, numbers or
|
||||
the underscore character (_).<br>
|
||||
Other characters can't be used because they have a special meaning in Unix Shell.<br>
|
||||
<br>
|
||||
<!-- Examples of naming -->
|
||||
|
@ -42,7 +44,8 @@ Let me show you a simple example:
|
|||
VAR_1=Strawberry
|
||||
</pre>
|
||||
<!-- How to access the variables - text -->
|
||||
To access a variable we have to use the dollar sign ($). So if I want to access VAR_1, I have to write:
|
||||
To access a variable we have to use the dollar sign ($). So if I want to
|
||||
access VAR_1, I have to write:
|
||||
<!-- Examples of accessing -->
|
||||
<pre>
|
||||
VAR_1=Strawberry
|
||||
|
@ -56,7 +59,8 @@ And shell will give us the following result:
|
|||
<!-- How to delete a variable - text -->
|
||||
<h3>Deleting a variable</h3>
|
||||
<!-- Explaination -->
|
||||
Deleting a variable means that shell will remove a certain variable from the list of those that it tracks.<br>
|
||||
Deleting a variable means that shell will remove a certain variable from the list of
|
||||
those that it tracks.<br>
|
||||
To delete a variable we use the following command:
|
||||
<!-- Examples of deleting -->
|
||||
<pre>
|
||||
|
@ -72,7 +76,8 @@ which in our case would be:
|
|||
<!-- How to protect a variable - text -->
|
||||
<h3>Protecting variables</h3>
|
||||
<!-- Explaination -->
|
||||
To protect a certain variable, we can set them as read-only so that it can't be changed or deleted.<br>
|
||||
To protect a certain variable, we can set them as read-only so that it can't be
|
||||
changed or deleted.<br>
|
||||
So, if we try to change the value of VAR_1, the result will be the following:
|
||||
<!-- Examples of protection -->
|
||||
<pre>
|
||||
|
@ -95,5 +100,6 @@ If we try to delete the variable, shell will give us the following value:
|
|||
echo $VAR_1
|
||||
</pre>
|
||||
|
||||
As VAR_1 is read-only, shell will not give us any output, as you can't use the unset command with read-only files.
|
||||
As VAR_1 is read-only, shell will not give us any output, as you can't use the
|
||||
unset command with read-only files.
|
||||
</p>
|
||||
|
|
|
@ -7,127 +7,131 @@ author: Matteo Omenetti
|
|||
title: While Loop
|
||||
---
|
||||
<p>
|
||||
|
||||
<!-- Introduction -->
|
||||
Loops are an important concept in programming and therefore also in scripting. Thanks to loops you are able to repeat an instruction
|
||||
automatically several times, until a certain condition turns false. <br>
|
||||
Loops are an important concept in programming and therefore also in scripting.
|
||||
Thanks to loops you are able to repeat an instruction
|
||||
automatically several times, until a certain condition turns false.<br>
|
||||
|
||||
Two are the main types of loops: while and for. They both generate a repeating piece of code, but with some key differences
|
||||
that make them suitable for different needs while programming. <br>
|
||||
Two are the main types of loops: while and for. They both generate a repeating
|
||||
piece of code, but with some key differences
|
||||
that make them suitable for different needs while programming.<br>
|
||||
|
||||
While loops take this form:
|
||||
<pre>
|
||||
while [condition]
|
||||
do
|
||||
{% highlight bash %}
|
||||
while [condition]
|
||||
do
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
...
|
||||
done
|
||||
</pre>
|
||||
done
|
||||
{% endhighlight %}
|
||||
<!-- End of Introduction -->
|
||||
|
||||
<!-- First Example -->
|
||||
Here is a first simple example:
|
||||
Here is a first simple example:
|
||||
{% highlight bash %}
|
||||
i=0;
|
||||
|
||||
while [$i -lt 4]
|
||||
do
|
||||
echo $i
|
||||
i=$((i + 1))
|
||||
done
|
||||
{% endhighlight %}
|
||||
|
||||
In this first example, you simply create a variable called i and evaluate it to 0.
|
||||
Then you acces the while loop: the condition <code> [$i -lt 4] </code> means that this while
|
||||
loop will run until the <code> i </code> varibale is less than 4.
|
||||
Every cycle of this loop, you print out the value of variable i with <code> echo $i </code>
|
||||
and finally you increase its value by 1 with <code> i=$((i + 1)) </code>.
|
||||
Therefore in 4 cycles the value of i will be 4. This will make the condition of the while loop false.
|
||||
The output of this piece of code is:
|
||||
<pre>
|
||||
i=0;
|
||||
|
||||
while [$i -lt 4]
|
||||
do
|
||||
echo $i
|
||||
i=$((i + 1))
|
||||
done
|
||||
</pre>
|
||||
|
||||
In this first example, you simply create a variable called i and evaluate it to 0.
|
||||
Then you acces the while loop: the condition <code> [$i -lt 4] </code> means that this while
|
||||
loop will run until the <code> i </code> varibale is less than 4.
|
||||
Every cycle of this loop, you print out the value of variable i with <code> echo $i </code>
|
||||
and finally you increase its value by 1 with <code> i=$((i + 1)) </code>.
|
||||
Therefore in 4 cycles the value of i will be 4. This will make the condition of the while loop false.
|
||||
The output of this piece of code is:
|
||||
<pre>
|
||||
0
|
||||
1
|
||||
2
|
||||
3
|
||||
</pre>
|
||||
|
||||
<!-- End of First Example -->
|
||||
</pre>
|
||||
|
||||
<!-- End of First Example -->
|
||||
|
||||
|
||||
<!-- Infinite Loop First Example -->
|
||||
Sometimes it is required to decleare infinite loops for various programming purposes. <br>
|
||||
Here is an example:
|
||||
<pre>
|
||||
i=1
|
||||
<!-- Infinite Loop First Example -->
|
||||
Sometimes it is required to decleare infinite loops for various programming purposes. <br>
|
||||
Here is an example:
|
||||
{% highlight bash %}
|
||||
i=1;
|
||||
|
||||
while :
|
||||
do
|
||||
while :
|
||||
do
|
||||
printf "i=$i\i: Hello World"
|
||||
if [ $i == 3 ]
|
||||
then
|
||||
echo "I love programming"
|
||||
elif [ $i == 5]
|
||||
then
|
||||
echo "I love Bash"
|
||||
elif [ $i == 7 ]
|
||||
then
|
||||
echo "I love this website"
|
||||
elif [ $i == 9 ]
|
||||
then
|
||||
exit 0
|
||||
if [ $i == 3 ]; then
|
||||
echo "I love DrRacket"
|
||||
elif [ $i == 5]; then
|
||||
echo "I love Bash"
|
||||
elif [ $i == 7 ]; then
|
||||
echo "I love this website"
|
||||
elif [ $i == 9 ]; then
|
||||
exit 0
|
||||
i=$((i + 1))
|
||||
done
|
||||
</pre>
|
||||
No termination condition is set for this loop in this example. This type of loop is called infinite loop.
|
||||
The <code> exit </code> statement is used to quit the loop. This loop will iterate for 9 times, then
|
||||
as soon as <code> i </code> becomes equal to 0, the condtition of the last if statement will evaluate to true and the
|
||||
loop will be terminated. <br>
|
||||
The output of this piece of code is:
|
||||
<pre>
|
||||
1: Hello World
|
||||
2: Hello World
|
||||
3: Hello World
|
||||
I love programming
|
||||
4: Hello World
|
||||
5: Hello World
|
||||
I love Bash
|
||||
6: Hello World
|
||||
7: Hello World
|
||||
I love this website
|
||||
8: Hello World
|
||||
9: Hello World
|
||||
</pre>
|
||||
done
|
||||
{% endhighlight %}
|
||||
|
||||
No termination condition is set for this loop in this example. This type of loop
|
||||
is called infinite loop.<br>
|
||||
The <code> exit </code> statement is used to quit the loop. This loop will
|
||||
iterate for 9 times, then
|
||||
as soon as <code> i </code> becomes equal to 0, the condtition of the last if
|
||||
statement will evaluate to true and the loop will be terminated. <br>
|
||||
|
||||
The output of this piece of code is:
|
||||
<pre>
|
||||
1: Hello World
|
||||
2: Hello World
|
||||
3: Hello World
|
||||
I love programming
|
||||
4: Hello World
|
||||
5: Hello World
|
||||
I love Bash
|
||||
6: Hello World
|
||||
7: Hello World
|
||||
I love this website
|
||||
8: Hello World
|
||||
9: Hello World
|
||||
</pre>
|
||||
|
||||
<!-- End of Infinite Loop First Example -->
|
||||
|
||||
<!-- Infinite Loop Second Example -->
|
||||
If you want your shell to hang forever doing nothing you can write out the following infinite loop:
|
||||
<pre>
|
||||
while :
|
||||
do
|
||||
If you want your shell to hang forever doing nothing you can write out the following infinite loop:
|
||||
{% highlight bash %}
|
||||
while :
|
||||
do
|
||||
:
|
||||
done
|
||||
</pre>
|
||||
<!-- End of Infinite Loop Second Example -->
|
||||
done
|
||||
{% endhighlight %}
|
||||
<!-- End of Infinite Loop Second Example -->
|
||||
|
||||
<!-- Read -->
|
||||
In scripting, while loops are often used to process files line by line. <br>
|
||||
Here is an examaple:
|
||||
<pre>
|
||||
while read -r first_name last_name phone;
|
||||
do
|
||||
printf '%s\n' "$last_name"
|
||||
done <!-- Find out how to write "<" --> "$file"
|
||||
</pre>
|
||||
In scripting, while loops are often used to process files line by line. <br>
|
||||
Here is an examaple:
|
||||
{% highlight bash %}
|
||||
while read -r first_name last_name phone;
|
||||
do
|
||||
printf '%s\n' "$last_name"
|
||||
done < "$file"
|
||||
{% endhighlight %}
|
||||
|
||||
The <code> read </code> command is used to read a file line by line. The flag <code> -r </code> is used to tell the
|
||||
command read to intepret backslashes (/) literally, instead as escape characters. This command, expect for some few
|
||||
rare cases, should always be used with this flag.
|
||||
In this example, <code> <!-- Find out how to write "<" --> "$file" </code> redirects the loop's input from a file whose name is stored in a variable.
|
||||
This file has 3 colums, <code> first_name last_name phone </code>, separated by blank space (or a tab).
|
||||
This piece of code only prints out the second column.
|
||||
<!-- End of Read -->
|
||||
|
||||
</p>
|
||||
The <code> read </code> command is used to read a file line by line.
|
||||
The flag <code> -r </code> is used to tell the
|
||||
command read to intepret backslashes (/) literally, instead as escape characters.
|
||||
This command, expect for some few
|
||||
rare cases, should always be used with this flag.
|
||||
In this example, <code> < "$file" </code> redirects the loop's input from a file
|
||||
whose name is stored in a variable.
|
||||
This file has 3 colums, <code> first_name last_name phone </code>, separated by
|
||||
blank space (or a tab).
|
||||
This piece of code only prints out the second column.
|
||||
<!-- End of Read -->
|
||||
</p>
|
||||
|
|
Loading…
Reference in a new issue