MoreBash:Exercises - Tools

From Juneday education
Jump to: navigation, search

Including some exercises

Below is an include of ShellTools:ExercisesExtra:

The exercises here are not intended to be read as something useful, well the first one is. Rather we have written them as challenge exercises for the keen students. We think they will give you some insight in pipes (|) and redirects < and >). And hopefully you will get a laugh and most likely you will sigh.

cat

The following exercises are based around the command cat:

cat > file.txt

What (do you think) will happen if you type the following command:

cat > file.txt

and type some text and finally press control-d.

Expand using link to the right to see an explanation.

Two things are important to separate:

  1. cat is started with no arguments (cat), so instead of reading and outputing text from a file cat opens stdin and outputs the text from there. Think of stdin for now as reading from "the keyboard"
  2. the output from cat, which really is its input, is printed to stdout which in turn is redirected to a file (> file.txt).

So the result from the command above is that all the text you type on stdin will end up in the file file.txt. This means that the commande above is an editor :). Fact is that the idiot author's to these exercises use this trick every day.

cat | cat > file.txt

Type the following:

cat | cat > file.txt

and enter some text (including some enter/return), followed by ctrol-d

What do you think will happen?


Expand using link to the right to see an explanation..

There are a few things are important to separate:

  1. the first cat is started with no arguments (cat), so instead of reading and outputing text from a file cat opens stdin and outputs the text from there. Think of stdin for now as reading from "the keyboard".
  2. | means that the output on stdout from the left command (cat) will become stdin for the right command (cat here as well!)
  3. the second cat is also started with no arguments (cat), so instead of reading and outputing text from a file cat opens stdin and outputs the text from there. stdin in this case is connected to the first cat's stdout
  4. the output from cat, which really is its input, is printed to stdout which in turn is redirected to a file (> file.txt).

So the result from the command above is that all the text you type on stdin will end up in the file file.txt. This means that the commande above is an editor.....but something we've never seen used ;)

cat | cat

Type the following:

cat | cat

and enter some text (including some enter/return), followed by ctrol-d

What do you think will happen?

Expand using link to the right to see an explanation..

There are three things are important to separate:

  1. the first cat is started with no arguments (cat), so instead of reading and outputing text from a file cat opens stdin and outputs the text from there. Think of stdin for now as reading from "the keyboard".
  2. | means that the output on stdout from the left command (cat) will become stdin for the right command (cat here as well!)
  3. the second cat is also started with no arguments (cat), so instead of reading and outputing text from a file cat opens stdin and outputs the text from there. stdin in this case is connected to the first cat's stdout

So the result from the command above is that all the text you type on stdin will end up on stdout. Pretty uselsess isn't it. Well two cat are pretty useless but one is quite useful.

echo cat | cat

Type the following:

echo cat | cat

and enter some text (including some enter/return), followed by ctrol-d

What do you think will happen?

Expand using link to the right to see an explanation..

There are three things are important to separate:

  1. the first command is an echo command (echo cat) which outputs the text "cat" on stdout
  2. | means that the output on stdout from the left command (cat) will become stdin for the right command (cat)
  3. cat is also started with no arguments (cat), so instead of reading and outputing text from a file cat opens stdin and outputs the text from there. stdin in this case is connected to the echo command's stdout

So the result from the command above is that you will see "cat" being printed to the terminal. Speaking about useless stuff, you might think that this is the winner. Well, just you wait and see!

cat | wc -l

Type the following:

cat | wc -l

and enter some text (including some enter/return), followed by ctrol-d

What do you think will happen?

Expand using link to the right to see an explanation.

There are three things are important to separate:

  1. the first cat is started with no arguments (cat), so instead of reading and outputing text from a file cat opens stdin and outputs the text from there. Think of stdin for now as reading from "the keyboard".
  2. | means that the output on stdout from the left command (cat) will become stdin for the right command (cat)
  3. wc is started with an option (wc -l) which tells wc to count lines, so instead of reading and counting lines of text from a file wc -l opens stdin and counts the lines from there. stdin in this case is connected to the cat command's stdout

So the result from the command above is that wc will count the number of lines you type to stdin.

tac

The following exercises are based around the command tac:

tac > file.txt

What (do you think) will happen if you type the following command:

tac > file.txt

and type some text and finally press control-d.

Expand using link to the right to see an explanation.

