Difference between revisions of "ITIC:Working in the shell - Introduction to Bash - Exercises"

From Juneday education
Jump to: navigation, search
(Issuing commands: more exercises)
(Working with text files)
(18 intermediate revisions by the same user not shown)
Line 99: Line 99:
 
== Working with text files ==
 
== Working with text files ==
  
Commands: wc, grep,  
+
=== Preparations ===
 +
 
 +
==== Create a directory structure ====
 +
Before we proceed with the exercises we need to create directories like this:
 +
<source lang="bash">
 +
tig015/
 +
`-- itic
 +
    `-- working-in-the-shell
 +
        |-- 10
 +
        |-- 11
 +
        |-- 12
 +
        |-- 13
 +
        |-- 14
 +
        |-- 15
 +
        |-- 16
 +
        |-- 17
 +
        |-- 18
 +
        |-- 19
 +
        |-- 20
 +
        `-- gutenberg
 +
</source>
 +
 
 +
You can create this directory structure in many ways but we suggest the following command:
 +
<source lang="bash">
 +
mkdir -p tig015/itic/working-in-the-shell/{10..20} tig015/itic/working-in-the-shell/gutenberg
 +
</source>
 +
 
 +
You can check your directory structure like this:
 +
<source lang="bash">
 +
$ tree -d --charset=ascii tig015/
 +
tig015/
 +
`-- itic
 +
    `-- working-in-the-shell
 +
        |-- 10
 +
        |-- 11
 +
        |-- 12
 +
        |-- 13
 +
        |-- 14
 +
        |-- 15
 +
        |-- 16
 +
        |-- 17
 +
        |-- 18
 +
        |-- 19
 +
        |-- 20
 +
        `-- gutenberg
 +
 
 +
14 directories
 +
</source>
 +
 
 +
==== Create a file with text ====
 +
Next thing we should do is to create a file with some text in it. The file, called <code>book.txt</code> shall be created in the <code>tig015/itic/working-in-the-shell</code> folder. The content of the file should be:
 +
<source lang="text">
 +
First of all, we're happy you're using our course material over at wiki.juneday.se.
 +
 
 +
The authors of these exercises are Rikard and Henrik.... and we suck at just about everything.
 +
 
 +
The hardest thing with these exercises was to actually come up with the text for this very text file. But since we're creative, innovative and painters of digital landscapes we're not afraid of trying. So here it is. This is the text we're going to use in some exercises.
 +
</source>
 +
 
 +
If you're not familiar with tools to create a file with the content above here are some instructions:
 +
 
 +
Copy the text content above.
 +
 
 +
In <code>bash</code> enter the directory <code>tig015/itic/working-in-the-shell</code> with the following command:
 +
<source lang="bash">cd tig015/itic/working-in-the-shell</source>
 +
 
 +
In <code>bash</code> assuming you're in the <code>tig015/itic/working-in-the-shell</code> folder type the following command:
 +
<source lang="bash">cat > book.txt</source>
 +
Bash (actually the command <code>cat</code>) is now waiting for text and to redirect that to the file <code>book.txt</code>. 
 +
 
 +
Paste the text you copied earlier to the terminal window. Use a menu in your terminal.
 +
 
 +
Press <code>Ctrl-d</code> (that is first press down the key Ctrl and then press and release d and after this release the Ctrl key).
 +
 
 +
By now <code>bash</code> should by now display the normal prompt ending with a dollar sign (e g <code>hesa@allan:~/tig015/itic/working-in-the-shell$ </code>)
 +
 
 +
To make sure you have correctly created the file you can use <code>ls</code>:
 +
<source lang="bash">
 +
$ ls -al book.txt
 +
-rw-r--r-- 1 hesa hesa 453 sep 12 15:36 book.txt
 +
</source>
 +
 
 +
The date will and the user (<code>hesa</code>) will differ when you execute the ls command.
 +
 
 +
=== Add some real books ===
 +
 
 +
Enter the gutenberg directory (<code>cd tig015/itic/working-in-the-shell/gutenberg/</code>) and issue the following commands (assuming you have [[<code>curl</code>|curl]] installed):
 +
<source lang="bash">
 +
curl -LJO "https://www.gutenberg.org/ebooks/16328.txt.utf-8"
 +
curl -LJO "https://www.gutenberg.org/files/1342/1342-0.txt"
 +
