UNIX OS Lecture III: Process Management, Scripting Basics
1. UNIX OS
Lecture III
Simonas Kareiva
Vilnius University
Faculty of Mathematics
and Informatics
Preparation of the material was supported by the project „Increasing Internationality in Study
Programs of the Department of Computer Science II“, project number VP1–2.2–ŠMM-07-K-
02-070, funded by The European Social Fund Agency and the Government of Lithuania.
2. Lecture #3 outline
Working with OS. Runlevels. Terminals
Access rights, file administration
System and network settings, date/time, etc.
File systems, mounting, management
Process review and management, listing, killing…
System logs, work automation
2
3. Runlevels
System booting stages
Services are assigned for particular runlevels
ID Name Description
1 Single-User Mode Mode for administrative tasks
2 Multi-User Mode No network and service configuration
3 Multi-User Mode with
Networking
Normal system startup
4 Not used/User-
definable
Special purpose mode
5 X11 Like 3 + graphic environment
3
7. tty settings (linux)
/etc/event.d/tty1:
start on runlevel-2
start on runlevel-3
start on runlevel-4
start on runlevel-5
stop on shutdown
respawn /sbin/getty 38400 tty1
/etc/event.d/tty2:
start on runlevel-2
start on runlevel-3
stop on runlevel-4
stop on runlevel-5
stop on shutdown
respawn /sbin/getty 38400 tty2
7
9. File permissions
Value Permissions Expressi
on
Binary
0 No permission --- 000
1 Execution only --x 001
2 Only write allowed -w- 010
3 Only write and execute -wx 011
4 Read only r-- 100
5 Read + execute, no write r-x 101
6 Read + write, no execution rw- 110
7 All permissions rwx 111
9
10. File permissions
File permissions are defined by three byte octets:
1. User permissions
2. Group permissions
3. Others permissions
Example:
10
11. File permissions
Notation:
-rw-r--r–-
0123456789
First symbol (0) indicates file type – file, directory,
special device, socket, pseudo-file, etc.
Symbols 1-3 define user permissions
Symbols 4-6 define group permissions
Symbols 7-9 define permissions for everyone else
11
12. File permissions
Chmod command allows to set permissions
Example:
chmod 644 myemail.eml
chmod 700 secret.sh
chmod 000 nobody.txt
Question:
how do you understand the following commands?
chmod go= FILE.pl
chmod go-w,a+x FILE.pl
12
13. File permissions
chmod parameter values:
u – user
g – group
o – others
a – all
+ add permissions
- revoke permissions
= give only specified permissions (explicit)
r – read permission
w – write permission
x – execute permission
13
34. Lecture #3 – part II
How to start writing scripts?
Basics of script writing. Command line arguments
Control structures (if, while, for, case)
Auxiliary tools (sed, awk, perl, ex)
Practical examples and “what do I need this for?”
34
35. How to begin?
Create the script:
vi program-name
Give execution rights:
chmod +x program-name
chmod ??? program-name
Execute:
bash program-name
sh program-name
./program-name
35
36. Example
# Author: Simonas Kareiva
#
# My first program
#
clear
echo “Hello $USER"
echo “Today is c ";
date
echo “The number of joined users : c" ;
who | wc -l
echo “Calendar:"
cal
exit 0
36
39. Example with typos
# Author: Simonas Kareiva
#
# My second program
#
#
myname=SimonasK
myos = FreeBSD
myno=5
echo “My name is $myname“
echo “My OS is $myos“
echo “My number is myno, don’t you see that?"
39
40. Echo command
-n Doesn’t end output by additional empty line.
-e enables interpretation of:
a alert
b backspace
c homework
n new line
r returning
t horizontal tab
slash
echo -e “Apple is a healthy attfoodrn"
40
47. Commands and their arguments
ls blabla
cp y y.bak
mv y.bak y.good
tail -10 myf
mail –s attention vilnius
sort -r -n manof
date
clear
47
48. Example
#!/bin/sh
#
# Script which counts everything
#
echo “The number of arguments is $#“
echo "$0 is file name“
echo "$1 is first argument“
echo "$2 is second argument“
echo “And they all together are : $* arba $@"
48
58. Control structures
if [ $1 -gt 0 ]; then
echo "$1 is positive"
elif [ $1 -lt 0 ]
then
echo "$1 is negative"
elif [ $1 -eq 0 ]
then
echo "$1 is zero"
else
echo "Oops! $1 is not number, give number"
fi
58
59. Loops
for i in 1 2 3 4 5
do
echo “Hello $i times"
done
for i in `seq 1 100`
do
echo “Hello $i times"
done
59
61. Example (1)
if [ $# -ne 1 ]
then
echo “Arguments needed“
exit 1
fi
n=$1
sum=0
sd=0
while [ $n -gt 0 ]
do
sd=`expr $n % 10`
sum=`expr $sum + $sd`
n=`expr $n / 10`
done
echo "Sum is $sum"
61
62. Example (2)
temph=`date | cut -c12-13`
dat=`date +"%A %d in %B of %Y (%r)"`
if [ $temph -lt 12 ]
then
mess="Good Morning $LOGNAME, Have nice day!"
fi
if [ $temph -gt 12 -a $temph -le 16 ]
then
mess="Good Afternoon $LOGNAME"
fi
if [ $temph -gt 16 -a $temph -le 18 ]
then
mess="Good Evening $LOGNAME"
fi
62
63. Example (3) (/bin/bash)
while :
do
ti=`date +"%r"`
echo -e -n "033[7s"
tput cup 0 69
echo -n $ti
echo -e -n "033[8u"
sleep 1
done
63
64. Homework
for (( i = 1; i <= 9; i++ )) do
for (( j = 1 ; j <= 9; j++ )) do
tot=`expr $i + $j`
tmp=`expr $tot % 2`
if [ $tmp -eq 0 ]; then
echo -e -n "033[47m "
else
echo -e -n "033[40m "
fi
done
echo -e -n "033[40m“
echo ""
done
64
/etc/host.conf Tells the network domain server how to look up hostnames. (Normally /etc/hosts, then name server; it can be changed through netconf.)
/etc/hosts Contains a list of known hosts (in the local network). Can be used if the IP of the system is not dynamically generated. For simple hostname resolution (to dotted notation), /etc/hosts.conf normally tells the resolver to look here before asking the network nameserver, DNS or NIS.
/etc/hosts.allow ir /etc/hosts.deny
/etc/issue ir /etc/issue.net These files are read by mingetty (and similar programs) to display a &quot;welcome&quot; string to the user connecting from a terminal (issue) or through a telnet session (issue.net). They include a few lines stating the Red Hat release number, name, and Kernel ID. They are used by rc.local.
/etc/rc.d/rc
/etc/rc.local
/etc/rcX.d/ (/etc/rc2.d/
/etc/mtab This changes continuously as the file /proc/mount changes. In other words, when filesystems are mounted and unmounted, the change is immediately reflected in this file.
/etc/fstab Lists the filesystems currently &quot;mountable&quot; by the computer. This is important because when the computer boots, it runs the command mount -a, which takes care of mounting every file system marked with a &quot;1&quot; in the next-to-last column of fstab.
/etc/passwd
/etc/group
/etc/securetty Contains the device names of tty lines (one per line, without leading /dev/) on which root is allowed to login.
return status
return status
return status
return status
return status
return status
return status
-n string1 is NOT NULL and does exist
-z string is null and exists
-s file Non empty file
-f file Is File exist or normal file and not a directory
-d dir Is Directory exist and not a file
-w file Is writeable file
-r file Is read-only file
-x file Is file is executable