OPERATING SYSTEMS
DESIGN
UNIX BASICS
&
SHELL SCRIPTING
What Is UNIX?
■ UNIX is an operating system (OS)
■ Multiuser – computer system used by more
than one person at the same time
■ Multithreaded – multiple simultaneous use
of the same program
■ Multitasking – handles more than one job
(task) per person at the same time
Kernel
■ The kernel of UNIX is the hub of the operating system: it
allocates time and memory to programs and handles the
filestore and communications in response to system calls.
■ As an illustration of the way that the shell and the kernel work
together, suppose a user types rm myfile (which has the effect
of removing the file myfile). The shell searches the filestore for
the file containing the program rm, and then requests the
kernel, through system calls, to execute the program rm on
myfile. When the process rm myfile has finished running, the
shell then returns the UNIX prompt % to the user, indicating
that it is waiting for further commands.
Filesystem Hierarchy Standard
■ Outlines standard locations for files and
directories
■ Gives software developers a consistent
context regardless of the distribution
■ Helps users of one system work on
another
Linux directories defined by FHS
FHS: Linux directories
Directory Structure
Directory Path Structure
■ Absolute Path of pg1
/home/its/pg1
■ Relative Path
working dir: ee51vn
../ -> /home/its/ug1
../.. -> /home/its
../../pg1 -> /home/its/pg1
Some useful terms
■ When you open terminal it will write:
user@hostname:~$
○ user: the user account which you are logged in.
○ hostname: the machine name which you are
operating.
○ ~:the home dir of user. Generally points
/home/<user>
○ $: Run the command with ordinary User
privileges.
○ #: Run the command with system admin
privileges.
Some useful commands
■ cd - change directories (built into shell)
■ wc - word, line, character, and byte
count
■ echo - echo characters back (print)
■ passwd - change password
■ sort - sort lines of a text file
■ ls - lists the contents of your current
working directory
Some useful commands
■ cp - copy a file(s)
■ ps - status of processes (what’s running)
■ pwd - Prints the current directory.
■ kill [-sig] %job
■ Send signal sig to the specified job. sig can be
either numeric or symbolic. kill -l lists all available
signals. By default, sig is SIGTERM (15).
■ read name - Reads one line from standard
input and assigns it to the variable name.
Some useful commands
■ grep - It searches files for specified
words or patterns.
■ $ grep -i science science.txt
■ wc - does a word count
■ Last but one of most important
command is MAN
Finding files
■ locate command
■ Fastest method to search for files
■ A shortcut to the slocate (or secure
locate) command
■ find command
■ Used to find files using various criteria
■ Searches the directory tree recursively,
starting from a certain directory, for files
that meet criteria
which command and PATH variable
■ which command
■ Used to locate files within directories
listed in the PATH variable
■ PATH variable
■ Stores list of directories searched when
commands are executed without an
absolute or relative pathname
The structure of a mode
Interpreting permissions
SHELL
■ A shell is a command interpreter.
■ The shell acts as an interface between the
user and the kernel.
■ You can put commands in a file and execute
them all at once.
■ The script itself is just a series of commands.
■ Comments begin with a hash (#) and
continue to the end of the line (the first line is
special)
#!/bin/sh
■ The first line of any script must begin with #!,
followed by the name of the interpreter.
■ When Unix tries to execute the script, it sees
the first two characters (#!) and knows that it
is a script.
■ It then reads the rest of the line to find out
which program is to execute the script.
Running a script
■ chmod +x {script_name}
■ ./{script_name}
Variables
■ Variables do not need to be declared.
■ There is only one type of variable
■ VAR=value →Set value to variable
■ $VAR or ${VAR} →Use the value of the
variable
■ COLOR=yellow
echo This looks $COLORish
echo This seems ${COLOR}ish
Special variables
■ Command-line arguments
■ $1 refers to the first command-line argument (after
the name of the script), $2 refers to the second one,
and so forth, up to $9.
■ If you have more than nine command-line
arguments, you can use the shift command:
this discards the first command-line
argument, and bumps the remaining ones up
by one position: $2 becomes $1, $8 becomes
$7, and so forth.
Other special variables
$? gives the exit status of the last command
that was executed.
$- lists all of the options with which sh was
invoked.
$$ holds the PID of the current process.
$! holds the PID of the last command that was
executed in the background
$IFS (Input Field Separator) determines how sh
splits strings into words.
Flow control
■ #!/bin/sh
myname=`whoami`
if [ $myname = root ]; then
echo "Welcome to FooSoft 3.0"
else
echo "You must be root to run this script"
exit 1
fi
While & For
■ while condition; do
commands
done
■ for i in foo bar baz "do be do"; do
echo "$i"
done
I/O redirection
■ “< filename” → Connect standard input to
the file filename. This allows you to have a
command read from the file.
■ “> filename” →Connect standard output to
the file filename. This allows you to save the
output of a command to a file. If the file does
not exist, it is created. If it does exist, it is
emptied before anything happens.
I/O redirection (contd.)
■ “>> filename” →Connects standard
output to the file filename. Unlike >,
however, the output of the command is
appended to filename.
■ “command1 | command2” → Creates a
pipeline: the standard output of
command1 is connected to the standard
input of command2.
Useful utilities
■ basename : basename pathname
prints the last component of pathname.
■ basename /foo/bar/baz prints baz
■ dirname : dirname pathname prints all
but the last component of pathname,
that is the directory part: pathname
■ dirname /foo/bar/baz prints /foo/bar
AWK
■ Awk (and its derivatives, nawk and
gawk) is a full-fledged scripting
language.
■ Inside sh scripts, it is generally used for
its ability to split input lines into fields
and print one or more fields.
AWK (contd.)
BEGIN actions performed
before first input line
processed
END actions performed after
last input line
processed
AWK (contd.)
■ awk -F : '{print $1, $3 }' /etc/passwd
■ The -F : option says that the input
records are separated by colons. By
default, awk uses whitespace as the
field separator.
■ Sum up column 2 and print the total
{total += $2} END { print
total}
AWK Example
■ Print fields in reverse order one per line
{
for (i=NF; i>=1; i--)
print $i;
}
Process
a process is an instance of a computer program that is being executed.
It contains the program code and its current activity. Depending on
the operating system (OS), a process may be made up of
multiple threads of execution that execute instructions concurrently.
A computer program is a passive collection of instructions, while a process
is the actual execution of those instructions. Several processes may be
associated with the same program; for example, opening up several
instances of the same program often means more than one process is
being executed.
looking all created processes: ps –aux
killing process: kill -9 <pid>
Some useful commands for
Processes
ps - status of processes (what’s running)
kill [-sig] %job
Send signal sig to the specified job. sig can be either
numeric or symbolic. kill -l lists all available
signals. By default, sig is SIGTERM (15).
System Call & Library
Functions
A system call is the programmatic way in which a computer
program requests a service from the kernel of the operating system it
is executed on. This may include hardware-related services (for
example, accessing a hard disk drive), creation and execution of
new processes, and communication with integral kernel services such
as process scheduling. System calls provide an essential interface
between a process and the operating system.
On Unix, Unix-like and other POSIX-compliant operating systems, popular
system calls are open, read, write, close, wait, exec, fork, exit, and kill.
In a C program, system calls can be directly implemented or indirectly
using a library function.
Library Functions Example: printf, scanf,fopen,fprintf,fscanf, etc …
Library Functions are not system calls.
Console Line Arguments
./a.out 1 2
C file:
int main(int argc, char*[] argv){
int i;
for(i=0;i<argc;i++){
printf(“%s”,argv[i]);
}
}
C Structures
typedef struct{
int a;
int b;
}example; // you can define a new variable type with the
given variable fields. example is a type like int.
int main(int argc, char*[] argv){
example starea; // example: type starea : variable
starea.a=1; // access a field of starea variable with .
starea.b=starea.a+4;
printf(“a is %d - b is %d\n”,starea.a,starea.b);
}
C Structures - 2
typedef struct{
int a;
int b;
}example;
int main(int argc, char*[] argv){
example *stpoint; // creation of a pointer of a struct
// in the current state, stpoint is a dangling pointer
and we cannot access any field with:
stpoint.a=1; // ERROR !!!
stpoint.b=starea.a+4; // ERROR !!!
//…
}
C Structures - 2
typedef struct{
int a;
int b;
}example;
int main(int argc, char*[] argv){
example *stpoint; // creation of a pointer of a struct
stpoint->a=1; // -> is used to access any field with a
structure pointer . But this is an errorneous code too.
Because stpoint is a dangling pointer and points an
unallocated area !!!
stpoint->b=stpoint->a+4; // ERROR too !!!
///…
}
C Structures - 2
typedef struct{
int a;
int b;
}example;
int main(int argc, char*[] argv){
example *stpoint; // creation of a pointer of a struct
stpoint = (example*)malloc(sizeof(example)); // we should
allocate a memory area with the size of structure firstly,
and then assign the returning address to struct pointer
stpoint->a=1; // -> is used to access any field with a
structure pointer.
stpoint->b=stpoint->a+4;
printf(“a is %d - b is %d\n”,stpoint->a,stpoint->b);
}