curl -LJO "https://www.gutenberg.org/files/84/84-0.txt"
 +
curl -LJO "https://www.gutenberg.org/ebooks/1080.txt.utf-8"
 +
curl -LJO "https://www.gutenberg.org/ebooks/60271.txt.utf-8"
 +
curl -LJO "https://www.gutenberg.org/files/2701/2701-0.txt"
 +
curl -LJO "https://www.gutenberg.org/files/1952/1952-0.txt"
 +
curl -LJO "https://www.gutenberg.org/files/43/43-0.txt"
 +
curl -LJO "https://www.gutenberg.org/files/1661/1661-0.txt"
 +
curl -LJO "https://www.gutenberg.org/files/11/11-0.txt"
 +
curl -LJO "https://www.gutenberg.org/ebooks/851.txt.utf-8"
 +
curl -LJO "https://www.gutenberg.org/files/98/98-0.txt"
 +
curl -LJO "https://www.gutenberg.org/ebooks/2542.txt.utf-8"
 +
curl -LJO "https://www.gutenberg.org/ebooks/345.txt.utf-8"
 +
curl -LJO "https://www.gutenberg.org/ebooks/1232.txt.utf-8"
 +
curl -LJO "https://www.gutenberg.org/ebooks/13701.txt.utf-8"
 +
curl -LJO "https://www.gutenberg.org/files/46/46-0.txt"
 +
curl -LJO "https://www.gutenberg.org/ebooks/5200.txt.utf-8"
 +
</source>
 +
 
 +
==== Checking your preparations ====
 +
If you enter the directory where you created the <code>tig015</code> directory you should have a directory and file structure as below:
 +
 
 +
<source lang="bash">
 +
$ tree --charset=ascii tig015
 +
tig015
 +
`-- itic
 +
    `-- working-in-the-shell
 +
        |-- 10
 +
        |-- 11
 +
        |-- 12
 +
        |-- 13
 +
        |-- 14
 +
        |-- 15
 +
        |-- 16
 +
        |-- 17
 +
        |-- 18
 +
        |-- 19
 +
        |-- 20
 +
        |-- book.txt
 +
        `-- gutenberg
 +
            |-- 1080.txt.utf-8
 +
            |-- 11-0.txt
 +
            |-- 1232.txt.utf-8
 +
            |-- 1342-0.txt
 +
            |-- 13701.txt.utf-8
 +
            |-- 16328.txt.utf-8
 +
            |-- 1661-0.txt
 +
            |-- 1952-0.txt
 +
            |-- 2542.txt.utf-8
 +
            |-- 2701-0.txt
 +
            |-- 345.txt.utf-8
 +
            |-- 43-0.txt
 +
            |-- 46-0.txt
 +
            |-- 5200.txt.utf-8
 +
            |-- 60271.txt.utf-8
 +
            |-- 84-0.txt
 +
            |-- 851.txt.utf-8
 +
            `-- 98-0.txt
 +
 
 +
15 directories, 19 files
 +
</source>
 +
 
 +
==== Final preparation step ====
 +
 
 +
