Professional Documents
Culture Documents
Duc Le
Page 1
9/15/2008
I. UNIX
1. What is UNIX?
-
Todays UNIX system are splited into various branches, developed over
time by AT&T as well as various commercial venders and non-profit
organizations.
As a UNIX user, you have a choice of shells available to you. These are
the Bourne shell, the C shell, the Korn shell.
2. Kernel
3. Shell
Duc Le
All the files in UNIX are grouped together in the directory structure. The
file-system is arranged in a hierarchical structure, like an inverted tree.
The top of the hierarchy is traditionally called root (written as a slash /)
Page 2
9/15/2008
Duc Le
Page 3
9/15/2008
6. Basic commands
Basic commands in UNIX are: mkdir, cd, cp, mv, rm, cat, touch, vi, ls, du, df, pwd, who,
id, chmod, chown, chgrp, top, rlogin, rsh, ssh, ftp, sftp, clear, echo, setenv, tar, gzip, kill,
history, man.
Duc Le
9/15/2008
For more detail about above commands, from terminal, type: man <command>
7. VI editor
Duc Le
enter insert mode, the characters typed in will be inserted after the current cursor
position. If you specify a count, all the text that had been inserted will be repeated
that many times.
Page 5
9/15/2008
enter insert mode, the characters typed in will be inserted before the current cursor
position. If you specify a count, all the text that had been inserted will be repeated
that many times.
replace one character under the cursor. Specify count to replace a number of
characters
undo the last change to the file. Typing u again will re-do the change.
delete character under the cursor. Count specifies how many characters to delete.
The characters will be deleted after the cursor.
:w
Write file
:q
Exit VI editor
II. C-SHELL
1. About C-Shell
-
Duc Le
The C shell (csh) is a developed by Bill Joy for the BSD Unix system. It
was originally derived from the 6th Edition Unix /bin/sh (which was the
Thompson Shell), the predecessor of the Bourne Shell. Its syntax is
modeled after the C programming language. The C shell added many
feature improvements over the Bourne shell, such as aliases and
command history. Today, the original C shell is not in wide use on Unix;
it has been superseded by other shells such as the Tenex C shell (tcsh)
based on the original C shell code, but adding filename completion and
command line editing, comparable with the Korn Shell (ksh), and the
GNU Bourne-Again shell (bash).
Page 6
9/15/2008
The C shell has the typical Unix shell structure: each line of input (or line
of a script file) is interpreted as a separate command to execute, with
backslashes "escaping" newlines where needed (so that multiple input
lines can comprise a single command to be executed).
2. Invoking C-Shell
-
alias
bg
Background execution
break
breaksw
case
cd
Change directory
chdir
Change directory
Duc Le
default
dirs
echo
eval
exec
Page 7
9/15/2008
Duc Le
exit
fg
foreach
glob
goto
hashstat
history
if
Conditional execution
jobs
kill
Signal a process
limit
login
logout
newgrp
nice
nohup
notify
onintr
popd
pushd
rehash
repeat
set
setenv
shift
Shift parameters
source
stop
suspend
switch
Conditional execution
time
Time a command
umask
unalias
unhash
unlimit
unset
9/15/2008
while
Looping control
%job
Foreground execution
Expression evaluation
4. Shell programming
a. Shell script
-
Example:
b. Variable
-
A variable name can consist of only uppercase and lowercase letters and
digits. The name cannot begin with a digit, because names beginning
with a digit are reserved for use by the shell. General usage indicates the
use of all capital letters for the names of environment variables, and all
lowercase letters for local variables, although the shell imposes no such
restriction.
Duc Le
Ex:
Page 9
9/15/2008
set name=a
set name=(a,b,c)
set name[1]=a
unset name
-
Ex:
setenv $DISPLAY pc150:0.0
setenv $PATH .:/usr/local/bin
unsetenv $DISPLAY
Meaning
$name
Replaced with the value of name. It is an error if the ${name} variable name is not defined.
Replaced with the value of elements of array variable ${name[n]}name. For n, write an element
$name[n] number, or a range of element numbers in the form m-n. Use -n to substitute elements 1-n, and mto substitute elements m through the end of the array.
$#name Replaced with the number of elements in array variable ${#name}name.
$?name Replaced with 1 if variable name is set, otherwise 0. ${?name}
d. Array variables
-
Ex:
set name=(a b c)
=> wordlist
set name[1]=a
set name[2]=b
set name[3]=c
Duc Le
Page 10
9/15/2008
Reference the array variable name without an index, the shell replaces
the reference with a wordlist.
Use the reference $name[*] to obtain all the words of the array without
the surrounding parentheses.
The special form $name[n-] refers to the elements of the array beginning
with n and extending through the last.
Replaced with the name of the current shell input file, if known. If unknown, this variable is unset,
and a reference to it is an error.
$?0
$1, $2,
...
Replaced with the value of the shell command's first (second, third,_) argument. If used within a
shell script invoked by name, these symbols refer to the command $9 arguments. Up to nine
arguments can be referenced this way. To reference arguments beyond nine, must use the
reference notation $argv[n].
$*
$$
Replaced with the process number of the parent shell. Used within a shell script, refers to the
process number of the invoking shell.
$<
Replaced with a line of text read from the standard input file.
-
The variables $1, $2, through $9 have special significance when used
inside a shell script, because they refer to the arguments of the
command line that invoked the shell script. The same command
arguments are accessible via the array variable argv. Using the argv
variable, you can refer to all command-line arguments, not just the first
nine. For example, $argv[10] references the tenth argument, and
$argv[$n] references whichever argument is designated by another
variable $n.
f. Operators
- Arithmetic and Logical Operators
Operator Syntax
Duc Le
Operation
Page 11
9/15/2008
~a
Bitwise one's complement. The bits of a are inverted so that 1 yields 0, and 0 yields 1.
!a
Logical negation. If the value of a is zero, the value of the expression is 1; if the value of
a is nonzero, the value of the expression is zero.
a*b
a/b
a%b
Remainder. The value of the expression is the remainder from the integer division of a
by b.
a+b
a-b
<<
a << b
Left shift. Shifts a left the number of bits specified by b. Equivalent to a 2b.
>>
a >> b
Right shift. Shifts a right the number of bits specified by b. Equivalent to a 2b.
<
a<b
>
a>b
<=
a <= b
>=
a >= b
=~
a =~ b
!~
a !~ b
==
a == b
!=
a != b
a|b
a^b
&
a&b
&&
a && b
||
a || b
Logical or. Yields 1 if either a is true or b is true (or both are true); otherwise 0.
Page 12
9/15/2008
Conditional statements
if
if (expr) then
commands
else if (expr) then
commands
else
commands
endif
switch
switch (string)
case pattern:
commands
default:
commands
endsw
-
Iterative statements
while
while (expr)
commands
end
foreach
Page 13
9/15/2008
The C shell provides for two initialization scripts, the .cshrc and .login
files.
.cshrc:
if ($?prompt) then
setenv ARCH `arch`
set hostname = `hostname`
set prompt = "`hostname` YOU% "
set history = 100
set savehist = 100
set filec
source ~/.login
source ~/.aliases
mesg n
xhost +
limit coredumpsize 0
Duc Le
Page 14
9/15/2008
endif
-
.login:
####################################################
setenv OPENWINHOME /usr/openwin
setenv LD_LIBRARY_PATH /usr/local/lib:$OPENWINHOME/lib:/usr/lib
setenv MANPATH .:/usr/share/man:/usr/openwin/man:/usr/dt/man:/usr/local/man
setenv JAVAHOME /usr/java/
####################################################
stty erase '^H'
stty werase '^?'
set path = ( . \
/usr/java/bin \
$JAVAHOME/bin \
/soft/bin \
/usr/ucb \
/bin /usr/bin /usr/sbin \
/usr/local/bin /usr/local/sbin \
$HOME/bin $JAVAHOME/bin \
$OPENWINHOME/bin \
/usr/ccs/bin \
/usr/dt/bin \
/opt/Netscape \
/usr/X11R6/bin \
/sbin \
)
Duc Le
Page 15
9/15/2008
####################################################
7. Job control
&
fg
Duc Le
Page 16
9/15/2008
Ex: stty erase '^H' -> sets the erase key to backspace.
For more detail about this command, type man stty in your terminal.
b. xhost: xhost program is used to add and delete host names or user names to
the list allowed to make connections to the X server.
-
Ex:
For more detail about this command, type man xhost in your terminal.
III. PERL
1. What is Perl?
- Perl is the power and flexibility of high-level programming languages
- Contain control structures and operators similar to C programming
- Ability to write useful programs in a very short time.
- Perl is freeware.
Duc Le
Page 17
9/15/2008
2. Perl script
- A Perl program consists of an ordinary text file containing a series of Perl
commands.
Ex:
#!/usr/local/bin/perl
print Hello world!\n;
\n: new line character.
3. Data types
a. Scalars
-
All Perl variable names, including scalars, are case sensitive. $Name and
$name, for example, are two completely different quantities.
b. Arrays
-
Duc Le
Page 18
9/15/2008
print $machine[2]\n;
result in:
vsi100
vsi102
c. Associative arrays
-
d. File handles
- A file handle behaves in many ways like a variable.
- A file handle can be regarded as a pointer to a file from which Perl is to
read or to which it will write. The basic idea is that you associate a
Duc Le
Page 19
9/15/2008
handle with a file or device and then refer to the handle in the code
whenever you need to perform a read or write operation.
- File handles are generally written in all uppercase. Perl has some useful
predefined file handles:
File Handle Points To
STDIN
STDOUT
STDERR
4. Operators
a. Arithmetic operators
Operator
Operation
Addition
Subtraction
Multiplication
Division
**
Modulus (ex: 7 % 5 is 2)
b. Comparison operators
Duc Le
String operator
Comparison operation
lt
Less than
<
Page 20
9/15/2008
gt
Greater than
>
eq
Equal to
==
le
<=
ge
>=
ne
Not equal to
!=
cmp
Compare, returning 1, 0, or -1
<=>
c. Logical operators
Operator Name
&&
And
||
Or
Not
and
And
or
Or
not
Not
xor
Xor
Name
Result
-e
-e $a
Exists
-r
-r $a
Readable
-w
-w $a
Writable
-d
-d $a
Directory
-f
-f $a
File
-T
-T $a
Text File
e. Assignment operators
Duc Le
Operator
Operations performed
Assignment only
+=
-=
*=
9/15/2008
/=
%=
**=
&=
|=
^=
Ex:
$a *= 2 => $a = $a * 2
$a %= 2 => $a = $a % 2
f. Operator precddence
Operator
Operation Performed
++, --
-, ~, !
**
Exponentiation
=~, !~
Pattern-matching operators
*, /, %, x
+, -, .
<<, >>
Shifting operators
File-status operators
Inequality-comparison operators
Equality-comparison operators
&
Bitwise AND
|, ^
&&
Logical AND
||
Logical OR
..
List-range operator
? and :
Assignment operators
and so on
Duc Le
Comma operator
not
and
Page 22
9/15/2008
or, xor
For
more
operators,
please
refer
http://192.168.100.222/web_books/perl_books/teach_yourself_perl5_21_days/ch
4.htm.
5. Flow control
a. if statement
-
Systax:
if (expr)
{
Statement block
}
elsif (expr)
{
Statement block
}
elsif (expr)
{
Statement block
}
else
{
Statement block
}
Ex:
print Enter your age:;
chop ($age);
if ($age < 18)
{
print "You cannot Vote or have a beer, yet.\n";
}
else
{
print "Go and Vote and then have a beer.\n";
}
Result in:
Enter your age:18
Duc Le
Page 23
9/15/2008
Syntax:
for ( initialise_expr; test_expr; increment_expr )
{
statement(s);
}
Ex:
for ( $i = 1; $i <= 10; $i++ )
{ # count to 10
print "$i\n";
}
Result in:
1
2
3
4
5
6
7
8
9
10
c. while statement
-
Syntax:
while (expr)
{ # while expression is true execute this block
Duc Le
Page 24
9/15/2008
statement(s);
}
-
Ex:
$i = 1;
while ( $i <= 10 )
{ # count to 10
print "$i\n";
$i++;
}
d. until statement
-
Syntax:
until (expression)
{ # until expression is false execute this block
statement(s);
}
Ex:
i = 1;
until ( $i > 10 )
{ # count to 10
print "$i\n";
$i++;
}
e. foreach statement
- Syntax:
foreach localvar (listexpr)
{
statement_block;
}
Duc Le
Page 25
9/15/2008
filevar represents the name you want to use in your Perl program
to refer to the file.
- Ex:
open (TEMP, temp.txt);
open (TEMP, /tmp/temp.txt);
-
Duc Le
File mode:
Read mode
9/15/2008
Ex:
open (TEMP, >temp.txt);
open (TEMP, >>temp.txt);
c. Writing to file
-
Ex:
open (TEMP, >temp.txt) || die (Could not open this file.\n);
print TEMP (Hello world.\n);
Above statement will write to file temp.txt the following content:
Hello world.
d. Closing a file
- When we finish reading or writing a file, we call library function close to
close file.
- Syntax:
close (filevar);
Duc Le
Page 27
9/15/2008
Ex:
close (TEMP);
Perl automatically closes the file when the program terminates or when we open
another file using a previously defined file variable.
7. Pattern matching
A pattern is a sequence of characters to be searched for in a character string. In Perl,
patterns are normally enclosed in slash characters:
/def/
def is a pattern.
If the pattern is found, a match occurs.
a. The match operators
-
Ex:
$result = $var =~ /abc/
$var is searched for pattern abc, if abc is found, $result is
assigned a nonzero value, otherwise, $result is set to 0.
$result = $var !~ /abc/
If abc is found, $result is assigned to 0, otherwise, $result is set to
nonzero value.
Duc Le
+: one or more of the preceding characters. Ex: /de+f/ means def, deef,
deeef,...
Page 28
9/15/2008
[0-9]: matches any digit between 0 and 9. [a-z], [A-Z] match any
lowercase letter, uppercase letter.
^ or \A: match at beginning of string only. Ex: /^def/ matches def only if
these are the first three characters in the string.
$ or \Z: match at end of string only. Ex: /def$/ matches def only if these
are the last three characters in the string.
\B: matches inside word. Ex: /\Bdef/ matches abcdef, but not def.
Description
Range
\d
Any digit
[0-9]
\D
[^0-9]
\w
[_0-9a-zA-Z]
\W
[^_0-9a-zA-Z]
\s
White space
[ \r\t\n\f]
\S
Duc Le
Option
Description
Page 29
9/15/2008
Ignore case
For example, to change all occurrences of abc to def, use the following:
s/abc/def/g
Duc Le
Page 30
9/15/2008
f. Translation operator
- Syntax:
tr/string1/string2/
- Ex:
$string = "abcdefghicba";
$string =~ tr/abc/def/;
So, $string is now: defdefghifed
Options for translation operator
Option
Description
Like any good programming langauge Perl allows the user to define their
own functions, called subroutines. They may be placed anywhere in our
program but it's probably best to put them all at the beginning or all at
the end. A subroutine has the form:
sub mysubroutine
{
print "Somethings are here\n";
}
regardless of any parameters that we may want to pass to it.
b. Parameters
-
Duc Le
Page 31
9/15/2008
Ex:
sub printargs
{
print @_\n;
}
&printargs(arg1, arg2);
Result in:
arg1 arg2
Just like any other list array the individual elements of @_ can be
accessed with the square bracket notation:
Ex:
sub printargs
{
print "$_[0]\n";
print "$_[1]\n";
print "$_[2]\n";
}
&printargs("arg1", arg2, "arg3");
Result in:
arg1
arg2
arg3
c. Returning values
-
Duc Le
Page 32
9/15/2008
{
$_[0];
}
else
{
$_[1];
}
}
$biggest = &maximum(37, 24);
# Now $biggest is 37
d. Local variable
- We can define local variables for using inside subroutines. These local
variables exist only while the subroutine is being executed. When a
subroutine finishes, its local variables are destroyed; if it is invoked
again, new copies of the local variables are defined.
- Ex:
sub printargs
{
local ($a, $b);
($a, $b) = ($_[0], $_[1]);
$c = $a + $b;
}
$sum = &printargs(1, 2);
print "$sum\n";
print "$a\n"; #Empty
print "$b\n"; #Emty
$a = 1;
$b = 2;
print "$a\n";
print "$b\n";
Duc Le
Page 33
9/15/2008
Result in:
3
1
2
9. Some functions
a. chop
-
Syntax:
chop ($var)
Ex:
$input = <STDIN>;
print $input; #print which we input and newline character
chop ($input);
print $input; #print which we input
Result in:
%
Hello
Hello
Hello%
b. split
-
Syntax:
list = split (pattern, value);
Ex:
Ex1:
$line = "This:is:a:string";
@list = split (/:/, $line);
Duc Le
Page 34
9/15/2008
print @list\n;
Result in:
This is a string
Ex2:
$line = "This:is:a:string";
@list = split (/:/, $line,3);
print @list\n;
Result in:
This is a:string
In this example, after three elements have been created, no more new elements
are created.
c. sort
-
Syntax:
@sorted = sort (@list);
- Ex:
@list = ("h", "a", "z", "b");
print "@list\n";
@sorted = sort (@list);
print "@sorted\n";
Result in:
hazb
abhz
d. Reverse
-
Syntax:
@reversed = reverse (@list);
Duc Le
Ex:
Page 35
9/15/2008
Duc Le
Page 36
9/15/2008