Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Scripting
Introduction to UNIX
Agenda
• UNIX has evolved over the past many years, from its conceptual
state into a powerful and effective OS
• UNIX evolved from a early time-sharing OS called MULTICS.
• Ken Thompson of AT&T of Bell Laboratories originally designed
UNIX OS in 1969
• Originally written in assembly language, the core of the UNIX
system was recoded in ‘C’ in 1973,after Dennis Ritchie came up
with ‘C’ language
• In 1977, Berkeley campus of the University of California made the
first BSD release
• Ideas and code from Berkeley Unix (including the vi(1) editor) were
fed back to Bell Labs
Origins of Unix and Linux
• System V
• BSD Unix FreeBSD
• Solaris Digital Unix
• IRIX AIX
• Linux - distro
– RedHat Slakeware Mandrake Ubuntu
– Debian SuSE Knoppix Fedora
Features of UNIX OS
• Multi-user, Time-sharing OS
• Multi-tasking OS
• Portable
• Modular
• Secure
• Efficient tools
• Strong text processing capability
• Simple
Structure of UNIX system
• The kernel
• The shell
• Utilities/tools and applications
Starting a session
• passwd
• pwd
• ls
• cat
• rm
• mkdir
• banner
• date
• env
• whoami
• who
Some commonly used commands
• passwd
– Changes login password
• pwd
– Stands for Present Working Directory
– Displays the current directory
• List files – ls
– Lists the files in the current directory
-l gives a long listing
• rm
– To delete a file
– Using –i option, prompts the user before deleting the
file
• cd
– command is used to change directories
– ‘.’ refers to the current directory
– ‘..’ refers to the parent directory
– cd when used without arguments changes to home
directory
• Mkdir
– used to create directories
Predefined Shell Variables
• Relative pathname
$ cat ./info
• Absolute pathname
$ cat /usr/guest/intro/sample
Wild-card characters
$ cat Ap*
Displays the contents of all files having a name starting with Ap.
$ ls ?st
Lists all file starting with any character followed by st.
The [ ]
$ ls [a-z]pple
• Mount / Umount
– A process by which a specific file system (on a device/partition)
can be accessible
– Mounting involves specifying the device file representing the
partition and the directory under which the device files would be
available (mount point)
– This concept works on the basic principle of unix filesystems
that there is only one directory hierarchy , which can span
multiple devices /partitions
– Mount information is available in /etc/mtab
– Mounting can be done automatically by adding an entry in the
/etc/fstab
Filenames
• fsck
• mkfs
• du
• man hier
• mount
• umount
• df
File Types
• Ordinary Files
– text, executable programs, images, etc.
• Directories
– Branching points in the hierarchical tree
– Used to organize groups of files
– May contain ordinary files, special files or other directories
• Special Files
– Used to represent a real physical device such as a printer, tape
drive or terminal, used for Input/Output (I/O) operations
– Two types of I/O: character and block
– Usually only found under directories named /dev
Files
• Pipes
– UNIX allows commands to be linked together using a pipe.
The pipe acts a temporary file which only exists to hold
data from one command until it is read by another
– For process communication
• Links
– a system to make a file appear in more than one location
on a filesystem
• cp
• mkdir
• rmdir
• mv
• cd
• rm
• ln
• cat
• ls
• file
• find
• diff
• touch
Redirections
• execute
– allows a user to use the filename as a system command.
– On a directory it allows the user ‘search’ permissions on
the directory.
– The user can copy files into the directory
• Absolute
– The absolute mode indicates the exact settings for all
permissions
– In the absolute mode octal numbers are used to represent the
three kinds of access permissions.
– The octal values are:
4 read permission
2 write permission
1 execute permission
– Chmod [nnn] [filename]
• Symbolic
– The symbolic mode indicates particular permissions to be set
– The symbolic mode consists of three parts. Who is affected, the
operator indicating action taken, and the permission.
– chmod [ugo] [+/-] [rwx] [filename]
Examples
• $ chmod uo +x ac
This changes file permissions on file ac for specified users
Changing Owner chown
– sort
– split
– tail
– tr
– uniq
– wc
head & tail
sort [filename]
Sort Options
$ cat second
“waves can banish a beach overnight, then build a new one the next
spring”
firstdisplays what occurs in the first files but not in the second
second displays what occurs in the second file but not in the first
Third displays what is common in both files
• Displays the second and third column, i.e. what is only in the second file and what is common to
both files.
tr
• the paste command joins lines from two files and displays the
output.
• It can take a number of filenames as command line
arguments.
• A ‘-‘ takes input from standard input.
• The command takes each file from left-to-right and joins the
lines. If there is no corresponding field the command puts the
delimiter and leaves it blank.
paste file1 file2
• A number of delimiters may be specified after the –d option to
separate different fields.
– paste –d$:* file1 file2 file3
– $ paste –d$: file1 file2 file3
split
• Using split a large file may be split into smaller files. After
editing, the file may be concatenated into a single file.
split filename
• The file is split into lines of 1000. Each file is named. Xaa,
xab, xac, … xba until xzz.
Symbol Meaning
• Egrep
– Provides a set of regular expressions which is more powerful
than grep
+ one or more repetitions of a character.
? Matches zero or one instance of a character.
| Search for several targets.
() Treats enclosed text as a group.
$ egrep “marian|ron|bruc” phone_list
Searches entries for marian , ron and bruc.
grep options
-mtime the last time the file was modified +n greater than n number of
days -n less than n number of days
• For example
xargs cat
/etc/passwd /etc/group [The arguments are given in the
standard input]
Sed – Changing information in Files
• Sed edits a character stream, and is thus able to handle very large
files. It is useful over vi in a way that vi cannot edit files exceeding a
certain size.
• It doesn’t change the original content of the file rather it writes to
standard output
Syntax:
sed <option> ‘address action’ <file (s)>
Internal Sed Commands-Options
vi – Visual Editor
$ vi +67 list.c
Puts the cursor on line if exists otherwise vi will
position the cursor at the beginning of the last line.
Vi’s Input Mode Commands
Command Significance
a Begins appending typed text after the cursor position
Command Significance
:w Will write the contents of editing buffer into the file
ZZ Equivalent to :wq
:q Quit
Command Significance
Command Significance
Moves to the next word or punctuation mark
w
W Moves to the next word
Command Significance
Command Significance
Command Significance
Command Significance
yw Yanks a word
$ SIZE=1024
$ MY_ADDRESS=buchanan@phys.ualberta.ca
$ greeting='Welcome to the Bash shell'
Variable Description
$$ Process no of the current process
$! Process no of the last background process
$? Exit value of last command
$# Number if command line arguments
$0 Command or program name
$n Positional Parameter number n
$* All command line arguments
“$@” All arguments each in quotes
Testing and Branching
if statement_A
then
statement_B
statement_C
elif statement
statement_D
else
fi
case value in
pattern1) command list;;
pattern2) command list;;
.
.
.
patternN) command list
esac
loops
• While <command>
do
done
• Until <command>
do
done
File Test Operators
#! /bin/bash
# # Move is a script that moves a file if no overwrite will occur
# and will delete the file to be moved if it is empty
# # Usage: move [file] [destination]
#
if [ ! -s $1 ] then
if rm $1 2>/dev/null then
echo "$1 was empty and thus removed"
else
echo "$1 does not exist"
fi
else
mv -i $1 $2
fi
String Comparisons
Operator Operation
+ Addition of two integers
- Subtraction of two integers
* Multiplication of two integers
/ Integer division
% Modulus of integer division
An Example expr:
$ expr 3 + 2 \* 8
19
Substitution
Wildcard Description
Form Description
${parameter:-word} If parameter is null or unset, word is substituted for
parameter. The value of parameter does not change.
${parameter:=word} If parameter is null or unset, parameter is set to the value
of word.
${parameter:?mess If parameter is null or unset, message is printed to
age} standard error. This checks that variables are set
correctly.
${parameter:+word} If parameter is set, word is substituted for parameter. The
value of parameter does not change.
Substituting a Default Value
•After the execution of this statement, both HOST and PS1 are set.
Aborting Due to Variable Errors
`command`
• Command substitution is generally used to assign the output of a command
to a variable. Examples:
DATE=`date`
USERS=`who | wc –l`
UP=`date ; uptime`
Quoting
SIGKILL 9 kill
– Option:
$ nohup command
$ nohup myproc
Subshells
Parent Shell
Child Shell
Environment
Environment Copied from Parent
Local
Local Clean, initialized
Exporting ..Revisited
• ps – Process Status
– Displays the characteristics of a process
– Option:
-f Prints the full details
-u (user) Prints the activities of the user “user” at any time
-a Prints all the processes running in the system
-l Long listing of the attributes of each processes
Scheduling jobs
$ at –l
Returns the ID number and scheduled time for each of your at jobs.
cron