Upload
others
View
5
Download
0
Embed Size (px)
Citation preview
Information Technology Skills Lab Manual (PART I)
Lab1: Experimenting common commands and exploring file system Lab1- Part1 1. Try the following command sequence:
echo hello world
passwd
date
hostname
arch
uname -a
dmesg | more(you may need to press q to quit) uptime
who am i
who
id
last
finger
w
top (you may need to press q to quit) echo $SHELL
echo {con,pre}{sent,fer}{s,ed}
man "automatic door"
man ls (you may need to press q to quit)
man who (you may need to press q to quit) who can tell me why I got tiered
lost
clear
cal 2000
cal 9 1752(do you notice anything unusual?)
bc –l (type quit or press Ctrl-d to quit) echo 5+4 | bc -l
yes please(you may need to press Ctrl-c to quit) time sleep 5
history
Lab1- Part2
2. Try the following command sequence:
cd
pwd
ls -al
cd .
pwd (where did that get you?)
cd ..
pwd
ls -al
cd ..
pwd
ls -al
cd ..
pwd (what happens now)
cd /etc
ls -al | more
cat passwd
cd -
pwd
3. Continue to explore the filesystem tree using cd, ls, pwd and cat. Look in /bin, /usr/bin,
/sbin, /tmp and /boot. What do you see?
4. Explore /dev. Can you identify what devices are available? Can you identify your tty (terminal) device
(typing who am i might help); who is the owner of your tty (use ls -l)?
5. Explore /proc. Display the contents of the files interrupts, devices, cpuinfo, meminfo
and uptime using cat. Can you see why we say /proc is a pseudo-filesystem which allows
access to kernel data structures?
6. Change to the home directory of another user directly, using cd ~username.
7. Change back into your home directory.
8. Make subdirectories called work and play.
9. Delete the subdirectory called work.
10. Copy the file /etc/passwd into your home directory.
11. Move it into the subdirectory play.
12. Change into subdirectory play and create a symbolic link called terminal that points to your tty
device.
13. Experiment with the options on the ls command. What do the d, i, R and F options do?
Lab2: Practicing vi Editor
Remember The vi editor uses "modes" The easiest thing to do if you get confused in vi is to press the ESCape key a couple of times and
start over. With what you were doing.
To begin do:
$ cd
$ touch temp.txt
$ vi temp.txt
Press the "i" key to switch to input mode.
Type something like, "VI is great! I think I'll be using vi from now
on instead of Word” Press <ENTER> to add lines.
Type some more text
Save the file that you are in. To do this do:
Press the ESCape key for command mode
Type “:wq” to save and quite the file
Copy a large file to your home directory so that you can play around with some more vi
commands. Name it testfile
Edit the file, but let's start at the bottom of the file:
$ vi + testfile
Go to the first line of the file:
:1
Go to line 10, add a new line, and add in some text:
:10
Press the “o” key
Add the following text:
##
## A sample comment
##
Delete the three lines you just created:
Move to the first line of new text
Press the ESCape key
Press “dd” to delete a line, repeat until the text is gone Practice
Copying and pasting text.
Go to line 10, copy 10 lines of text, go to the bottom of the file, and place the text there:
ESC
:10
10yy
G
p
Under what we just did:
u
Go to the top of the file, choose a word to replace by another for example if we replace all
occurrences of “YES” with “NO”, but prompt for each change we should use the following:
ESC
:%s/YES/NO/gc
Say “yes” or “no” to a few prompts then escape from this mode by pressing ctrl-c and <ENTER>.
Go to line 1, search for a specific word for example here if we search the word “named”, move
to the end of the line, add some text:
ESC
:1
/named
$
i
“text here”
ESC
Practice some of the following items:
Moving around:
By word
End of line
Start of line
Top of file
Bottom of file
To an absolute line number
Copying and pasting multiple lines (use vi commands)
Copying and pasting single lines (use vi commands)
Copying and pasting multiple lines (use your mouse buffer)
Copying and pasting single lines (use your mouse buffer)
Search for items backwards and forwards
Replacing text
And, anything else you wish to practice.
Lab3: Users and access permission The traditional permissions model in Linux/UNIX is simple—it is based on four access types, or rules. The
possible access types are
(r) Read permission
(w) Write permission
(x) Execute permission
(-) No permission or no access
In addition, these permissions can be applied to three classes of users. The classes are
Owner The owner of the file or application
Group The group that owns the file or application
Everyone All users
UNIX OCTAL FILES PERMISSIONS
The permissions for each user type can be represented by an octal value. Each type of permission carries
with it a value:
4 r read
2 w write
1 x execute/cd
Putting these together in combination yields an octal number from 0 to 7. For example, read (4)
and execute (1) permissions together are represented by 5 (4+1)
Lab3-Part1 (Change Mode)
1. Let’s take a closer look at the contents of a sample directory by typing the command ls -l
$ cd
$ ls -l
What do you see?
2. Create an 3 empty files :
$ touch testfile.sh
$ touch testfile1.sh
$ touch testfile2.sh
What are the permissions for these files (User/Group/Others)?
3. What happens after each following command :
$ chmod o=w testfile.sh
$ chmod g= testfile1.sh
$ chmod u=rw,g=rw,o=rw testfile2.sh
4. Modify testfile.sh with vim and write :
echo “This is current month calendar :” cal
Execute this script : $ ./testfile.sh What do you see?
$ chmod u+x testfile.sh
Repeat script execution, what happens?
$ chmod u-x testfile.sh
Repeat script execution, what happens?
5. Try the following command sequence :
ls –l
$ chmod 777 testfile.sh
$ ls –l testfile.sh
$ chmod 700 testfile.sh
$ ls –l testfile.sh
$ chmod 610 testfile.sh
$ ls –l testfile.sh
$ chmod 630 testfile.sh
$ ls –l testfile.sh
6. What command, we must use to have the following permissions for testfile.sh : - rw- rwx r--
Lab3-Part2 (User Mask)
1. Try the following command :
$ umask
What do you see?
Create an empty file named myfile.txt :
$ touch myfile.sh
Use ls command to display myfile.sh permissions.
Create a directory named mydirectory :
$ mkdir mydirectory
Use ls command to display myfile.sh permissions.
2. Change the default umask value :
$ umask 000
Create an empty file named myfile1.txt :
$ touch myfile1.sh
Use ls command to display myfile.sh permissions.
Create a directory named mydirectory1 :
$ mkdir mydirectory1
Use ls command to display myfile.sh permissions.
Delete myfile1.sh and mydirectory1
$ rm myfile1.sh
$ rm –r mydirectory1
3. Repeat previous steps (2.) with umask = 077 What do you see with this umask value?
4. What umask value we must set to have a default file permission equal to r-xr-xr-x ? With this
value what is the default directory permission?
Lab3-Part3 (System Environment)
1. Switch to C-Shell :
$ csh
2. An example of an environment variable is the OSTYPE variable. The value of this is the current
operating system you are using. Type :
$ echo $OSTYPE
Try the same command with the following variables:
USER (your login name)
HOME (the path name of your home directory)
ARCH (the architecture of the computers processor)
DISPLAY (the name of the computer screen to display X windows)
PATH (the directories the shell should search to find a command)
3. Try the following command :
$ printenv | less
$ env | less
What do you see?
4. Create and set a value to your personal environment variable MYVAR :
$ setenv MYVAR “Information Technology Student”
Display the variable content: $ echo $MYVAR
Lab4: Line and parameter splitting
When you type
$ /bin/echo hello world
then the following things happen:
1. the shell splits this into three words: "/bin/echo", "hello" and "world", using spaces as the separator
2. the shell performs various types of expansion on these words (see below)
3. it starts a new process, runs /bin/echo in it, and passes in "hello" and "world" as arguments 4. this
program executes. /bin/echo just sends it arguments back down its standard output, separated by a
space, and ending with a newline; normally we'd be running something more useful! You can show this
is the case by putting in lots of spaces:
$ /bin/echo hello world
hello world
The shell has still split this into three words, "/bin/echo", "hello" and "world", and echo has joined them
together with a single space. Where this becomes a problem is if you need to use a filename which contains
a space (which are usually best avoided, but you will come across them from time to time). If you try this: $ touch my file $ ls –l
you should find that you have actually created *two* files, called "my" and "file". So you need to stop the
shell from breaking them into separate arguments. You can do this using quoting, and there are three main
ways:
$ touch "my file"
$ touch 'my file'
$ touch my\ file
$ ls –l
The first two are enclosing the filename in either single or double quotes; the last is to precede the space
with a backslash, which makes it lose its special meaning as an argument separator. This also gives you a
way to make echo give you a string with lots of spaces in it:
$ echo "hello world" hello world Argument expansion
The shell performs additional processing on arguments; it's very useful, but it can cause strange behaviour
if you're not aware of it. Try the following command: $ cd /usr/bin $ echo *what* is your name?
Here the shell is doing pattern matching against files in the filesystem, otherwise known as "filename
globbing". The character "*" matches any 0 or more characters in the filename. So, "\*what\*" looks in the
current directory for all files whose names contain the string 'what' anywhere in the middle, and replaces
it with those filenames. You might know something similar from the DOS/Windows world, called wildcards:
* and ?
$ echo "*what* is your name?" *what* is your name?
Globbing is really useful; for example you can delete all files in the current directory with names ending
.txt just by typing "rm *.txt" Filename globbing uses these special characters:
* matches any 0 or more characters
? matches any 1 character
[abc] matches 'a', 'b' or 'c' only
[a-z] matches any character between 'a' and 'z' inclusive
Try $ ls *
$ ls ???
$ ls [moq]*
$ ls [b-f]*
Parameter expansion
Arguments which contain $ are subject to further expansion. There are actually a lot of things you can do
with this, but the most common example is substitution of environment variables, as we saw before with
$PATH. $ echo $HOME
/home/yourname
There are some special shell variables, the most useful being $? which gives the exit status of the last
command (0=success, >0=failure) and $$ which gives the process id of the shell itself. $ echo $$ 2302
However you can do other fancy things, including using the output of one command and inserting it in the
command line as an argument to another. There are two syntaxes for this, $(...) and backticks (Backquote).
For example, we can run 'wc' to count the lines in a file, and use the result as an argument to another
command, such as 'echo' for testing.
$ echo $(wc -l /etc/passwd)
24 /etc/passwd
$ echo `wc -l /etc/passwd`
24 /etc/passwd
(If using the second form, make sure you use backticks (`), not apostrophes. If you use apostrophes then
you've just quoted the string, and it will be echoed back to you as-is) And argument expansions can even
perform basic arithmetic, although it's very rarely used:
$ echo $((3+4))
7
This sort of parameter expansion can be disabled by quoting, except that double quotes don't disable it;
single quotes and backslash do. So: $ echo "Home is $HOME" Home is /home/yourname
$ echo 'Home is $HOME'
Home is $HOME
$ echo Home is \$HOME Home is
$HOME
I/O redirection
try
$ echo "hello" >test.txt
But you can also append to a file, instead of overwriting it:
$ echo "hello" >>test.txt
$ echo "hello" >>test.txt
Equally you can connect the standard input of a process to read from a file:
$ less < test.txt
(should show three lines of 'hello')
Command grouping
The shell lets you run multiple commands on the same line. Some examples are:
1. Run commands one after the other $ echo hello; echo world
hello
world
2. Run command only if the preceding command succeeded $ echo hello && echo world
hello
world
3. Run command only if the preceding command failed $ echo hello || echo world
Hello
4. Run a group of commands in a subshell. They can share I/O redirection. $ (echo hello; echo world) > out.txt
Again, if you need to use these special symbols as part of an argument (say they are in a filename), you can
quote them.
$ echo "hello; echo world"
hello; echo world
Quoting summary
This is by no means a complete list of shell features, but it should be clear by now that lots of characters
have special meanings to the shell. If you are using a filename which contains anything other than letters
and numbers, dot, dash and underscore, you'd be wise to quote it to make sure nothing unexpected
happens! And you should try to avoid creating files whose names contain special characters (space,
question mark, asterisk, ampersand, tilde etc) in the first place, because of the confusion they could cause
to others.