| |
|
|
| |
|
|
| |
| |
|
|
BA 372 - Linux Lab
This lab contains exercises that familiarize you with the Linux
command-line environment. It is loosely based on two labs developed
by Hanne Munkholm of the IT University of Copenhagen. Following the
IT University licensing of
this material, the materials in this lab are copyright (c)
2007 by Dr. René Reitsma. This material may be distributed only
subject to the terms and conditions set forth in the Open Publication
License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/).
Why do we still use the command line today? Because it is much more efficient for certain things than a point-and-click interface. Also, the Unix/Linux command lines are very powerful and allow complex actions to be carried out with just a few commands. However, command lines do take a bit more effort to get started; you have to learn some commands and you have to master the way in which these commands can be connected into chains that do good things. Nevertheless, when you have learned a certain amount of basic commands, and you are getting the hang of it, you will get a control and understanding of your computer that you usually don’t get in the point-and-click world. It’s your choice. There will always be more to learn...
The program that is displaying the command prompt and that
interprets the commands you type, is called a shell. Many different
shells exist for UNIX/Linux, and they have some differences. Your
default ONID/Linux shell is the so-called cshell (csh).
Another, well-know Linux command shell is Bash or the
”Bourne again shell” (named after the older Bourne shell).
The first command that we often use when logging into a computer for the very first time is a command that tells us what operating system we're running:
uname -a
drwx------
2 your_login students 96 Sep 12 2002 mail
-rw-r--r-- 1 your_login students 0 Feb 23 16:24 mytest
drwxr-xr-x 2 your_login students 96 Feb 14 16:27 public_html
| #!/bin/sh |
Start a bash shell
script (note that bash has a
somewhat different language than csh. csh is not
a great shell for scripting. Hence, many programmers use bash
instead). |
| #
My first shell script. # Tells how much space my home directory occupies. |
Comments |
| echo
"Calculating disk space" echo "" |
Write some text to the
screen |
| cd |
cd to my
home directory |
| USAGE=`du
-sh ./ 2>/dev/null | cut -f1` |
Set a variable USAGE to
the amount of space used in the current directory (du is
a command that reports on disk usage; cut is used
to select the first column returned by du). Notice the `back tics` around the du | cut command. These indicate that the results of the part of the command beteween the ticks must be computed before the right hand side of the assignment is assigned to the left hand side (if you leave them off, the du | cut command itself rather than its results gets assigned to USAGE). |
| echo
"My homedir occupies $USAGE". echo "" |
Do some more writing to
the screen. Notice how the USAGE
variable that got a value assigned in the previous line is now
referenced using the $ sign. |