IT244: Introduction to Linux/UNIX – Fall 2010 Project 1

advertisement
IT244: Introduction to Linux/UNIX – Fall 2010
Project 1
Due: Monday, 11/24/2010
Project Description
You will create your own login script that will set your shell session
environment every time you log in. This file, called .bash_profile, must
exist in the root of your home directory. On sf05.cs.umb.edu, that would
be /home/<yourloginname>.
Important Information
Use the following information as a resource for completing the project
assignment:
 When a profile script is created, you must source the script in
order to see what it has done to your environment. This is done in
either of two ways: log out and back in (tedious), or use the
command “source <myloginscript>”, or “. <myloginscript”. The
latter is shorthand for the former, and the former is done every
time you log into a new terminal session. There is a space after the
dot in the latter.
 Echoing text without having a newline entered at the end by the
echo command can be achieved by using the –n option.
 Comments are used to document a bash script. Please document
each of the sections of entries in your script. To precede entries
with comments, add a line to the text file just before the first line
of the section you are commenting, and precede comment text
with a ‘#’ sign. The following is an example. Usually the comment
would start at the left most start of the next line:
# This is a comment before an if statement
if [[ $1 && $1 == “somename“ ]]
then
# This is a comment for before the first line
ls –l
1







fi
Creating a new name or new way of acting for commands
o To create a new name for a command, you can say
something like alias moveit=mv. Now whenever you type
moveit followed by a file and a destination path/filename,
the mv command will be used.
o To create a new way of acting, you can say something like
alias cd=”cd ~”. Note that you cannot have a space before
or after the = sign, and you must surround with double or
single quotes because there is a space in the text that is
being assigned as an alias.
In order to display the name of the system that you are on, you
type “hostname”. In order to display the type of system UNIX
system you are on, you type uname. In order to see more
information, you type “uname –a”.
Reading files into a for loop is as easy as:
for file in `ls *`
do
echo $file
done
Remember that for every command that produces an output, you
can use it to the system, and then and use grep either to make
sure if something is in the output, or to grab one or more lines
from the output to process with another command like awk to get
something else or something more specific.
I can use a while loop to count lines of input from a file, or even
words in the file, by counting words in a line and adding up all
lines. I could also just use wc to count lines, words, or characters
in a file or in output that comes to the standard in for wc.
The df command is great for getting disk usage and availability. If
you type df, you will see a multi-line output with a header line that
explains the columns, the last of which is the directory that
represents a certain disk space mounting (filesystem). Therefore,
if you can find a filesystem in the right column, then you can find
disk usage and availability for that filesystem in two of the other
columns.
To echo your own text followed by the output of a command,
followed by more of your own text, followed by more command
2
output, you would type an echo command with embedded
command substitutions as follows:
echo “My home directory: `hostname`; My login: `whoami`
For me, this would print:
My home directory: /home/bmahf; My login: bmahf
Sections that must be added to your login script:
NOTE: For each of the sections you create in your .bash_profile file,
please precede the entry with a one-line comment about that entry.
NOTE: Test everything by running the script each time you add
something. Source the script to see that what was added works. Note
also that each time your source, $PATH will become longer with
redundant information. To see the effect of your login script without
these redundancies, log out and log in, rather than sourcing.
1) Being that you like vim so much, set it as your default command
line editor. This means that, having added this setting and
sourced your script, you can hit <Esc> and then use any of the
commands that you had used before, such as / to search history.
2) Create new names or new ways of acting:
a. “ls” should execute the ls command, showing file type
symbols after file names.
b. “l” should execute the ls command, showing the output of ls.
c. “ll” should do the same, showing the output of “ls –l”.
d. “la” should show output of “ls –a”.
e. “lla” should show output of “ls –la”.
f. “lld” should show output of “ls –ld”.
g. “vi” should start the vim editor (even though that’s already
a soft link in the system, let’s do it as practice).
3) Variables:
a. PATH: Add the directories “.” And “~/bin” to the beginning
of the current PATH variable. Don’t worry about whether
you have a bin directory created in your home directory.
We’re just adding the setting for the case where you did.
i. Note that you want the current variable settings not
to be lost when adding the new directory paths.
ii. Note that PATH entries are separated using “:”.
3
b. CDPATH: Add the directories /usr/local, /usr/src, and
~/bin to the end of the current CDPATH variable.
i. Note that you want the current variable settings not
to be lost when adding the new directory paths. If
there isn’t anything in your CDPATH yet, you should
still do it this way, since the system administrator for
your company may change the generic login script to
set your CDPATH with some necessary settings that
you don’t want to lose.
c. Reset the PS1 and PS2 variables so to change your prompts.
i. PS1 should have hostname, date, username, current
history number, ‘>’. The prompt should look like:
sf05: Sun Nov 14: bmahf: 32>
ii. PS2 has current history number, ‘>’. The prompt
should look like:
32>
d. JAVA_HOME: If you are going to set up Java for a user, their
JAVA_HOME variable needs to be set. Set your JAVA_HOME
variable to be “/tools/jdk-1.6.0_03”.
e. CLASSPATH: Java needs to know where the find the
installed class files. Set the CLASSPATH variable to have “.”,
and /tools/junit-3.8.1/junit.jar. Remember to separate
them with a “:”.
f. EDITOR: Some utilities on UNIX systems allow you to set
the editor that they will use when the do text editing. You
can often set your editor by setting the EDITOR variable. Se
the EDITOR variable to be the full path to the vim command
on the sf05 system.
4) Login Printout:
a. Users often want to be given some information about their
system, home folder, and session each time that they log in.
Do the following to notify yourself about these bits of
information every time that you log into sf05.
i. Add an if statement that tests
1. If this is a Linux box, then,
a. if it is an Ubuntu Linux box, print a very
happy statement about Ubuntu being the
best
4
b. Else, if it’s a Linux box but not Ubuntu,
print a happy statement about Linux
being loads of fun
2. Otherwise, if it’s not a Linux, print an unhappy
statement about it not being Linux.
ii. On a separate line, print the home directory path for
your login account (use the system variable).
iii. Loop through all the files in your home directory and
count the total number of all files, and the total
number of files that are ascii files. Print this out on a
separate line as:
Total files: 36, Total Ascii files: 12
iv. Use df to print out the amount of used and available
space for /home/<yourlogin> on a separate line.
Output should look as follows:
Disk space: used: 56409889, available: 1450987
5
Download