LAB 2
The .bashrc file
There are many environment variables we can look at and
change. However, we certainly don't want to enter these every time we start a
new shell. There is a special file, named .bashrc, which
is used to store your settings. It is located in the user's home directory. For
example, the .bashrc file for the root user
is in the /root
directory.
Here is a .bashrc file from one of my systems:
The description of the lines is as follows:
1. To
comment a line, precede it with a #
symbol.
2. To
create a variable, use the export
tag.
3. To
create an alias, use the alias
tag (as shown earlier in this chapter).
4.
Control statements are allowed; see the if clause in the
previous screenshot.
5. After
modifying your .bashrc
file, remember to source it using the dot operator as follows:
. .bashrc
Dealing with blanks and special characters in filenames
Linux (and Unix) filesystems were
not originally designed to handle blanks in filenames. This can cause quite a
few problems, as the
shell treats each item after a blank as another
file or parameter. A solution is to use quotes, the backslash, or the Tab key.
The following sections assume the user has
not modified the Bash Internal
Field Separator (IFS)
variable.
How
to do it...
See the following screenshot. I purposely created three "bad" filenames:
1. Run ls -la file with blanks.txt and notice the errors.2. Now
run it again, but enclose the filename in quotes: ls -la "file with blanks.txt";
it will work properly now.
3. Enter
ls -la file
and press Tab. It will escape the
blanks for you.
4. Run
ls -la
special>.txt. Observe the error.
5. Enclose
in quotes as before using the following command: ls -la
"special>.txt"
6. Now
try ls -la
-startswithdash.txt and then try quoting it. Doesn't work, right?
7. Precede
the filename with the
./ operator using the following
command: ls -la ./-starWtswithdash.txt
As you can see, this can also be a problem
if special characters have been used in the filename. Study this one a bit and
it will become clear. Remember the Tab
key; it works really well for just about every case. If the file starts with a
dash, use the ./ operator. It
means to refer to the file in the
current directory.
There's
more...
The issue of blanks and special characters is even more of a problem in scripts. This will be covered in more detail in Chapter 8, Working with Scripts.
Understanding the $? variable
Typically, when a command is run in Linux it performs a
task; it either reports what it did or
indicates an error occurred. An internal return code is also generated, and is
displayed by running the echo $? command.
Note that this must be the very next thing typed after the original command.
Here is a quick example of echo
$?:
1. Run the following command:
ping -c 1 packt.com
2. It
should succeed. Run the following command: echo $?
3. You
should get a 0
for success.
4. Now
run the following command:
ping -cp
5. Run echo
$? again. It should return a non-zero value to indicate failure.
How
it works...
In general, a return of zero means success. A
non-zero return means an error has occurred, and in many cases the code
returned indicates what the error was. Remember this the next time you type a
command, hit Enter, and get the shell
prompt back without anything appearing to happen.
There's
more...
The man and info
pages for a command typically contain an entry showing what the errors mean. If
the man
page is lacking, consult the web.
Redirection and piping
Suppose you run a command, say route, and want to save the
output in a file. The redirection
(>)
operator is used to do this instead of sending the output to the screen.
Let's try some redirection:
1. Enter
ifconfig >
file1.txt. You won't see anything, because the output went into the
file.
2. Run
cat file1.txt.
You should now see the output.
3. This
works the other direction as well, to read from a file run the following
command: sort < file1.txt
4. You
can even do both in one step: sort < file1.txt >
output-file.txt
5. You
can also send the output to another command using the pipe operator. For
example, run route |
grep eth0. The above command would display only the lines from route that contain the phrase eth0.
There's
more...
Here is something that I use
all the time. Say I have written a program in C a long time ago, have several
versions, and want to find the latest one. I could run locate to find them all:
locate crc.c
This might return quite a few lines. How can
I run ls on each file to
find the latest one? By piping the
output into the xargs
command and then ls: locate
crc.c | xargs ls -la
This will now show the time and date of
each file.
This might seem a bit complicated at
first, but if you experiment a little it will become second nature to you.
Sending output from one terminal to another
This is a really handy feature
that is unique to Linux/UNIX systems. It's most useful in scripts but can be
used on the command line as well. If you have a system available try the given
steps.
Getting
ready
You will need two open terminals.
We show how to send the output from one terminal to another
in the following steps:
1. In
one terminal run the tty
command. The output should be something like /dev/ pts/16.
2. In
the other terminal run the route
command. You will see the output in that terminal.
3. Now
run route
again, but now using the command: route > /dev/pts/16
4. The
output will go to that other terminal.
How
it works...
Terminals on Linux systems are devices that have
their own buffer space. By referring to
the device by name you can write to it.
Terminal 1
There's
more...
This feature is even more
useful in scripts, which we will see in Chapter
8, Working with Scripts.
Using the Screen program
Screen
is a full-screen window manager that shares a physical terminal with other
processes (which are usually other terminals/shells). It is normally used when
no other manager or desktop is available, such as on a server. It has a
scroll-back history buffer and also allows
for copy and paste of text between windows.
Getting
ready
The following is a brief list of
some of the many key bindings available with Screen:
f Ctrl + A + ?: It displays a list
of commands and their key bindings f Ctrl + A + C:
It brings up a new window f Ctrl + A + D:
It detaches a window
f Ctrl + A + N: It is used to go
to the next window in the sequence f Ctrl
+ A + P: It is used to go to the previous window in the sequence f
Ctrl + A + # (where # is a number): It is used to
go directly to that window f Ctrl +
A + ": It shows the list of windows; user can select any one by
the number
The following is a list of frequently used commands:
f screen -list: It
shows all of the windows
f screen <program>:
It creates a new window and run that program in it
How
to do it...
An example of running the Screen
utility is as follows:
1. In
a terminal run the screen
-L command.
2. Now
press Ctrl + A and then press C. This
will create another window.
3. Do
this two more times.
4. Try
typing Ctrl + A + 0.
5. Try
Ctrl + A + 3.
How
it works...
In the previous section, step 1 will create a new window, window 0. If you
are running inside a window manager you may notice the title change showing
which window it is.
Step 2 will create another window. After step 3, you will
have 4 windows in total.
When you perform the actions in step 4, you should be
in window 0.
Typing Ctrl + a + 3 will take you to window 3.
There's
more...
Here is a helpful hint, if you
are running only a command line with no desktop, you may want to change your PS1 variable to
something like the following in your .bashrc
file:
export PS1="screen$WINDOW \h \u \w \$ "
Now the prompt will always show which window you are in.
This describes only a small part of what Screen can do. Consult the man page for more information.