Академический Документы
Профессиональный Документы
Культура Документы
SUBDIRECCIÓN ACADÉMICA
DEPARTAMENTO DE SISTEMAS Y COMPUTACIÓN
ACADEMIA DE CIENCIAS COMPUTACIONALES
01/08/2007
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
1. In order to describe the hardware components of a computer comment what are processors,
memories and devices.
2. In order to describe some Solaris operating system components comment what is a File System,
CDE interface and command line interface.
3. In order to describe the SunOS operating system comment what is a Kernel.
4. Log in to the system using CDE, later exit.
5. Log in to the system using the Telnet application from windows, later exit.
6. In order to use the desktop environment log in to the system using CDE and practice chapter 1 to 4
from “Solaris Common Desktop Environment: Guía del usuario”.
1. To construct commands, just write the letters to form the command, for example date, in the
command line interface (CLI). If you committee a mistake, correct with backspace key. As you are
working with bash Shell, you can also construct the commands with up and down keys if these have
been previously entered. You get a command line interface by
2. To execute commands, just press enter key after you write it.
3. Practice with the following commands.
a. In order to get a command line interface (CLI), open a terminal window from your desktop
environment. It is done by pressing double click on bottom one of the mouse in a free
space of your desktop, from the menu display, choose Host item and then Terminal.
b. Command to see the date of the system.
$date
Thu Aug 2 09:50:20 CDT 2007
$finger
(List of users)
$who
(List of users)
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 2/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
$cal
August 2007
S M Tu W Th F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
$cal 2007
2007
$cal 6 1962
June 1962
S M Tu W Th F S
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Note: For command cal, the number 6 or 2007 are arguments. Many commands use arguments to work
properly.
$clear
4. In order to see and use online documentation execute the man command, as an argument write
the command you want to see. Practice for the following commands
a. To see the documentation for the date command.
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 3/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
$man date
Reformatting page. Please Wait... done
NAME
date - write the date and time
SYNOPSIS
/usr/bin/date [-u] [ +format]
/usr/bin/date [ -a [-]sss.fff]
/usr/xpg4/bin/date [ -a [-]sss.fff]
DESCRIPTION
The date utility writes the date and time to standard output
or attempts to set the system date and time. By default, the
current date and time is written.
$man finger
(Output omited for brevety)
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 4/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
1. In order to work with directories, you need to move through them. You use the cd command to
change directories. Directories are arranged in tree order, so each directory can have none or
many children directories but only a father directory. The upper directory is called root and is
represented by “/”. The directory where you are, is called working directory. Use the pwd command
to know what is it. You also have your own directory where you can create, rename or rename files
or directories, this directory is called home directory. You move through directories by their names.
The father directory is call “..”. If you move to any directory beginning where you are, it is said that
the movement was by relative manner and if it was since the root directory, it is said that the
movement was in absolute manner.
2. Practice movements through different directories using the following commands:
$pwd
$cd /
$ls
d. To see with more information about files and directories (long format), use ls command
with option -l
$ls –l
Note: When you see information in long format, you can determine if a file is a directory,a normal file or a
link by seeing the first column. A d represents a directory, a hyphen represents a normal file and an l
represent a link.
e. Now, to see hidden files and directories use the option –a. The hidden files or directories
begin their names with dot.
$ls –a
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 5/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
Note:-a and –l are options for ls command. Many commands have options and these are preceded by the
hyphen sign. To see other options for ls command, execute the man command with argument ls.
f. Now, change to the /var directory and see all information of the files and directories.
Include the hidden files.
$cd var
$pwd
$ls -la
Note: Many options can be joined so –a and –l are the same than –al or –la.
$cd ..
h. Change again to the /var directory, but now in absolute manner, and see the files.
$cd /var
$pwd
$ls -la
i. Try to find bin since the root directory, and determine if it is a file, a directory or link.
j. Try to find ftphosts since the root directory and determine if it is a file, a directory or a link
3. One way to work with files is to see its contents. You see the contents of a file using different
commands like cat, more, head and tail.
4. Practice with the following commands.
$cat passwd
$more passwd
d. Now to see the 5 first lines of the passwd file. 5 is an option of the head command.
head -5 passwd
e. Now to see the 20 last lines of the passwd file. 20 is an option of the tail command
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 6/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
5. For the moment to print files is omitted cause there isn’t a printer connected. But it is done by
using lp command and the name of the file to print.
1. In order to change the directory contents, you need to create, copy, rename, move and remove
files and directories.
2. Let’s begin with the following tasks to create directories:
$cd
b. Use the mkdir command to create a directory. Only the father can create children
directories. Create dir1 and dir2 directories.
$mkdir dir1
$mkdir dir2
$ls –l
$cd dir1
e. Now, create dira, dirb, .dirx and .diry directories. Note that .dirx and .diry will be hidden.
$mkdir dira
$mkdir dirb
$mkdir .dirx
$mkdir .diry
$ls –l
$ls -la
g. Change to the .dirx directory, check the working directory path and verify its contents.
$cd .dirx
$pwd
$ls –l
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 7/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
h. Change to the dir2 directory and check the working directory path.
$cd ../../dir2
$pwd
i. Now, create trash1 and trash2 directories and verify that if was done.
$mkdir trash1
$mkdir trash2
$ls –l
a. Change to the dir2/trash1 directory, check the working directory path and create file1 and
file2 files with touch command.
$cd
$cd dir2/trash1
$pwd
$ls -l
$touch file1
$touch file2
ls -l
Note: One way to create files is using touch command. This command creates a file with a name, an i-node
number and none contents (zero bytes size). Another way is using the output of a command, i.e. redirecting
the output to a file. The symbol used to redirect the output is “>”. The last one is using the vi editor and will
be seeing next module.
b. Change to the dir2/trash2 directory, check the working directory path and create file1 and
file2 files redirecting the output of finger and who commands.
$cd ../trash2
$pwd
$ls -l
$finger > file1
$who > file2
$ls –l
$cd
b. Copy dir2 directory to tmp directory with the cp command. Verify that it was done.
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 8/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
Note: The cp command with option –r is used to copy files recursively, so it is used to copy directories and
of course if a directory has subdirectories, these ones are also copied.
$cd
$cd tmp/trash2
b. Copy file1 file to filefinger file with the cp command. Verify that it was done.
6. It is turn to move (same place) or rename directories, make the following tasks:
$cd
b. Move or rename dir1 directory to newdir1 directory with the mv command. Verify that it
was done.
$cd
b. Move newdir1 directory inside of tmp directory with the mv command. Verify that it was
done.
8. It is turn to move (same place) or rename files, make the following tasks:
$cd
$cd tmp/trash2
b. Move or rename file2 file to filewho file with the mv command. Verify that it was done.
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 9/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
$cd
$cd tmp/trash2
b. Move file1, filewho and filefinger files to tmp/trash1 directory with the mv command.
Verify that it was done.
Note: Observe that file1 in trash1 directory was substituted by the new one.
$cd
$cd tmp
b. Remove trash2 and newdir1 directories with the rmdir command. Verify that it was done.
What happened?
$rmdir trash2
$rmdir newdir1
$ls –l
The directory trash2 was deleted but newdir1 was not. Because newdir1 directory has files
or subdirectories.
Note: Only the father directory can delete the children directories, but these ones must be empty.
c. To remove directories that are not empty, it is use the rm command with option –r.
Remove newdir1 directory with the rm command. Verify that it was done.
$rm –r newdir1
$ls –l
Note: You should be very carefully when using rm –r command because the files and directories once
deleted, they can not be recovered.
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 10/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
$cd
$cd tmp/trash1
b. Delete file1 file, later the rest of files with the rm command. Verify that it was done.
$rm file1
$ls –l
$rm file*
$ls -l
$cd ..
$rmdir trash1
$ls -l
12. In order to work with links, you should know that exist two types: hard links and soft links. Also, you
should know that a file is composed by tree elements: a name, an i-node and its contents. A soft
link, also called symbolic link, is a file that just has name and i-node. Its content is the same of the
file which it points to. To work with soft links, make the following tasks:
a. Change to your home directory and create dirx and diry directories.
$cd
$mkdir dirx
$mkdir diry
b. Change to dirx directory and create filewho file redirecting the output of who command.
Verify its i-node number.
$cd dirx
$who > filewho
$ls –li
Note: The –i option of the ls command is used to show the i-number of a file.
c. Create a soft link with the ln command. Create the filewho.softlink soft link (file) that point
to filewho file. Verify its i-node numbers, size, type and contents. What do you observe?
The i-node number are different but the contents are the same. The filewho.softlink has an
“l” instead of a “-“ in the type file field, indicating that it is a soft link. Also the size of the
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 11/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
filewho and filewho.softlink are different. The soft link file name has an arrow indicating
where the file is.
Note: The –s option of the ln command is used to create soft links of a file. The first argument is the file and
the second is the link. In a soft link, the size of the file is the sum of character composed of the path file
name.
d. Now, create the filewho.new.softlink soft link (file) in the diry directory that point to
dirx/filewho file. Verify its i-node numbers, size, type and contents. What do you observe?
$cd ../diry
$ln –s ../dirx/filewho filewho.new.softlink
$ls –li ../dirx
$ls –li
$cat ../dirx/filewho
$cat filewho.new.softlink
The same as the above incise. But now, the file where the link point to is on another
directory.
e. Everything made to any of the soft links or file itself is reflected to everyone. Change the
contents of the filewho.new.softlink file in the diry directory by the date output. Verify the
contents of dirx/filewho and dirx/filewho.softlink files. What do you observe?
f. But if you delete the file then the links point to a file that does not exist. So you need to
delete the links, too. If you delete a link, nothing happens. Delete filewho.new.softlink file
in the diry directory and verify the contents of dirx/filewho and dirx/filewho.softlink files.
Delete dirx/filewho file and verify the contents of dirx/filewho.softlink file. Delete
dirx/filewho.softlink file. What do you observe?
$rm filewho.new.softlink
$cd ../dirx
$cat filewho
$cat filewho.softlink
$rm filewho
$cat filewho.softlink
$rm filewho.softlink
When filewho.new.softlink is deleted, nothing happens it can be seen the contents of both
files. When filewho file is deleted, it is shown an error even thought the file exists. The error
is because the file pointed to is removed.
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 12/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
13. Now turn to hard links. A hard link is a file that just has name because it is used the same i-node
and contents of the file which is pointed to. It is incremented the link count in the i-node. To work
with hard links, make the following tasks:
a. Change to your home directory. It will be used the dirx and diry directories.
$cd
b. Change to dirx directory and create filefinger file redirecting the output of finger
command. Verify its i-node and link count numbers.
$cd dirx
$finger > filefinger
$ls –li
c. Create a hard link with the ln command. Create the filefinger.hardlink hard link (file) that
point to filefinger file. Verify its i-node numbers, link count, size, type and contents. What
do you observe?
The i-node number, the type, the size and the contents are the same. Both files increment
the link count to two.
d. Now, create the filefinger.new.hardlink hard link (file) in the diry directory that point to
dirx/filefinger file. Verify its i-node numbers, link count, size, type and contents. What do
you observe?
$cd ../diry
$ln ../dirx/filefinger filefinger.new.hardlink
$ls –li ../dirx
$ls –li
$cat ../dirx/filefinger
$cat filefinger.new.hardlink
The same as the above incise. But now, the link count is three.
e. Everything made to any of the hard links or file itself is reflected to everyone. Change the
contents of the filefinger.new.softlink file in the diry directory by the date output. Verify
the contents of dirx/filefinger and dirx/filefinger.hardlink files. What do you observe?
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 13/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
$cat ../dirx/filefinger
$cat ../dirx/filefinger.hardlink
f. When you delete a hard link or the file itself, it only decrements the link count, unless you
delete the last one. Delete filefinger.new.hardlink file in the diry directory and verify the
link count and contents of dirx/filefinger and dirx/filefinger.hardlink files. Delete
dirx/filefinger file and verify the link count and contents of dirx/filefinger.hardlink file.
Delete dirx/filefinger.hardlink file. What do you observe?
$rm filefinger.new.hardlink
$cd ../dirx
$ls –li
$cat filefinger
$cat filefinger.hardtlink
$rm filefinger
$ls –li
$cat filefinger.hardlink
$rm filefinger.hardlink
Only decreased by one the link count, the contents was the same.
$cd ..
$rmdir dirx
$rmdir diry
1. In order to describe the fundamentals of the vi editor, comment its characteristics and some of its
main commands.
2. In order to modify files by using the vi editor, do the following commands:
a. Change to your home directory. In that directory you have permission to write files.
$cd
$vi testingvi
c. Always press the ESC key to change to command mode of vi, so press ESC and then the key
“i” to insert a text. Write the following lines. It doesn´t matter if you committee a mistake.
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 14/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
d. Press ESC key and then the key “o” to open a line below and put in insert mode. Write the
following lines (The first line you write is empty, i.e. press Enter key).
e. Now, you can move through the text by pressing the arrow keys, do some movements.
f. Put at the end of the first line, and press ESC key then the key “a” to add text. Write the
following text.
and use
g. Now, put and the end of text “below” and add a “.”.
h. In order to number the lines, press ESC then “:” and write set number
i. In order to delete the number of the lines, press ESC then “:” and write set nonumber
j. To replace a character, use the “r” command.
k. Put in the letter “c” of the phrase “the O command”. Then press the key “r”, later, type the
capital “C”.
l. To replace many characters, use the “R” command.
m. Put at the beginning of the word “use” in the first line. Then press the key “R”, later, type
“show”
n. To delete character, use the command “X” to delete at the left and the command “x” to
delete to the right.
o. Put in whatever place of the phrase “the o command”. Then press the key “x”, one, two or
three times. Now with the key “X”, one, two or three times. So you delete “the o
command”.
p. To save file and exit, press ESC then write “:wq”. The “w” is for write and “q” for quit.
$cat testingvi
This text is typed to test and show
the i command to insert text.
Command Action
Dd Delete a line a save it to the buffer
Yy Save the line to the buffer
P Insert a line contained in the buffer
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 15/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
5. Create the file EjemploMenu.java with the following text. Comment the commands used.
/*
* EjemploMenu.java
*
* Obtenido del libro “Manual de Referencia Java 2, cuarta edición”,
* para el curso de Programación II.
* 16/05/2007
*/
do {
System.out.println("Ayuda para:");
System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. while");
System.out.println(" 4. do-while");
System.out.println(" 5. for\n");
System.out.println("Elija una opcion:");
eleccion= (char) System.in.read();
} while ( eleccion < '1'||eleccion > '5');
System.out.println("\n");
switch(eleccion) {
case '1':
System.out.println("La sentencia if:\n");
System.out.println("if(condicion) sentencia;");
System.out.println("else sentencia;");
break;
case '2':
System.out.println("La sentencia switch:\n");
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 16/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
System.out.println("switch(expresion) {");
System.out.println("case constante:");
System.out.println("sucesion de sentencias");
System.out.println("break;");
System.out.println(" // ...");
System.out.println("}");
break;
case'3':
System.out.println("La sentencia while:\n");
System.out.println("while(condicion) sentencia;");
break;
case'4':
System.out.println("La sentencia do-while:\n");
System.out.println("do {");
System.out.println("sentencia;");
System.out.println("} while(condicion);");
break;
case '5':
System.out.println("La sentencia for:\n");
System.out.println("for(inicializacion; condicion; iteracion)");
System.out.println(" sentencia;");
break;
}
}
}
$cd
$cd tmp
$touch tmp
$touch tmpa
$touch tmpb
$touch tmpc
$touch tmpab
$touch tmpabc
$touch tmp123
$touch tmp345`[]
$touch tmp456
$touch tmp.123
b. Use the metacharacters with the ls command, practice with the following tasks.
$ls *a
$ls tmp?
$ls tmp[a-b]
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 17/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
$ls tmp*[3-5]?
$ls ??
$ls tmp??
$ls *3
$ls *.*
$set
$set |more
$env
$env|more
$more .bash_history
$ls -l
a. Create the myrights file with the following contents, using the echo command and see the
permissions
Hello everybody
I am testing permissions
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 18/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
$ls –l myrights
$mkdir mynewdir
$ls –ld mynewdir
$cd mynewdir
5. Modify the default permissions: When you create a file or a directory as shown before, they have
default permissions. These permissions are modified only by the root user.
(Omitted)
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 19/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
3. Create three files with hard links and see their i-node number
$echo “This is for testing find command with –I option” > testfind
$ln testfind testfind.lnk
$ln testfind.lnk testfind.link
$ls -li
$find . –inum 1032 –exec cat {} \;
$sleep 6
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 20/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
$sleep 60&
$sleep 60&
$sleep 60&
$bash
$bash
$ps
$ps –ef
$prstat
$pgrep Xsun
$pgrep –fl Xsun
$pgrep –fl Xsesion
$kill 1030
$kill 1020
$pkill sleep
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 21/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
1. In order to manage jobs in the Bash shell, comment what a job is and what is to schedule a job.
2. Schedule a job by practicing the following tasks:
$echo $EDITOR
Or
$EDITOR=vi
$export EDITOR
$tty
/dev/pts/4
$date
f. Use the at command to schedule a job, so the job execute 2 minutes later.
$at 12:03
>echo “This is my job running very good” > /dev/pts/4
CTRL-d
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 22/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
3. In order to describe the Bash shell alias utility, it can comment that aliases are used to simplified a
complex commands. An alias is another way to name a command. You can add aliases in the
command line and they are present while the session is on. They are in .profile shell scripts to be
executed in connection time, while .profile is executed when bash shell start.
4. Practice alias command with the following tasks:
$alias
$alias
$dir
$dir /
$dir /etc
$touch onefile
$ls –l onefile
$rename onefile one.file
$ls –l onefile
$ls –l one.file
$rm one.file
$thisuser
$unalias dir
$alias
$unalias -a
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 23/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
a. As a first example of a shell script, write script1 file with the following contents.
$vi script1
#!/bin/bash
#
# ~/script1
#
# Copyright 2007 by Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales.
#
# First example of a shell script.
# It executes simple commands.
#
# ident "@(#)script1 1.0 9/8/2007 Roberto H.P."
date “+%r”
cat phrase
Just print the hour and the contents of the phrase file.
c. As a second example of a shell script, write script2 file with the following contents.
$vi script2
#!/bin/bash
#
# ~/script2
#
# Copyright 2007 by Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales.
#
# Second example of a shell script.
# Show messages and if the user decides, shows the contents of a file.
#
# ident "@(#)script2 1.0 9/8/2007 Roberto H.P."
clear
echo
echo "Welcome Roberto H.P. to Solaris 10"
echo
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 24/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
e. As a third example of a shell script, write script2a file with the following contents.
$vi script2a
#!/bin/bash
#
# ~/script2a
#
# Copyright 2007 by Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales.
#
# Third example of a shell script.
# Show messages and if the user decides, shows the contents of a file.
# Same script2 but use test command instead of [].
#
# ident "@(#)script2a 1.0 9/8/2007 Roberto H.P."
clear
echo
echo "Welcome Roberto H.P. to Solaris 10"
echo
echo -n "You begin your session at "
date “+%r”
echo -n "Do you wish to see the today’s phrase? (y/n)"
read wish
if test "$wish" = "s" ; then
cat phrase
fi
g. As a fourth example of a shell script, write script3 file with the following contents.
$vi script3
#!/bin/bash
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 25/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
#
# ~/script3
#
# Copyright 2007 by Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales.
#
# Fourth example of a shell script.
# Use environment variables.
#
# ident "@(#)script3 1.0 9/8/2007 Roberto H.P."
clear
echo
echo "Welcome Roberto H.P. to Solaris 10"
echo
echo "Your are connected to $HOSTNAME host"
echo
echo "Your home is $HOME"
echo
echo -n "You begin your session at "
date “+%r”
echo -n "Do you wish to see the today’s phrase? (y/n)"
read wish
if [ "$wish" = "y" ]; then
cat phrase
fi
i. As a fifth example of a shell script, write script4 file with the following contents.
$vi script4
#!/bin/bash
#
# ~/script4
#
# Copyright 2007 by Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales.
#
# Fifth example of a shell script.
# Use functions.
#
# ident "@(#)script4 1.0 9/8/2007 Roberto H.P."
Enter()
{
echo
echo -n "Press any key to continue ..."
read enter
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 26/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
Entery()
{
echo -n "Do you wish to see the today’s phrase? (y/n)"
read wish
}
clear
echo
echo "Welcome Roberto H.P. to Solaris 10"
echo
echo "Your are connected to $HOSTNAME host"
Enter
echo "Your home is $HOME"
Enter
echo -n "You begin your session at "
date “+%r”
Entery
if [ "$wish" = "y" ]; then
cat phrase
fi
k. As a sixth example of a shell script, write script5 file with the following contents.
$vi script5
#!/bin/bash
#
# ~/script4
#
# Copyright 2007 by Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales.
#
# Sixth example of a shell script.
# Use of arguments. It also uses the case structure.
# It can be run as "./script5" or "./script5 none" or "./script5 all".
#
# ident "@(#)script5 1.0 9/8/2007 Roberto H.P."
Enter()
{
echo
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 27/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
Entery()
{
echo -n "Do you wish to see the today’s phrase? (y/n)"
read wish
}
case "$1" in
'all')
clear
echo
echo "Welcome Roberto H.P. to Solaris 10"
echo
echo "Your are connected to $HOSTNAME host"
Enter
echo "Your home is $HOME"
Enter
echo -n "You begin your session at "
date “+%r”
Entery
if [ "$wish" = "y" ]; then
cat phrase
fi
;;
'none')
clear
;;
*)
echo "Usage: $0 { all | none }"
exit 1
;;
esac
exit 0
$cat /etc/rc3.d/S81volmgt
#!/sbin/sh
#
# Copyright 2004 Sun Microsystems, Inc. All rights reserved.
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 28/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
case "$1" in
'start')
if [ -f /etc/vold.conf -a -f /usr/sbin/vold -a \
"${_INIT_ZONENAME:=`/sbin/zonename`}" = "global" ]; then
echo 'volume management starting.'
/usr/sbin/vold >/dev/msglog 2>&1 &
fi
;;
'stop')
/usr/bin/pkill -x -u 0 -z ${_INIT_ZONENAME:=`/sbin/zonename`} vold
;;
*)
echo "Usage: $0 { start | stop }"
exit 1
;;
esac
exit 0
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 29/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
1. In order to archive files, you should know what is an archive file, how to create it, see its contents
and deployed it so you can recover the original files. An archive file is a file that contains other files,
so you can manage only one and in that way you have many files in only one. You use the tar
command with different options to manage it.
2. Before archive files, let’s see the file types.
a. Check the script1 script shell file type, what type is it?
$file script1
b. Now, take care out the execution permission and check the type again, what type is it?
Restore its permissions.
This file is also known as an executable even thought it has not executable permissions. It is
due to the first line contains #!/bin/bash.
c. Let’s try the same, but without the first line. Copy script1 file to script1.tmp file and delete
the first line of this last file. Execute it. Verify its type. Later take care out the executable
permissions, try to execute it. Last, verify its type. Is it run? What type is it in both cases?
Delete script.tmp file.
Put cursor in the first line, press d key twice, then save it and exit (ESC :wq).
$./script1.tmp
$file script1.tmp
$chmod u-x script1.tmp
$./script1.tmp
$file script1.tmp
$rm script1.tmp
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 30/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
Yes, it runs good as script1 do, but the second time it is not, as it should be. In both cases,
the type is ascii.
Note: The file command is used to determine the type of a file. Possible types are ascii, directory, link,
executable, compress, zip and tar.
3. Take present the script shell files done before. Archive files.
a. Create the myscripts archive file with five script shell files
$file myscripts
c. Now, create the myscripts.tar archive file with all script shell files and check its type.
f. Deploy myscripts.tar file in a new directory called dir.scripts and see the script1 file
contains.
$mkdir dir.scripts
$cp myscripts.tar dir.scripts
$cd dir.scripts
$tar –xvf myscripts.tar
$cat script1
$cd
$rm –r dir.scripts
4. There is not exits compression and archive files altogether using the tar command. So first archive
files with tar and later compresses it with any utility. To compress files is shown next module.
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 31/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
a. First, verify the size of script1 (done before) file then compress it with compress command.
Verify the name and the size.
$ls –l script1
$compress script1
$ls –l script1
Note: If you use compress command for compressing files, these ones end with .Z.
$file script1.Z
2. In order to view compressed files, it is used the zcat command if the compressed file was done with
the compress command.
$zcat script1.Z
3. Uncompress files using uncompress command for files compressed with compress command.
a. Uncompress the script1.Z file. Verify the name and the size.
$uncompress script1.Z
$ls –l script1*
a. First, verify the size of script1 (done before) file then compress it with gzip command.
Verify the name and the size.
$ls –l script1
$gzip script1
$ls –l script1*
Note: If you use gzip command for compressing files, these ones end with .gz.
$file script1.gz
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 32/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
5. In order to view compressed files, it is used the gzcat command if the compressed file was done
with the gzip command.
$gzcat script1.gz
6. Uncompress files using gunzip command for files compressed with gzip command.
a. Uncompress the script1.gz file. Verify the name and the size.
$gunzip script1.gz
$ls –l script1*
(Pending)
1. In order to establish a remote login session, you need to be in a login session. Once you have a login
session, you can connect to another system by a remote login session with rlogin command. (it can
also use telnet command as you did it in Module 1).
a. Logging to system 192.168.254.54, use guest as user name and 123pass as password.
$rlogin 192.168.254.54
$ls –l
No, I can not see my files because in this session (window, I am connected to another
system, but the desk top is mine.
2. In order to copy files or directories to and from another system, it is necessary that a file system or
part of it, be mounted in one of them.
a. Previously, the administrator have mounted the /Blade2 file system into /Blade2 directory.
b. Change to the /Blade2 directory, where are you?, what files you see?
$cd /Blade2
$pwd
$ls –l
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 33/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
I am in /Blade2 directory, and there is nothing files. But in reality, I am listing files on
another (Blade2) system.
c. Make a directory, its named will be as your logging name, for instance, 285.
$mkdir 285
d. Now, change to your home directory and copy your script files (done before) to
/Blade2/285 directory. Verify that it is done.
$cd
$cp script* /Blade2/285
$ls –l
$cd /Blade2/285
$ls –l
Note: It is the same to do $cd /Blade2/285 and $ls –l that $ls –l /Blade2/285. The difference is that you do
not change working directory.
e. Now, change to your home directory and copy your tmp directory and its contents to
/Blade2/285 directory. Verify that it is done.
$cd
$cp –r tmp /Blade2/285
$ls –l tmp
$ls –l /Blade2/285/tmp
$cd
$rm script1
$ls –l script1
h. Copy from /Blade2/285 directory this file to your home again. Verify that it is your home.
$cp /Blade2/285/script1 .
$ls –l script1
$rm –r /Blade2/285/tmp
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 34/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
I can manage files and directories as if they were in my system. They are in another system.
It can be done because the administrator gave the permissions.
k. Change to your home directory and as your administrator to unmount the /Blade2 file
system.
$cd
l. Change to the /Blade2 directory, where are you? What files do you see? Where are your
files?
$cd /Blade2
$pwd
$ls –l
I am in /Blade2 directory, and there is nothing files. But now, I am listing files on my own
(Blade1) system. My files remain in another (Blade2) system.
3. Another way to copy files to and from another system is transferring files between systems by using
ftp command. Transferring files can be done between heterogeneous systems, so you can copy a
file from a Solaris system to a Windows one and vice versa. First, transfer files between Solaris
systems.
a. Make a connection to Blade2 (192.168.254.54) system using ftp command, use guest as a
user name and 123pass as password.
$ftp 192.168.254.54
ftp>ls
No, I can not see my files because I am connected to another (Blade2) system by ftp
protocol.
c. Make a directory, its named will be as your logging name, for instance, 285.
ftp>mkdir 285
Note: The 285 directory was created in Blade2 system and it is located in the guest user home directory.
d. Now, change to this directory and copy (transfer or put) your script1 file (done before) to
285 directory. Verify that it is done.
ftp>cd 285
ftp>put script1
ftp>ls
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 35/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
e. Now, Transfer or put your script3 and script3a files (done before), answer “y” for each
question. Verify that it is done.
ftp>mput script3*
ftp>ls
Note: You use “put” to transfer a file from your system to the other one. You use “mput” to transfers
multiple files. In order to get a file or multiple files from the other system to your system, you must use
“get” and “mget” respectively.
g. Now, open a new terminal window with a CLI and in your home directory, delete script1
file.
$cd
$rm script1
$ls –l script1
h. Change to the ftp connection and get a copy of the script1 file.
ftp>get script1
$ls –l script1
ftp>bye
a. In your Windows system, open an MS-DOS Console and change to a directory where you
have permissions to write files. For this practice, it will be use the c:\tmp directory.
b. Make a connection to Blade1 (192.168.254.53) system using ftp command, use your login
name as user name and your password.
C:/tmp>ftp 192.168.254.53
ftp>ls
Yes, I can see them because I am connected to my (Blade1) systems from another
(Windows) system by ftp protocol.
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 36/37
UNIX Essentials Featuring the Solaris 10 Operating System: Exercises
d. Now, copy (transfer or get) your script1 file (done before) to your c:\tmp directory
windows system.
ftp>get script1
e. In order to verify that it is done, in your Windows system, use any Windows Utility and
observe the c:\tmp directory.
f. Now, Transfer or get your script3 and script3a files (done before), answer “y” for each
question.
ftp>mget script3*
g. In order to verify that it is done, In your Windows system, use any Windows Utility and
observe the c:\tmp directory.
h. Where are your copied files in reality?
i. Now, open a new terminal window with a CLI in you Solaris system and in your home
directory, delete script1 file.
$cd
$rm script1
$ls –l script1
j. Change to the ftp connection in your Windows system and put a copy of the script1 file to
Solaris system.
ftp>put script1
k. Verify that it is your home directory in Solaris system. Change to your terminal window.
$ls –l script1
ftp>bye
Instituto Tecnológico de Pachuca. Academia de Ciencias Computacionales. Roberto Hernández Pérez. Agosto 2007. Page 37/37