0% found this document useful (0 votes)
15 views

CSCI322 2

Computer Science

Uploaded by

Thet Myat Noe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

CSCI322 2

Computer Science

Uploaded by

Thet Myat Noe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 68

CSCI322

SystemsAdministration
School of Computing and Information Technology
University of Wollongong

Courseware developed with input from Daniel Saffioti and William Tibben
What is an operating system?

• The executive manager that manages all of the


hardware and all of the software.
– Memory manager
– Process manager
– Device manager
– File manager
– Network manager
– User command interface
An Operating System (OS) is the main software that manages all the hardware and software on a computer. Think of it as the executive manager of your computer—it handles everything
behind the scenes to make sure programs run smoothly and hardware works properly. Here’s a breakdown of its key roles:

1. Memory Manager
Controls and tracks the use of computer memory (RAM).
Allocates memory to programs and frees it up when it's no longer needed.
2. Process Manager
Manages the CPU's time and decides which program runs and for how long.
Keeps track of active processes and handles their execution.
3. Device Manager
Manages communication between the computer and connected hardware (like printers, keyboards, or hard drives).
Allocates and deallocates devices to programs as needed.
4. File Manager
Handles the creation, deletion, reading, and writing of files.
Manages access to files and ensures security and organization in storage.
5. Network Manager
Manages network connections, enabling communication between computers.
Handles data transfer over the internet and local networks.
6. User Command Interface
Provides a way for users to interact with the computer, either through a Graphical User Interface (GUI) (like Windows or macOS) or a Command Line Interface (CLI) (like Linux terminal).
In Short:
The Operating System is like the boss of your computer, coordinating all activities between hardware and software to ensure everything functions properly and efficiently.
The UNIX operating system

• A portable operating system


• A multi-tasking operating system
• A multi-user computer operating system in a time-
sharing configuration
• Provides a standard command-line interface
Friction between UNIX and Linux

• Linux was developed in 1991 in Finland


• Linux is a reimplementation and elaboration of the
UNIX kernel.
• Linux conforms to the POSIX standard.
• Linux is free, open source, and cooperatively
developed.
• Many distributions: Debian, Linux Mint, Red Hat
Enterprise, SUSE Linux Enterprise, Ubuntu, etc.
• There are other UNIX flavors like AIX, FreeBSD, HP-
UX, Solaris, Unixware
Accessing a UNIX system

• The kernel is a core component that is loaded when you


first turn you computer on.
• The kernel loads all other components and serves to
centrally control the activities of the computer.
• A user interacts with the kernel through a channel
called terminal.
• After logging into a command-line terminal, the user
receives a user interface called a shell.
userA userA userA userA

terminal terminal terminal terminal

shell shell shell shell

UNIX kernel

Hardware
Common UNIX shells

• Bourne shell (sh)


• POSIX shell (sh)
• Korn shell (ksh)
• C shell (csh)
• BASH shell (bash)

• When a user logs into a graphical terminal


– X Windows is loaded to provide graphical functionality
– A desktop environment is loaded to standardise the look
and feel of X Windows
Basic shell commands for a command-
line terminal
• Shell prompt
– # for the root user
– $ for all other users
• Commands indicate the name of the program to execute
and are case sensitive
• Options are specific letters that start with a dash (-) and
appear after the command name to alter the way the
command works.
• Arguments also appear after the command name, yet do
not start with a dash. They specify the parameters that
the command works upon.
Some common UNIX command

Command Description
ls List your files
more filename Show the first part of a file
emacs filename Create and edit a file
vi filename Create and edit a file
mv fn1 fn2 Move a file
cp fn1 fn2 Copy a file
rm filename Remove a file
diff fn1 fn2 Compare files, and show where they differ
wc filename How many lines, words, and characters
chmod Change the permission of a file
Some common UNIX command

Command Description
gzip filename Compress a file
gunzip filename Uncompress a file
gzcat filename Look at a gzipped file without having to gunzip
it
cat filename Look at the content of the file
lpr filename Print
mkdir dirname Make a new directory
cd dirname Change the directory
pwd Where you are
grep string fn(s) Look for the string in the files.
Some common UNIX command

Command Description
w Who’s logged in and what they are doing
who Who’s logged in and there they are coming from
finger Display information on system users
last Give a list of everyone’s logins
id Display the user id and group id
uname -a Display all information about the system
date Current date and time
cal Display the calendar of the current month
exit Exit the current shell
Some common UNIX command

Command Description
whoami Return your usename
passwd Change your password
ps Display current processes
kill PID Kill the process with the id you have
quota -v Show what your disk quota is
echo Print text to the terminal screen
mail Send file by email
ssh Open a secure connection
ftp Upload or download file
!! Repeat the previous command
Obtain command help

• It is impractical to memorise the syntax and use of


more than 1000 UNIX commands.
• Use the command man followed by the command name
to retrieve the manual page of that command
man date
• Search the manual pages by keyword
man –k keyword
Manual page section number

