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

From Juneday education
Jump to: navigation, search
(Issuing commands: more exercises)
(2 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
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.
 
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=
 
=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 ==
 
== File system navigation ==
 +
Start a new terminal. Use <code>pwd</code> to notice where you are in the file system. What is this directory called in general terms?
  
Commands: cd, ls, pwd, tree
+
Issue the following command:
 +
<source lang="Bash">
 +
$ echo ~
 +
</source>
 +
Notice the output from echo. It seems that Bash has expanded the <code>~</code> to a path. What path is that?
  
 +
Change directory to one of the directories that are in your current directory. You use <code>cd</code> to change directory. But what do you need to write after the <code>cd</code> command (as an argument) for it to know what to do?
 +
 +
What happens if you, from any directory, simply issues the command <code>cd</code> without any arguments? Try it.
 +
 +
Change directory again, from your home directory to some other directory. Issue the command <code>pwd</code>. Then issue the command <code>cd -</code> (that is, <code>cd</code> with the argument of a single ''dash'' <code>-</code>). Where did you end up? Issue <code>cd -</code> 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 <code>tree</code> without arguments. What does it list? Next, issue the command <code>pwd</code> 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 <code>ls</code> 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 <code>ls</code> (without any arguments) and notice what files and directories are listed. Next, issue the command <code>ls -a</code> 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 <code>mkdir</code> to do this) called <code>.this-is-a-test</code> (note the leading dot in the file name). Run <code>ls</code> without arguments. Is the new directory listed? Use <code>ls -a</code> to list all files again. Was the new directory listed?
 +
 +
Use <code>rmdir</code> to delete the new directory.
 
== Issuing commands ==
 
== 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 <code>ls -l</code> to see what directories you have in your current directory (your home is a good place to do this). The <code>-l</code> (dash lowercase L) is an option (or a flag - that's the same thing).
 +
 +
Use <code>ls</code> to list a directory. The directory is the argument to <code>ls</code> telling it ''what'' to list.
 +
 +
Use <code>ls</code> both with a flag (try <code>-a</code>, <code>-l</code> and <code>-L</code> for instance) and an argument of the same directory again. Notice the result of mixing flags and arguments.
 +
 +
Use <code>echo</code> to print the following (notice the different kinds of quotations):
 +
<source lang="Bash">
 +
$ echo ~
 +
 +
$ echo $HOME
 +
 +
$ echo "$HOME"
 +
 +
$ echo '$HOME'
 +
</source>
 +
As usual, the <code>$</code> symbolizes the prompt, and shouldn't be written by you.
 +
 +
Create two directories, called <code>first</code> and <code>second</code>. Try the following commands (note the quotes):
 +
<source lang="Bash">
 +
$ ls -L first second
 +
 +
$ ls -L "first second"
 +
 +
$ rmdir "first second"
 +
 +
$ rmdir first second
 +
</source>
 +
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:
 +
<source lang="Bash">
 +
$ date
 +
</source>
 +
What language and regional settings does it look like the <code>date</code> command is using?
 +
 +
Next, issue the following command:
 +
<source lang="Bash">
 +
$ echo "$LC_TIME"
 +
</source>
 +
Do you think there's a correlation between how <code>date</code> prints the date and time and the output from printing the ''environment variable'' <code>LC_TIME</code>?
 +
 +
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):
 +
<source lang="Bash">
 +
$ LC_TIME="en_US.UTF-8" date
 +
 +
$ LC_TIME="sv_SE.utf8" date
 +
</source>
 +
 +
You can see what ''locales'' are installed by typing:
 +
<source lang="Bash">
 +
$ locale -a
 +
</source>
 +
 +
What happened above was that Bash set the <code>LC_TIME</code> ''environment variable'' to a new value temporarily for the <code>date</code> command. You should conclude that the behavior (output) from <code>date</code> relies on the value of the variable <code>LC_TIME</code>.
  
 
== Working with text files ==
 
== Working with text files ==

Revision as of 09:26, 12 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

Commands: wc, grep,

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 »