Now, [https://en.wikipedia.org/wiki/At_Last! At Last!] (this is a really great album!), we should do the last preparation. Enter the <code>tig015/itic/working-in-the-shell/</code>:
 +
 
 +
<source lang="bash">
 +
$ cd tig015/itic/working-in-the-shell/
 +
</source>
 +
 
 +
=== Displaying content ===
 +
Display the content of the file <code>book.txt</code> in the terminal.
 +
<div class="mw-collapsible mw-collapsed">
 +
Expand using link to the right to see an explanation and a suggested solution for how to do this.
 +
<div class="mw-collapsible-content">
 +
<source lang="text">
 +
$ cat book.txt
 +
First of all, we're happy you're using our course material over at wiki.juneday.se.
 +
 
 +
The authors of these exercises are Rikard and Henrik.... and we suck at just about everything.
 +
 
 +
The hardest thing with these exercises was to actually come up with the text for this very text file. But since we're creative, innovative and painters of digital landscapes we're not afraid of trying. So here it is. This is the text we're going to use in some exercises.
 +
</source>
 +
</div>
 +
</div>
 +
 
 +
Display the content of the file <code>gutenberg/1342-0.txt</code> in the terminal.
 +
<div class="mw-collapsible mw-collapsed">
 +
Expand using link to the right to see an explanation and a suggested solution for how to do this.
 +
<div class="mw-collapsible-content">
 +
<source lang="bash">
 +
$ cat gutenberg/1342-0.txt
 +
</source>
 +
'''''Note''': we do not display the content of the file displayed since this files is 13247 lines long.''
 +
</div>
 +
</div>
 +
 
 +
=== Counting ===
 +
How many lines are there in the file <code>book.txt</code>?
 +
<div class="mw-collapsible mw-collapsed">
 +
Expand using link to the right to see an explanation and a suggested solution for how to do this.
 +
<div class="mw-collapsible-content">
 +
Let's use [[<code>wc</code>|wc]] to count the lines. Using the option <code>-l</code> we instruct wc to count lines.
 +
<source lang="text">
 +
$ wc -l book.txt
 +
5 book.txt
 +
</source>
 +
So there are 5 lines.
 +
</div>
 +
</div>
 +
=== Finding text in text ===
 +
 
 +
=== Parts of a a file ===
 +
 
 +
=== Adjusting text ===
 +
 
 +
=== Sorting ===
 +
 
 +
 
 +
Commands: cat, wc, grep, head, tail, tac, tr, sort, uniq
 +
Skip: join
 +
 
 +
Template for exercise solutions:
 +
<div class="mw-collapsible mw-collapsed">
 +
Expand using link to the right to see an explanation and a suggested solution for how to handle this problem.
 +
<div class="mw-collapsible-content">
 +
<source lang="bash">
 +
</source>
 +
</div>
 +
</div>
  
 
== Combining commands with pipes ==
 
== Combining commands with pipes ==

Revision as of 13:04, 13 September 2019

Shells - there are many shells out there, and Bash is one of them

TODO: Write exercises before October 9 2019 (at the very latest).

There are exercises linked below to other parts of this wiki, as a backup (if we don't have time to write new ones) and also as a bonus for the ambitious students, if we do have time to write new ones.

Exercises

Basics

A shell is a program that can be run interactively, e.g. in a terminal (or terminal emulator). Describe the terms (use a search engine, our Swedish compendium or the previous page if you need help):

  • shell
  • terminal
  • command line
  • command line interface

File system navigation

Start a new terminal. Use pwd to notice where you are in the file system. What is this directory called in general terms?

Issue the following command:

$ echo ~

Notice the output from echo. It seems that Bash has expanded the ~ to a path. What path is that?

Change directory to one of the directories that are in your current directory. You use cd to change directory. But what do you need to write after the cd command (as an argument) for it to know what to do?

What happens if you, from any directory, simply issues the command cd without any arguments? Try it.

Change directory again, from your home directory to some other directory. Issue the command pwd. Then issue the command cd - (that is, cd with the argument of a single dash -). Where did you end up? Issue cd - again. Where did you end up this time? Do it again, and again, until you figure it out.

In your home directory, issue the command tree without arguments. What does it list? Next, issue the command pwd again, but give it an argument of one of the directories in your current directory (e.g. Documents, Desktop, Music, Downloads etc - try all of them or if you don't have any of them, use an existing directory as the argument).

In your home directory, use ls with the appropriate flag (or flags) to list all files and directories in a way that shows you which ones are files, and which ones are directories.

Next, issue the command ls (without any arguments) and notice what files and directories are listed. Next, issue the command ls -a and notice what additional files and directories are listed. The additional files and folders you see, look at their names and make a note of what they have in common.

Next, create a directory (you may use mkdir to do this) called .this-is-a-test (note the leading dot in the file name). Run ls without arguments. Is the new directory listed? Use ls -a to list all files again. Was the new directory listed?

Use rmdir to delete the new directory.

Issuing commands

Issuing commands means that you write the name of a command and press Enter. Often, the command needs more information. Such extra information comes in two flavors, options (also known as flags) and arguments.

Options/flags typically start with a dash (the minus sign). Arguments are normal strings. You can have both flags and arguments.

Use ls -l to see what directories you have in your current directory (your home is a good place to do this). The -l (dash lowercase L) is an option (or a flag - that's the same thing).

Use ls to list a directory. The directory is the argument to ls telling it what to list.

Use ls both with a flag (try -a, -l and -L for instance) and an argument of the same directory again. Notice the result of mixing flags and arguments.

Use echo to print the following (notice the different kinds of quotations):

$ echo ~

$ echo $HOME

$ echo "$HOME"

$ echo '$HOME'

As usual, the $ symbolizes the prompt, and shouldn't be written by you.

Create two directories, called first and second. Try the following commands (note the quotes):

$ ls -L first second

$ ls -L "first second"

$ rmdir "first second"

$ rmdir first second

Try to explain what happened. What commands worked, and what commands didn't work? Why do you think that happened?

Next, we'll see how we can temporarily change the environment a command executes in (you could think of it as tricking the command to think it executes in a different setting).

First enter the following:

$ date

What language and regional settings does it look like the date command is using?

Next, issue the following command:

$ echo "$LC_TIME"

Do you think there's a correlation between how date prints the date and time and the output from printing the environment variable LC_TIME?

Now, issue the following commands exactly as we print them below (you may use copy-paste - you can paste into the terminal using Ctrl-Insert or Ctrl-Shift-V):

$ LC_TIME="en_US.UTF-8" date

$ LC_TIME="sv_SE.utf8" date

You can see what locales are installed by typing:

$ locale -a

What happened above was that Bash set the LC_TIME environment variable to a new value temporarily for the date command. You should conclude that the behavior (output) from date relies on the value of the variable LC_TIME.

Working with text files

Preparations

Create a directory structure

Before we proceed with the exercises we need to create directories like this:

tig015/
`-- itic
    `-- working-in-the-shell
        |-- 10
        |-- 11
        |-- 12
        |-- 13
        |-- 14
        |-- 15
        |-- 16
        |-- 17
        |-- 18
        |-- 19
        |-- 20
        `-- gutenberg

You can create this directory structure in many ways but we suggest the following command:

mkdir -p tig015/itic/working-in-the-shell/{10..20} tig015/itic/working-in-the-shell/gutenberg

You can check your directory structure like this:

$ tree -d --charset=ascii tig015/
tig015/
`-- itic
    `-- working-in-the-shell
        |-- 10
        |-- 11
        |-- 12
        |-- 13
        |-- 14
        |-- 15
        |-- 16
        |-- 17
        |-- 18
        |-- 19
        |-- 20
        `-- gutenberg

14 directories

Create a file with text

Next thing we should do is to create a file with some text in it. The file, called book.txt shall be created in the tig015/itic/working-in-the-shell folder. The content of the file should be:

First of all, we're happy you're using our course material over at wiki.juneday.se.

The authors of these exercises are Rikard and Henrik.... and we suck at just about everything.

The hardest thing with these exercises was to actually come up with the text for this very text file. But since we're creative, innovative and painters of digital landscapes we're not afraid of trying. So here it is. This is the text we're going to use in some exercises.

If you're not familiar with tools to create a file with the content above here are some instructions:

Copy the text content above.

In bash enter the directory tig015/itic/working-in-the-shell with the following command:

cd tig015/itic/working-in-the-shell

In bash assuming you're in the tig015/itic/working-in-the-shell folder type the following command:

cat > book.txt

Bash (actually the command cat) is now waiting for text and to redirect that to the file book.txt.

Paste the text you copied earlier to the terminal window. Use a menu in your terminal.

Press Ctrl-d (that is first press down the key Ctrl and then press and release d and after this release the Ctrl key).

By now bash should by now display the normal prompt ending with a dollar sign (e g hesa@allan:~/tig015/itic/working-in-the-shell$ )

To make sure you have correctly created the file you can use ls:

$ ls -al book.txt 
-rw-r--r-- 1 hesa hesa 453 sep 12 15:36 book.txt

The date will and the user (hesa) will differ when you execute the ls command.

Add some real books

Enter the gutenberg directory (cd tig015/itic/working-in-the-shell/gutenberg/) and issue the following commands (assuming you have [[curl|curl]] installed):

curl -LJO "https://www.gutenberg.org/ebooks/16328.txt.utf-8"
curl -LJO "https://www.gutenberg.org/files/1342/1342-0.txt"
curl -LJO "https://www.gutenberg.org/files/84/84-0.txt"
curl -LJO "https://www.gutenberg.org/ebooks/1080.txt.utf-8"
curl -LJO "https://www.gutenberg.org/ebooks/60271.txt.utf-8"
curl -LJO "https://www.gutenberg.org/files/2701/2701-0.txt"
curl -LJO "https://www.gutenberg.org/files/1952/1952-0.txt"
curl -LJO "https://www.gutenberg.org/files/43/43-0.txt"
curl -LJO "https://www.gutenberg.org/files/1661/1661-0.txt"
curl -LJO "https://www.gutenberg.org/files/11/11-0.txt"
curl -LJO "https://www.gutenberg.org/ebooks/851.txt.utf-8"
curl -LJO "https://www.gutenberg.org/files/98/98-0.txt"
curl -LJO "https://www.gutenberg.org/ebooks/2542.txt.utf-8"
curl -LJO "https://www.gutenberg.org/ebooks/345.txt.utf-8"
curl -LJO "https://www.gutenberg.org/ebooks/1232.txt.utf-8"
curl -LJO "https://www.gutenberg.org/ebooks/13701.txt.utf-8"
curl -LJO "https://www.gutenberg.org/files/46/46-0.txt"
curl -LJO "https://www.gutenberg.org/ebooks/5200.txt.utf-8"

Checking your preparations

If you enter the directory where you created the tig015 directory you should have a directory and file structure as below:

$ tree --charset=ascii tig015
tig015
`-- itic
    `-- working-in-the-shell
        |-- 10
        |-- 11
        |-- 12
        |-- 13
        |-- 14
        |-- 15
        |-- 16
        |-- 17
        |-- 18
        |-- 19
        |-- 20
        |-- book.txt
        `-- gutenberg
            |-- 1080.txt.utf-8
            |-- 11-0.txt
            |-- 1232.txt.utf-8
            |-- 1342-0.txt
            |-- 13701.txt.utf-8
            |-- 16328.txt.utf-8
            |-- 1661-0.txt
            |-- 1952-0.txt
            |-- 2542.txt.utf-8
            |-- 2701-0.txt
            |-- 345.txt.utf-8
            |-- 43-0.txt
            |-- 46-0.txt
            |-- 5200.txt.utf-8
            |-- 60271.txt.utf-8
            |-- 84-0.txt
            |-- 851.txt.utf-8
            `-- 98-0.txt

15 directories, 19 files

Final preparation step

Now, At Last! (this is a really great album!), we should do the last preparation. Enter the tig015/itic/working-in-the-shell/:

$ cd tig015/itic/working-in-the-shell/

Displaying content

Display the content of the file book.txt in the terminal.

Expand using link to the right to see an explanation and a suggested solution for how to do this.

$ cat book.txt 
First of all, we're happy you're using our course material over at wiki.juneday.se.

The authors of these exercises are Rikard and Henrik.... and we suck at just about everything.

The hardest thing with these exercises was to actually come up with the text for this very text file. But since we're creative, innovative and painters of digital landscapes we're not afraid of trying. So here it is. This is the text we're going to use in some exercises.

Display the content of the file gutenberg/1342-0.txt in the terminal.

Expand using link to the right to see an explanation and a suggested solution for how to do this.

$ cat gutenberg/1342-0.txt

Note: we do not display the content of the file displayed since this files is 13247 lines long.

Counting

How many lines are there in the file book.txt?

Expand using link to the right to see an explanation and a suggested solution for how to do this.

Let's use [[wc|wc]] to count the lines. Using the option -l we instruct wc to count lines.

$ wc -l book.txt 
5 book.txt

So there are 5 lines.

Finding text in text

Parts of a a file

Adjusting text

Sorting

Commands: cat, wc, grep, head, tail, tac, tr, sort, uniq Skip: join

Template for exercise solutions:

Expand using link to the right to see an explanation and a suggested solution for how to handle this problem.

Combining commands with pipes

Editing the commands line and some other tricks

Exit status

Blobbing and expansion

Introduction to shell expansion

Environment variables

Variables: $ ? PATH HOME LANG USER

Network

We will add exercises here when we have the time. See below for now.

Swedish exercises

Here are links to our Swedish compendium (which contains a lot of Bash), and the companion Swedish exercise PDF (which has a lot of Bash exercises).

Bonus exercises

These are some recommended exercise pages from other parts of this wiki:

Links

Where to go next

The next page is Networks_and_protocols.

« PreviousBook TOCNext »