Section Description
number
1 Commands that any user may execute
1M Command that only root user may execute
X1M X Windows commands that only the root user
may execute
2 System calls
3 Library routines and functions
3M Mathematical functions
3F Fortran functions
4 File formats
5 Miscellaneous
6 Games
7 Device drivers and interfaces
Shell metacharacters

Metacharacter Description
$ Shell variable
& Background command execution
; Command termination
< << > >> I/O redirection
| Command piping
* ? [] Shell wildcards
‘“\ Metacharacter quotes
` Command substitution
() {} Command grouping
Command input and output

• Your shell can manipulate command input and output.


Command input and output are represented by labels
known as file descriptors.
• Standard input (stdin), descriptor number 0, by default
refers to the user input typed on the keyboard
• Standard output (stdout), descriptor number 1, by default
refers to the terminal screen
• Standard error (stderr), descriptor number 2, by default
refers to the terminal screen
Redirection

• Redirect standard output and standard error from the


terminal screen to a file using the > shell metacharacter
followed by the pathname of the file
• The shell assumes standard output in the absence of a
numeric file descriptor
• You can also redirect both standard output and standard
error to the same file using special notation (see next
slide)
• You can redirect a file to the standard input of a
command using the < metacharacter
Pipes

• To connect the stdout of one command to the stdin of


another, use the | symbol, commonly known as a pipe
• You can use more than one | metacharacter on the
command line to pipe information from one command
to another
• Use the tee command to write to the standard output
and a file
• You can also combine the redirection and pipe
Combine redirection and piping

• You can also combine redirection and piping together


– Input direction must occur at the beginning of the pipe
– Output direction must occur at the end of the pipe
Filter
• Any command that can take from standard input and
give to standard output is called a filter.
Command Description
cut Prints selected portions of its input lines
sort Sorts lines in a file (ASCII)
sort –r Reverse sorts lines in a file (ASCII)
wc Counts the number of lines, words, and
wc -l characters in a file
wc -w
wc –c
pr Formats a file for printing
pr –d Formats a file double spaced
tr Replaces characters in the text of a file
grep Displays lines in a file that match a regular
expression
nl Numbers lines in a file
cut

• The cut command prints selected portions of its input


lines. It’s commonly used to extract delimited fields.
• The default delimiter is <Tab>
• You can change delimiters with the –d option
• The –f options specifies which fields to include in the
output

• How to enumerate the system’s users?


Sort

Option Meaning
-b Ignore leading whitespace
-f Case insensitive sorting
-k Specify the columns that form the sort key
-n Compare fields as integer numbers
-r Reverse sort order
-t Set field separator (the default is whitespace)
-u Output unique records only

How to reverse sort the /etc/group file by its group ID?


Exercises

• List /etc page by page


• Replace all lowercase a with uppercase A in
/etc/passwd and display the content in the terminal
screen page by page
• Replace all lowercase a with uppercase A in
/etc/passwd, sort the contents by the first character on
each line, and then save the results to
/home/jyan/results
Shell variables

• Environment variables are typical set by the system and


contain information that the system and programs
access regularly
• User-defined variables are created by users
• There are other special variables that are useful when
executing commands and creating new files and
directories
Environment variables

• Use the set command or env command to see a list of


environment variables and their values
– HOME: the absolute pathname to the user’s home
directory
– PWD: the present working directory in the directory tree
– PATH: a list of directories to search for executable
programs
– SHELL: the absolute pathname of the current shell
– LOGNAME: the username of the current user used when
logging into the shell
– TZ: the time zone for the system
User-defined variables

• Variable names are prefixed with a dollar sign when


their values are referenced
– echo $MYVAR
• You can surround the variable name with curly braces
– echo “I am the ${REV}th in the competition.”
• Variables created in the current shell are only
available to the current shell, not to the subshells,
unless you export them
Setting variables

• To change the value of a variable, specify the variable


name immediately followed by a equal sign (=) and the
new value
• In some distributions, you must use the setenv
command followed by the variable name and the new
value.
• Variable names can contain alphanumeric characters,
dash (-) or underscore (_)
• They must not start with a number
• They are typically capitalised to follow convention
Other variables

• Other variable are not displayed by set or env


commands, and perform specialised functions
– UMASK is a special variable (discussed in the next
lecture)
• Command aliases are shortcuts to commands stored in
special variables that can be created and viewed by the
alias command
– alias dw=“date;who”
Bash scripting

• Bash scripts automate things you’d otherwise be typing


on the command line
– The fist line is known as the “shebang” statement and
declares the text file to be a script for interpretation by
bash:
• #!/bin/bash
– Comments start with a hash mark (#)
– Invoke the shell as an interpreter or turn on the execution
bit of the script to run the script
– No need to set the file’s extension but some people use
.sh
Command-line arguments and
functions
• Command-line arguments to a script become variables
whose names are numbers
– $1 is the first command-line argument, and so on
– $0 is the name by which the script was invoked
– $# contains the number of command-line arguments that
were supplied
– $* contains all the arguments at once
Variable scope

• Variables are global within a script


• Functions can create their own local variables with a
local declaration
– local variablename
What is the output
Control flow

• if-then and if-then-else


• The terminator for an if statement is fi
• The elif keyword to mean “else if”

• See the example in the next slide. Both the peculiar []


syntax for comparisons and the integer comparison
operators (e.g., -eq) are inherited from the original
Bourne Shell’s channelling of /bin/test.
• The brackets actually invoke test and are not a syntactic
requirement of the if statement
Elementary bash comparison operators

String Numeric True if


x=y x –eq y x is equal to y
x != y x –ne y x is not equal to y
x<y x –lt y x is less than y
x <= y x –le y x is less than or
equal to y
x>y x –gt y x is greater than y
x >= y x –ge y x is greater than or
equal to y
-n x - x is not null
-z x - x is null
bash file evaluation operators

Operator True if
-d file file exists and is a directory
-e file file exists
-f file file exists and is a regular file
-r file You have read permission on
the file
-s file file exists and is not empty
-w file You have write permission on
the file
file1 –nt file2 file1 is newer than file2
file1 –ot file2 file1 is older than file2
Loops

• bash’s for…in construct takes some action for a group


of values
• Any whitespace-separated list of things, including the
contents of a variable, works as a target of for…in
• for loop
for (( i=0; i < $count; i++)); do

done
• while loop
• See examples in the next a few slides
Arrays

• Arrays are not often used in bash.


• Literal arrays are delimited by parentheses, and the
elements are separated by whitespace
– example=(aa bb cc dd)
• Use ${array_name[subscript]} to access individual
elements
– echo ${example[1]}
• The subscripts * and @ refer to the array as a whole
– ${example[@]}
– ${#example[@]} yields the number of elements in the
array
What is the output?
Regular expressions

• Regular expressions enable you to specify a certain


pattern of text within a text document.
• Similar to wildcard metacharacters but interpreted by a
text tool program.
• More regular expression metacharacters are available
than wildcard metacharacters.
Symbol Description
. Matches any one character
* Matches zero or more occurrences of the previous
character.
? Matches zero or one occurrence of the previous
character.
+ Matches one or more occurrences of the previous
character.
[chars] Matches any character from a given set, e.g., [a-z]
[^chars] Matches any character not in a given set, e.g., [^a-z]
(|) Matches either the element to its left or to its right.
Symbol Description
^ Matches the beginning of a line.
$ Matches the end of a line.
\w Matches any word character. [a-zA-Z0-9_]
\s Matches any whitespace character.
\d Matches any digit. [0-9]
{n} Matches exactly n instances of the preceding
element.
{min,} Matches at least min instances of the preceding
element.
{min,max} Matches any number of instances of the preceding
element from min to max.
Examples

• ^\d{4}$
• Letter[^1238]
• (mother|father)
• M[ou]’?am+[ae]r ([Aeae]|[-
])?[GKQ]h?[aeu]+([dhz]|[dhz]?){1,2}af[iy]
The grep command

• Global Regular Expression Print searches for


information using regular expressions
• It displays the lines of text that match extended regular
expressions.
– grep “hello” file1
– grep –v “hello” file1
– grep –i “hello” file1
– grep “^I ” file1
Perl programming

• Perl is a scripting language that offers vastly more


power than bash
• It is a better choice for systems administration than
traditional programing language (the other choice is
Python)
• Perl can do more in fewer lines of code, with less
painful debugging
• Perl’s catch phrase is that “there’s more than one way to
do it”
Perl programming

• Perl statements are separated by semicolons


• Comments start with a hash mask (#)
• Blocks of statements are enclosed in curly braces
• Lines in a perl script are not shell commands; they are
perl codes
• The fist line is known as the “shebang” statement and
declares the text file to be a script for interpretation by
perl:
– #!/usr/bin/perl
Variables and arrays

• Perl has three fundamental data types:


– Scalars: scalar variables start with $
• $a=“this is an example”;
• $count=0;
– Arrays: array variables start with @
• @items=(“socks”, “shoes”, “shorts”);
• Array subscripting begins at zero, the index of the highest
element is $#items
• The array @ARGV contains the script’s command-line
arguments
– Hashes (aka associative arrays): hash variables start with
%
What is the output?
Hashes

• A hash represents a set if key/value pairs.


• In other words, a hash is an array whose subscripts are
arbitrary scalar values
• Subscripting is indicated with curly braces rather than
square brackets
– $myhash{‘ron’}
What is the output?
Elementary Perl comparison operators

String Numeric True if


x eq y x=y x is equal to y
x ne y x != y x is not equal to y
x lt y x<y x is less than y
x le y x <= y x is less than or
equal to y
x gt y x>y x is greater than y
x ge y x >= y x is greater than or
equal to y

You get all the file-testing operators shown in Slide 48 except for
the –nt and –ot operators
Control flow

• Control flow in Perl is very similar to that in bash


• if construct has no then keyword or terminating word
Input and output

• When you open a file for reading or writing, you define


a filehandle to identify the channel
– open($fh, ‘<’, $inputfilename) || die “Couldn’t open
file.”;
– open($fh, ‘>’, $ouputfilename) || die “Couldn’t open
file.”;
Questions?

You might also like