Two things are important to separate:

  1. tac is started with no arguments (tac), so instead of reading and outputing text from a file tac opens stdin and outputs the text in reverse from there. Think of stdin for now as reading from "the keyboard"
  2. the output from tac is printed in reverse to stdout which in turn is redirected to a file (> file.txt).

So the result from the command above is that all the text you type on stdin will end up in the file file.txt but with all the separate lines having the reversed order. This means that the commande above is an editor :). Fact is that the idiot author's to these exercises use this trick every day.


tac | tac

What (do you think) will happen if you type the following command:

tac | tac

and type some text and finally press control-d.

Expand using link to the right to see an explanation.

Two things are important to separate:

  1. tac is started with no arguments (tac), so instead of reading and outputing text from a file tac opens stdin and outputs the text in reverse from there. Think of stdin for now as reading from "the keyboard"
  2. | means that the output on stdout from the left command (cat) will become stdin for the right command (cat)
  3. the second tac is started with no arguments (tac), so instead of reading and outputing text from a file tac opens stdin (which is the output from the first tac command) and outputs the text in reverse from there.

So the result from the command above is that all the text you type on stdin will end up in the file file.txt but with all the separate lines having the reversed order in reverse. This means that the commande above is an editor :).

rev

The following exercises are based around the command rev:

rev

What (do you think) will happen if you type the following command:

rev

and type some text and finally press control-d.

Expand using link to the right to see an explanation.

One thing to think about:

  1. rev is started with no arguments (rev), so instead of reading and outputing (in revers) text from a file rev opens stdin and outputs the text from there. Think of stdin for now as reading from "the keyboard"

So the result from the command above is that all the text you type on stdin will end up with all the characters reversed per line. This means that the commande above is an editor :). Fact is that the idiot author's to these exercises use this trick every day.


rev | rev

What (do you think) will happen if you type the following command:

rev

and type some text and finally press control-d.

Expand using link to the right to see an explanation.

One thing to think about:

  1. the firt rev is started with no arguments (rev), so instead of reading and outputing (in revers) text from a file rev opens stdin and outputs the text from there. Think of stdin for now as reading from "the keyboard"
  2. | means that the output on stdout from the left command (cat) will become stdin for the right command (cat)
  3. the second rev is started with no arguments (rev), so instead of reading and outputing (in revers) text from a file rev opens stdin and outputs the text line by line but with all characters reversed (per line)

So the result from the command above is that all the text you type on stdin will end up with all the characters reversed per line and then reversed again. So really this outputs the text from stdin (your keyboard) to stdout.

Mixing 'em

To the exercises below we provide no solutions. Instead we encourage you to:

  1. think about what you think is the result of the exercises
  2. test with only one tricky bit (redirect, pipe ...) at a time

Some extra commands you might want to read about are: head, tail and which

rev | rev | tac| tac | cat
echo -e "Liverpool\nAS Roma"
echo -e "Liverpool\nAS Roma" | rev | rev | tac | tac | cat
echo -e "Liverpool\nAS Roma" | head -1
echo -e "Liverpool\nAS Roma" | tail -1
head -2 | tail -2
$ ls -l > /tmp/one-file.txt
$(echo echo cat | sh ) /tmp/one-file.txt

which cat is it?

What (do you think) will happen if you type the following command:

cat $(which cat) | cat  | file -

and type some text and finally press control-d.

Try the following first before you go ahead and answer:

which cat
echo $(which cat)
file $(which cat)

Expand using link to the right to see an explanation.

Two things are important to separate:

  1. The leftmost command (cat) is started with one argument ($(which cat), which means that the output from which cat will be turned into a string). This means that on a computer running Fedora GNU/Linux the leftmost command is cat /usr/bin/cat which will print (to stdout) the content of the file /usr/bin/cat.
  2. | means that the output on stdout from the left command (cat /usr/bin/cat) will become stdin for the right command (cat here as well!)
  3. the second cat is also started with no arguments (cat), so instead of reading and outputing text from a file cat opens stdin and outputs the text
  4. | means that the output on stdout from the left command (cat with no arguments) will become stdin for the right command (cat here as well!)
  5. the command file is also started with one argument (file -), so instead of reading and outputing text about a file the command file - opens stdin and outputs some text about the type of the file content on stdout.

So the result from the command above is about the same as file /usr/bin/cat.

Include ends here

Above was an include of ShellTools:ExercisesExtra.

TODO: Write more exercises.

Links

External links

  • TODO

Source code

  • TODO

Where to go next

Next page is a new topic: MoreBash:Network Tools.

« PreviousBook TOCNext »