Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
UMRAKH
SUB: UNIX AND SHELL PROGRAMMING
IMPORTANT COMMANDS
Q
This is the mode where you can pass command to act on text, using most of the keys of
the keyboard.
Commands used to move the cursor, to delete or change part of the test, etc.
As soon as the command is entered, it is executed the Return key is not required.
When the vi editor is in the text mode, any key that is pressed by a user is considered
text.
To add the text in a document, we should place the cursor at the desired location.
This mode used to handle files like saving file and perform substitution.
Explain booting sequence with init process. [ Extract point as per mark ]
TYBCA (SEM:5)
Page 1
After you power on/Restart your machine, The first instruction it will run is to pass control
to BIOS (Basic Input/output System) to do POST(Power On Self-Test). Once the control goes
to BIOS it will take care of two things
1)
In this stage GRUB (Grand Unified Boot loader) which is located in the first 30 kilobytes of
hard disk immediately following the MBR is loaded into RAM for reading its configuration
and displays the GRUB boot menu (where the user can manually specify the boot
TYBCA (SEM:5)
Page 2
Stage 4: Kernel
Once the control is given to kernel, it act as a mediator of hardware and software
components.
Kernel once loaded into to RAM it always resides on RAM until the machine is shutdown.
Once the Kernel starts its operations the first thing it do is executing INIT process.
Stage 5: INIT
When init starts, it becomes the parent or grandparent of all of the processes that start up
set up in each run level and sets the default run level.
A run level is a configuration of processes.
Following are the available run levels
0 System shutdown
1 System administration mode.
2 Multiuser, without NFS
3 Full multiuser mode
4 unused
5 Graphical environment mode
6 reboot
All UNIX-like systems can be run in different process configurations, such as the single user
mode, which is referred to as run level 1 or run level S (or s). When system is booted , init
Define process. Which command is used to know the status of process? Explain it
TYBCA (SEM:5)
Page 3
There are three distinct phases in the creation of process using three important system
calls:
fork( ) ,
exec( ) and
wait( )
Fork( )
A process in unix is created with the fork system call, Which creates a copy of
the process that invoke it.
The process that invokes the fork system call is parent process and the newly
created process is the child process.
fork() allocates entry in process table and assigns a unique PID to the child
process.
child gets a copy of process image of parent: both child and parent share the
same code following fork(), different data.
But fork() returns the PID of the child to the parent process and returns 0 to
the child process
For example, when you enter a command at the prompt, the shell first creates a
copy of itself. The image is practically identical to the calling process, except for
TYBCA (SEM:5)
Page 4
The parent then overwrite the image that it has created with the copy of the
program that has to be executed.
This is done with the exec system call and the parent is said to exec the
process.
The parent then executes the wait system call to keep waiting for the child
Wait( )
process to complete.
When the child process has completed execution, it send a termination signal
to the parent.
because those characters are used in the names of special Unix variables.
For example: $ character represents the process ID number, or PID, of the current shell:
$echo $$
$n
These variables correspond to the arguments with which a script was invoked. Here n
is a positive decimal number corresponding to the position of an argument (the first
$#
$*
All the arguments are double quoted. If a script receives two arguments, $* is
$@
equivalent to $1 $2.
All the arguments are individually double quoted. If a script receives two arguments,
$@ is equivalent to $1 $2.
TYBCA (SEM:5)
Page 5
$$
The process number of the current shell. For shell scripts, this is the
process ID
TYBCA (SEM:5)
Page 6
The first array associate name with age. In this array, the name identify each persons age.
The second array associates department number with the sales for that department. The
department number in this array are string.
The associate array structure has several design constraints that we must remember when
we use them
1. Index must be unique, means each index can be associated with an array value only
once.
2. The association of the index with its values is guaranteed.
3. There is no ordering imposed on the indexes. It means if we create an associate array
and print it, there is no guarantee that the element will be printed based on the order in
which the array was created.
4. An array index cannot be sorted. The data value can be sorted.
Creation of Associative Array
To gain insights about array let us create and access the elements of the array.
$ awk 'BEGIN { fruits["mango"]="yellow";
fruits["orange"]="orange"
print fruits["orange"] "\n" fruits["mango"]
}'
On executing the above code, you get the following result:
TYBCA (SEM:5)
Page 7
Q
A
Description
This function convert string into upper case to lower case.
Non alphabetic characters are left unchanged.
Syntax: tolower(string)
Toupper
For example:
tolower("MiXeD cAsE 123")
Output: mixed case 123
This function convert string into upper case to lower case.
Non alphabetic characters are left unchanged.
Syntax: toupper(string)
Length
For example:
tolower("MiXeD cAsE 123")
Output: MIXED CASE 123.
It determines the length of its argument.
Syntax: length(string)
If no argument is present, it assumes the entire line as argument.
Following statement is used to locate the records whose length exceeds 57
characters.
Awk F | length > 57 emp.txt
For example in emp.txt file 2nd field contain the name of employee. To list out
those employee whose name is less than 10 character.
Awk f | length($2) < 10 emp.txt
Locate line in file which contain line between 100 and 150 character
TYBCA (SEM:5)
Page 8
Substr
substr(string, position)
substr(string, position, length)
Split
It Divide string into pieces separated by fieldsep and store the pieces in array
and the separator strings in the seps array
Syntax: split(string, array)
split(string, array, field seperator)
In first format, the field in a string are copied into an array. The first piece is
store in array[1], the second in array[2] and so on. The end of each field is
identified by a field separator character.
In second format, the field separator is specified as the parameter.
Before splitting the string, split() deletes any previously existing elements in
the arrays array.
For example: split("cul-de-sac", a, "-")
splits the string `cul-de-sac' into three fields using `-' as the separator. It sets
the contents of the array a as follows:
a[1] = "cul"
a[2] = "de"
a[3] = "sac"
Index
This searches the string in for the first occurrence of the string find, and
returns the position in characters where that occurrence begins in the string
in. Consider the following example:
Syntax: index(in, find)
For example:
$ awk 'BEGIN { print index("peanut", "an") }'
3
If find is not found, index returns zero.
Remember that string indices in awk start at one.
TYBCA (SEM:5)
Page 9
Without these built-in variables its very much difficult to write simple AWK code.
These variable are used to format output of an AWK command, as input field separator and
even we can store current input file name in them for using them with in the script.
AWK built-in variables are listed below in table
Variable
Description
NR
NF
FILENAME
FNR
FS
RS
OFS
ORS
NR Variable
This will come handy when you want to print line numbers in a file.
$ cat emps
Tom Jones
Mary Adams
4424
5346
5/12/66 543354
11/4/63 28765
4424
5346
5/12/66 543354
11/4/63 28765
NF Variable
Awk NF will be very useful for validating whether all the fields are exist in a record.
TYBCA (SEM:5)
Page 10
In the above example $2 and $5 represents Name and Salary respectively. We can get the
Salary using $NF also, where $NF represents last field. In the print statement , is a
concatenator.
How to print last field without knowing the number of field in file?
$ awk '{print $NF;}' employee.txt
$5,000
$5,500
$7,000
In the above example $NF represents last field.
FILENAME
Jones
2143
abc.txt
78
Gondrol
84
2321
77
56
58
FNR Variable
This will come handy when you want to print no of line present in a given file.
TYBCA (SEM:5)
Page 11
45
OFS Variable
This variable is useful for mentioning what is your output field separator which
separates output data.
Example: Display only 1st and 4th column and the separator between at output for
these columns should be $.
$ awk BEGIN{FS=,;OFS= $ }{print $1,$4} db.txt
Om $ IBM
Sai $ JHH
Ram $ BofA
RS Variable
Row Separator is helpful in defining separator between rows in a file. By default AWK
TYBCA (SEM:5)
Page 12
takes row separator as new line. We can change this by using RS built-in variable.
Example: I want to convert a sentence to a word per line. We can use RS variable for
doing it.
$ echo Om Sai Ram | awk BEGIN{RS= }{print $0}
Om
Sai
Ram
ORS Variable
This variable is useful for defining the record separator for the AWK command output.
By default ORS is set to new line.
Example: Print all the company names in single line which are in 4th column.
$ awk -F, BEGIN{ORS= }{print $4} db.txt
IBM JHH BofA DELL SmartDrive
expr command .
expr is a command line Unix utility which evaluates an expression and outputs the
corresponding value.
The Bourne cell doesnt have any computing features. So it rely on external command expr
for that purpose.
Syntax: expr (expression)
expr evaluates integer or string expressions, including pattern matching regular expressions.
It is not working with floating point value.
The operators perform following function
Perform arithmetic operation on integers
Manipulating string.
Arithmetic Operator used with expr
Operator
Description
TYBCA (SEM:5)
Page 13
Modulus - Divides left hand operand by right hand operand and returns
remainder
characters matching the pattern. Means give the length of the string.
$ expr omsairam : ,*
8
TYBCA (SEM:5)
Page 14
The for loop operate on lists of items. It repeats a set of commands for every item in a list.
Syntax:
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
Done
shell.
Each time the for loop executes, the value of the variable var is set to the next word in the
#!/bin/sh
for var in 1 2 3 4 5
do
echo n $var
TYBCA (SEM:5)
Page 15
The second form of the for loop is similar to the for loop in C programming language,
which has three expressions (initialization, condition and updation).
Syntax
for (( expr1; expr2; expr3 ))
do
Body of the loop.
done
Before the first iteration, expr1 is evaluated. This is usually used to initialize variables for
the loop.
All the statements between do and done are executed repeatedly until the value of expr2 is
TRUE.
After each iteration of the loop, expr3 is evaluated. This is usually used to increment a loop
counter.
TYBCA (SEM:5)
Page 16
When we use if to evaluate expressions, we need the test statement because the true or false
values returned by expressions cant be directly handled by if.
test use certain operators to evaluate the condition on its right and returns either a true or
false exit status, which is then used by if for making decision.
In the if statement instead of explicitly mentioning the word test, we have placed the
condition within [ ] which is allowed.
Here we must provide space around the operators and operands.
test command can carry out following types of test
Numerical Test
String Test
File Test
Numerical Test : All relation operator is used as numeric comparison operator with test.
It always begin with (hyphen) followed by two-letter string and enclosed on either side by
whitespace.
Operator
Description
eq
Equal to
-ne
Not Equal to
-gt
Greater Than
-lt
Less Than
-ge
-le
File Test : Test command has several options for checking the status of a file which listed below in
table.
Using this we can find out whether the specified file is an ordinary file or a directory or a
whether it grants read, write or execute permissions.
It also use with negates test !
Operator
TYBCA (SEM:5)
Description
Page 17
We
can
use
test
and
interchangeably.
Echo Enter the file name:;
Read fname;
If test -f fname then
echo It is a file
Else
echo You enter is not a file
name
Fi
String Test :
There are following string operator used with expr to perform operation on string.
Operator
TYBCA (SEM:5)
Description
Page 18
Shift Command
Shift transfer the contents of positional parameters to its immediate lower numbered one.
This is done as many time as the statement is called.
When called once, $2 becomes $1, $3 becomes $2, and so on.
For example 1: $ date
Wed Aug 19 04:10:30 IST 2015
$ echo $1 $2 $3
Wed Aug 19
For example 2:
$ Shift
$ echo $1 $2 $3
Aug 19 04:10:30
TYBCA (SEM:5)
Page 19
Output:
If we observed the output of last two word Good0, Good1. This occurred in the output
because at a time we can access only 9 positional parameters. When we tried to refer $10 it
was interpreted by the shell as if you wanted to output the values $1 and 0. Same is true for
$11 as $1 and 1.
To access word after 9th word, we have to use shift.
For example:
$ Shift 9
$ Echo $1 $2 $3
Output: you are fine.
Q
Definition: User mask is a 3 digit octal system variable which is used initially set
permission for a directory of files.
It contains the octal settings for the permissions that are to be removed from the default
when a directory or file is created.
You can change the settings by creating mask entry in you login file.
When new directory or file is created, the number in the mask is used to set the default
permission.
Default Permission :
For Directory: 777
For File: 666
MASK
TYBCA (SEM:5)
DIRECTORY PERMISSION
FILE PERMISSION
Page 20
To display the current user mask settings, use the umask command with no argument.
$ umask
000
To set it, use the command with the new mask setting.
$ umask 022
$ umask
022
You can schedule a job to be executed using relative time from now.
Syntax: $ at now + COUNT UNIT
TYBCA (SEM:5)
Page 21
TYBCA (SEM:5)
Page 22
batch
It
is used to schedules
permits.
The command doesnt take any arguments
be executed
For Example:
$ at 14:08
at> empdetails.sh [Ctrl d ]
The job goes to the queue and at 2:08 PM
today, the script file empdetails.sh will be
executed.
Q
Once job submitted using this command have been executed, the job have been rescheduled
if they are to be carried out again.
For example: If we want to backup all our c program files at the end of the day then we do
it as follow
$ at 5 PM
cp *.c ./cbackup
We must remember to issue this command every morning to be able to take backup every
evening without failing.
To overcome this we have to use crontab command. The job can be carried out on a regular
basis using crontab command.
Crontab utility is an effective way to schedule a routine background job at a specific time
and/or day on an on-going basis.
Crontab format:
TYBCA (SEM:5)
Page 23
Description
Allowed Value
MIN
Minute field
0 to 59
HOUR
Hour field
0 to 23
DOM
Day of Month
1-31
MON
Month field
1-12
DOW
Day Of Week
0-6
CMD
Command
Any
command
to
be
executed.
Example: Let create a file which contain command we execute regularly but also the details
of date and time in a specific format we discussed previously.
Here we want to print message work Hard on 10:30 am on first day of every month.
Second command would mail the letter to user aa2 on 17th Nov. of every year.
$ cat > cmdfile
30
10
1
*
0
0
17
11
Crtl d
$ crontab cmdfile
*
*
crontab -l
crontab -r
crontab -v
TYBCA (SEM:5)
Page 24
Q
A
Use the kill command to send a signal to each process specified by a pid. The default signal
For example:
Terminates the job having PID 105. To know the PID run ps command.
$ kill 105
If all these process have the same parent, you may simply to kill the parent of all this
process to know the PPID.
The system variable $! store the PID of the last background job.
TYBCA (SEM:5)
Page 25
So we can kill the last background job without using the ps command to find out PID
$ sort o emp.lst &
345
$ kill $!
By default, kill uses the SIGTERM signal (15) to terminate the process.
This signal cant be generated at the press of a key, we must use kill with signal name
preceded by the s option
$ kill s KILL 121
OR
$ kill -9 121
OR
$ kill s KILL 0
SIGNAL
USAGE
INT (interrupt)
QUIT (quit)
ABRT (abort)
14
15
The signal number 1 is a hangup signal. I recommended using 1 signal because it should
kill the process and it can save the buffer (if supported). For example if it is an editor, save
the buffer. This is the default if you do not specify a signal number. Signal number 9, a kill
signal, is the surest way to kill a process.
Explain ps command.
TYBCA (SEM:5)
Page 26
knowledge of the
characteristics.
By default, ps list out the processes associated with a user at that terminal
$ ps
PID
TTY
TIME
476
tty03
00:00:01
659
tty03
00:00:01
684
tty03
00:00:001
Each line show the pid, the
CMD
login
sh
ps
terminal with which the process is associated, the cumulative
processor time that has been consumed since the process has been started and process
name.
OPTION
DESCRIPTIONS
-f
-e or A
-u usr
-a
-l
-t term
DESCRIPTION
UID
PID
Process ID.
PPID
TYBCA (SEM:5)
Page 27
STIME
TTY
TIME
CMD
Example 2: Display all user process but doesnt display the system process.
$ps a
PID TTY
662
705
1005
1056
1069
tty02
tty04
tty01
tty02
tty02
TIME CMD
00:00:00
00:00:00
00:00:00
00:00:00
00:00:00
ksh
sh
ksh
sort
ps
TTY
TIME CMD
?
?
00:00:00 sched
00:00:01 init
?
00:00:00 vhand
00:00:01 bdflush
You can use multiple if...elif statements to perform a multiway branch. However, this is not
always the best solution, especially when all of the branches depend on the value of a single
variable.
TYBCA (SEM:5)
Page 28
Unix Shell supports case...esac statement which handles exactly this situation, and it does
so more efficiently than repeated if...elif statements.
The basic syntax of the case...esac statement is to give an expression to evaluate and several
different statements to execute based on the value of the expression.
The interpreter checks each case against the value of the expression until a match is found.
If nothing matches, a default condition will be used.
Syntax:
case word in
pattern1)
Statement(s) to be executed if pattern1 matches
;;
pattern2)
Statement(s) to be executed if pattern2 matches
;;
pattern3)
Statement(s) to be executed if pattern3 matches
;;
esac
Here the string word is compared against every pattern until a match is found. The
statement(s) following the matching pattern executes. If no matches are found, the case
statement exits without performing any action.
When statement(s) part executes, the command ;; indicates that program flow should jump
to the end of the entire case statement. This is similar to break in the C programming
language.
#!/bin/sh
FRUIT="kiwi"
case "$FRUIT" in
"apple") echo "Apple pie is quite tasty."
TYBCA (SEM:5)
Page 29
TYBCA (SEM:5)
Page 30