A free Basic UNIX-training with muLinux
To save this exercise on our work-floppy, we insert this into the disk drive,
mount it on /a, and then change to that directory.
/# mount /dev/fd0 /a
/# cd /a
/a#
In- and Output Redirections, Pipes
Usually all operating-systems, the output of commands is directed to the
standard output-device (Terminal, Monitor),
the input, if needed, through the standard input device (keyboard).
Error messages, which are related to faulty commands, are also shown on the
screen.
But sometimes it is necessary to save screen outputs.
output-redirection >
In Unix there is in-/output redirection, which is also available in DOS.
With the both signs < and > it's possible that the output of a
command is directed into a assigned file or that input-data is taken out of a
file.
A simple illustration
/a# ls > directory.txt
reads the contents of the working directory and writes it to
directory.txt.
The file directory.txt is created before the command ls is
executed.
An already existing file with the same name will be overwritten.
If the command which is being executed does not create any output, because for
example, something went wrong while executing or the current directory was empty, then the assigned file
will now exist, but it will be empty!
We want to be sure that everything is fine.
Firstly, with ls we want to see if the file exists.
Then we look at the file directory.txt:
/a# cat directory.txt
Great!
Using this simple command, I have created most of the screenoutputs for this Unix-training manual.
Lets try another command:
/a# date > date.txt
Now we already created two text-files.
The command cat has a special status by in- and output-redirections.
This command can concatenate more files at the same time:
/a# cat directory.txt Datum.txt > EverythingTogether.txt
Now we have added a third file, which hosts both other files.
And another variant:
/a# ls > /dev/null
What happened? Nothing!
/dev/null is the Trash-bin in Unix.
What gets there is gone.
But why is this useful?
This redirection is popular for testing programs,
which means, getting error-messages on the screen, while the actual output is
ignored.
We also can redirect outputs onto devices.
/a# date > /dev/tty3
To see the effect we press [Alt] [F3].
Like this we change to console 3 (tty3).
There we see the output of date
Paste to a file >>
While redirecting to a file, an existing file with the same name will be
overwritten.
But when the dataflow needs to be pasted to a file, or appended to the current
file without over writting it, we need two of these
> -signs.
/a# date >> date.txt
Here the actual date is pasted to the file.
We can test this easily by calling this command a few times.
For this of course we use the command-history (cursor-up).
With this command we can easily create a log-file.
When this command is linked to a certain event, like the execution of
another command, this action is logged with the date.
Redirecting error-messages 2>
For testing programs, logging errors can be important.
For this we use 2>
Lets look at an example:
/a# cd Partytime 2> error.txt
/a# cat error.txt
cd: can't cd to Partytime
Redirecting keyboard-input into a file
We want to create a file, which is filled immediately with the signs given on
the keyboard.
This command-variant actually is a simple text-editor.
And indeed, in the early days when almost no Unix-editors where available, this was the
way of creating files.
/a# cat > Textfile01.txt
All the lines you enter now will be written to Textfile01.txt.
Like also this text.
A new line can be acheived by pressing [enter].
To end the text entry, we need to give the keys [ctrl] [d] on an empty line:
[ctrl] [d]
/a#
With the command ls we can see if this file is really created.
With cat Textfile01.txt or less
Textfile01.txt we can also control the contents.
Output-redirection <
When we want to redirect the contents of a file into a standard input of a
command, we use following syntax:
command < filename
The command sort creates a sorted output.
We can redirect the file /etc/passwd to it, to get a sorted output:
sort < /etc/passwd
Pipes |
What is a pipe?
Even if your 'head' sometimes start to smoke with Unix, the pipe has nothing to do
with a tobacco-pipe.
The pipe has to be interpreted as pipeline.
Commands can be combined with the pipeline |, where the output of the
previous command makes the input of the next command.
command1 | command2
Of course the next command needs to be able to handle this data flow.
Usually these are commands which work like filters.
Such a filter is more, which allows an easy listing of data on
the screen.
/a# ls -l | more
This command, in muLinux is terminated with q.
Every time you expect a long output, you can use more.
For example with dmesg.
dmesg gives the kernel-messages.
This provides info about the used hardware.
Because the output is quite long, with more we can more easily control
the output information, leading to greater viewing comfort.
/a# dmesg | more
A similar function is the command less.
This one you can close with q like more.
/a# dmesg | less
A very common used command is grep.
grep searches in the dataflow all the signs which contain a
certain signs-chain, and then outputs these lines.
We only want to be informed about the memory, we filter out the line containing
the word "memory".
/a# dmesg | grep "Memory"
Another example:
/a# ls -l | grep "1999"
All files and directories which are created in 1999, are listed.
And another example:
/a# cat /etc/passwd | grep "root"
All the lines which contain the word "root" are filtered out.
Of course this dataflow can be redirected again. For example into a file:
/a# cat /etc/passwd | grep
"root" > password.root.txt
By the way, in Unix, filenames with more dots are allowed.
A filter to sort, is the already known command sort.
/a# cat /etc/passwd | sort
All lines are sorted by the first letter.
But if the file /etc/passwd is too big, it's recommended to use the
commands less or more:
/a# cat /etc/passwd | sort | less
The filter wc word count, counts words and signs.
/a# cat /etc/passwd | wc
To finish this exercise we unmount our work floppy again.
/a# cd /
/# umount /a
/#
Robert.Warnke@giso.de (copyleft) Robert Warnke, Berlin (Germany) - You can write me in English. | http://rowa.giso.de | translated by mek, corrected by Alex Wild
|