Os Module II
Os Module II
Introduction
Shell programming is a basic skill that every UNIX System administrator
should have. The Systems Administrator must be able to read and write
shell programs because
There are many tasks that can and should be quickly automated by
using shell programs
Many software products come with install scripts that have to be
modified for our system before they will work.
What is Kernel
The kernel is a computer program that is the core of a computer’s operating
system, with complete control over everything in the system. It manages
following resources of the Linux system –
File management
Process management
I/O management
Memory management
Device management etc.
It is often mistaken that Linus Torvalds has developed Linux OS,
but actually he is only responsible for development of Linux kernel.
Complete Linux system = Kernel + GNU system utilities and libraries + other
management scripts + installation scripts.
What is Shell
A shell is special user program which provide an interface to user
to use operating system services. Shell accept human readable commands
from user and convert them into something which kernel can understand. It
is a command language interpreter that execute commands read from input
devices such as keyboards or from files. The shell gets started when the user
logs in or start the terminal.
Linux Shell
There are several shells are available for Linux systems like –
BASH (Bourne Again SHell) – It is most widely used shell in Linux
systems. It is used as default login shell in Linux systems and in
macOS. It can also be installed on Windows OS.
CSH (C SHell) – The C shell’s syntax and usage are very similar to the
C programming language.
KSH (Korn SHell) – The Korn Shell also was the base for the POSIX
Shell standard specifications etc.
Each shell does the same job but understands different commands and
provide different built in functions.
1. Bourne Shell (sh) – Bourne Shell is the original unix shell developed at
AT&T by Stephen Bourne. Bourne shell also named as (sh) programming
name. It used the symbol $. Bourne shell’s family is bourne, korn shells,
bash and zsh .
2. Korn shell (ksh) – korn shell is the unix shell developed by David korn
of Bell labs. Is is considered as the family member of Bourne shell as it
uses the $ symbol of Bourne shell. It is also names as ksh programmatic
ally and it most widely used shell.
3. Bourne Again Shell (bash) – It is the free version of Bourne shell and
comes with all UNIX/Linux systems as free with some additional features
like command line editing. Its program name is bash. It can read
commands from file called scripts.
4. C Shell (sh) – C shell is the UNIX shell created by Bill joy at California
university as an alternative to Bourne shell – Unix original shell. C shell
along with Bourne and Korn, are there most popular and commonly used
shells. csh is the program name for C shell.
Examples:
$ echo "I am $LOGNAME"
I am lhunath
$ echo 'I am $LOGNAME'
I am $LOGNAME
$ # boo
$ echo An open\ \ \ space
An open space
$ echo "My computer is $(hostname)"
My computer is Lyndir
$ echo boo > file
$ echo $(( 5 + 5 ))
10
$ (( 5 > 0 )) && echo "Five is greater than zero."
Five is greater than zero.
GETTING HELP
Type help after almost any command to bring up a help menu for that
command:
help:- The help command provides information on built-in commands.
help pwd:- At the prompt, enter ‘help’ followed by the command we wish to
learn more about.
help –d pwd:- With the ‘-d’ option, the help command will only return a
short description of the specified command.
help –s pwd:- With the -s option, help will return a short usage synopsis for
the specified command.
MAN PAGES
Type man before almost any command to bring up a manual for
that command. The man command in Linux is used to display the user
manual of any command that we can run on the terminal. It provides a
detailed view of the command which includes NAME, SYNOPSIS,
DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES, ERRORS,
FILES, VERSIONS, EXAMPLES, and AUTHORS.
Syntax :
$man [OPTION]... [COMMAND NAME]...
Example
$ man printf
$ man 2 intro
$ man -a intro
$ man -k cd
$ man -f ls
In the FHS, all files and directories appear under the root directory /,
even if they are stored on different physical or virtual devices.
Some of these directories only exist on a particular system if certain
subsystems, such as the X Window System, are installed.
Most of these directories exist in all UNIX operating systems and are
generally used in much the same way; however, the descriptions here
are those used specifically for the FHS, and are not considered
authoritative for platforms other than Linux.
Here is an example which will list all the files, with details (-la) in
the /dev directory and pipe the output to more. The /dev directory should
have dozens of files and hence ensure that more needs to paginate.
Notice the --More-- prompt at the bottom of the screen. Press SPACE to see
the next page and keep pressing SPACE until the output is finished.
Here is another pipe example, this time using the “wc” (word count) tool.
ls -1 /dev | wc
The first redirection will overwrite the file listing.txt while the
second will append to it. Now whatever text we type will be sent to the
file atextfile.txt until we press Control-D, at which point the file will be
closed and we will be returned to the command prompt. If we want to add
more text to the file use the same command but with two greater than signs
(>>).
INFORMATIONAL COMMANDS
ps:- ps shows process status. Use this command to displays a table of
all our own running programs or processes
w:- Used to show who is logged on and what they are doing
id:- Used to find out user and group names and numeric ID’s (UID or
group ID) of the current user or any other user in the server
free:- Displays the total amount of free space available along with the
amount of memory used and swap memory in the system, and also the
buffers used by the kernel
clear:- Used to clear the terminal screen
echo:- Used to display line of text/string that are passed as an
argument
more:- Used to view the text files in the command prompt, displaying
one screen at a time in case the file is large (For example log files)
FILE PERMISSIONS
Every file and directory in our UNIX/Linux system has following 3
permissions defined for all the 3 owners (User, Group, Other).
Read: This permission give we the authority to open and read a file.
Read permission on a directory gives we the ability to lists its content.
Write: The write permission gives we the authority to modify the
contents of a file. The write permission on a directory gives we the
authority to add, remove and rename files stored in the directory.
Consider a scenario where we have to write permission on file but do
not have write permission on the directory where the file is stored. We
will be able to modify the file contents. But we will not be able to
rename, move or remove the file from the directory.
Execute: In Windows, an executable program usually has an
extension ".exe" and which we can easily run. In Unix/Linux, we
cannot run a program unless the execute permission is set. If the
execute permission is not set, we might still be able to see/modify the
program code(provided read & write permissions are set), but not run
it.
Setting Permissions
chmod +rwx filename to add permissions.
chmod -rwx directoryname to remove permissions.
chmod +x filename to allow executable permissions.
chmod -wx filename to take out write and executable permissions.
CREATING SHELL PROGRAMS
For creating Shell programs we need to identify some terminologies
COMMENTS
When we are writing shell programs as a Systems Administrator
comments are doubly important. Chances are won't be at the site forever. At
some stage another Systems Administrator is going to come along and have
to decipher what our shell programs are doing.
Example
#!/bin/bash
# A Simple Shell Script To Get Linux Network Information
# Vivek Gite - 30/Aug/2009
echo "Current date : $(date) @ $(hostname)"
echo "Network configuration"
VARIABLES
A variable is declared without a $, but has a $ when invoked. Let's
edit our hello-world example to use a variable for the entity being greeted,
which is World.
#!/bin/bash
who="World"
echo Hello, $who!
OPERATORS
1. Arithmetic Operators
Example
#!/bin/sh
a=5
b=10
val=`expr $a + $b`
echo "a + b : $val"
val=`expr $a - $b`
echo "a - b : $val"
val=`expr $a \* $b`
echo "a * b : $val"
Relational operators
Relational operators are those operators which defines the relation
between two operands. They give either true or false depending upon the
relation. They are of 6 types:
‘==’ Operator : Double equal to operator compares the two operands. Its
returns true is they are equal otherwise returns false.
‘!=’ Operator : Not Equal to operator return true if the two operands are not
equal otherwise it returns false.
‘<' Operator : Less than operator returns true if first operand is lees than
second operand otherwse returns false.
‘<=' Operator : Less than or equal to operator returns true if first operand is
less than or equal to second operand otherwise returns false
‘>’ Operator : Greater than operator return true if the first operand is
greater than the second operand otherwise return false.
‘>=’ Operator : Greater than or equal to operator returns true if first
operand is greater than or equal to second operand otherwise returns false
Example
#!/bin/bash
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
if(( $a!=$b ))
then
echo a is not equal to b.
else
echo a is equal to b.
fi
if(( $a<=$b ))
then
echo a is less than or equal to b.
else
echo a is not less than or equal to b.
fi
LOGICAL OPERATORS
They are also known as boolean operators. These are used to perform logical
operations. They are of 3 types:
1. Logical AND (&&) : This is a binary operator, which returns true if
both the operands are true otherwise returns false.
2. Logical OR (||) : This is a binary operator, which returns true is
either of the operand is true or both the operands are true and returns
false if none of then is false.
3. Not Equal to (!) : This is a uninary operator which returns true if the
operand is false and returns false if the operand is true.
Example
#!/bin/bash
#reading data from the user
read -p 'Enter a : ' a
read -p 'Enter b : ' b
SINGLE QUOTES
Use single quote when we want to literally print everything inside
the single quote. Even the special variables such as $HOSTNAME will be
print as $HOSTNAME instead of printing the name of the Linux host.
$ echo 'Hostname=$HOSTNAME ;
Current User=`whoami` ;
Message=\$ is USD'
Hostname=$HOSTNAME ;
Current User=`whoami` ;
Message=\$ is USD
DOUBLE QUOTES
Use double quotes when we want to display the real meaning of special
variables.
$ echo "Hostname=$HOSTNAME ;
Current User=`whoami` ;
Message=\$ is USD"
Hostname=dev-db ;
Current User=ramesh ;
Message=$ is USD
Double quotes will remove the special meaning of all characters except the
following:
$ Parameter Substitution.
` Backquotes
\$ Literal Dollar Sign.
\´ Literal Backquote.
\” Embedded Doublequote.
\\ Embedded Backslashes.
CONDITIONAL COMMANDS
There are total 5 conditional statements which can be used in bash
programming
1. if statement
2. if-else statement
3. if..elif..else..fi statement (Else If ladder)
4. if..then..else..if..then..fi..fi..(Nested if)
5. switch statement
Their description with syntax is as follows
If statement
This block will process if specified condition is true.
Syntax:
if [ expression ]
then
statement
fi
if-else statement
If specified condition is not true in if part then else part will be execute.
Syntax
if [ expression ]
then
statement1
else
statement2
fi
if..then..else..if..then..fi..fi..(Nested if)
Nested if-else block can be used when, one condition is satisfies
then it again checks another condition. In the syntax, if expression1 is false
then it processes else part, and again expression2 will be check.
Syntax:
if [ expression1 ]
then
statement1
statement2
.
else
if [ expression2 ]
then
statement3
.
fi
fi
Switch Statement
The case statement works as a switch statement if specified value
match with the pattern then it will execute a block of that particular pattern
When a match is found all of the associated statements until the double
semicolon (;;) is executed. A case will be terminated when the last command
is executed. If there is no match, the exit status of the case is zero.
Syntax:
case in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac
Example 1 Example 2
#Initializing two variables #Initializing two variables
a=10 a=20
b=20 b=20
Example
CARS="bmw"
#Pass the variable in string
case "$CARS" in
#case 1
"mercedes") echo "Headquarters - Affalterbach,
Germany" ;;
#case 2
"audi") echo "Headquarters - Ingolstadt, Germany" ;;
#case 3
"bmw") echo "Headquarters - Chennai, Tamil Nadu,
India" ;;
esac
ITERATIVE COMMANDS
There are total 2 looping statements which can be used in bash
programming
1. while statement
2. for statement
While Statement
Here command is evaluated and based on the result loop will
executed, if command raise to false then loop will be terminated
Syntax
while command
do
Statement to be executed
done
For Statement
The for loop operate on lists of items. It repeats a set of commands for
every item in a list. Here var is the name of a variable and word1 to wordN
are sequences of characters separated by spaces (words). Each time the for
loop executes, the value of the variable var is set to the next word in the list
of words, word1 to wordN.
Syntax
for var in word1 word2 ...wordn
do
Statement to be executed
done
Example 1 Example 1
#Start of for loop for a in 1 2 3 4 5 6 7 8 9 10
for a in 1 2 3 4 5 6 7 8 9 10 do
do # if a = 5 then continue the
# if a is equal to 5 break the loop loop and
if [ $a == 5 ] # don't move to line 8
then if [ $a == 5 ]
break then
fi continue
# Print the value fi
echo "Iteration no $a" echo "Iteration no $a"
done done
Example
a=0
# -lt is less than operator
BREAK – CONTINUE
Break command is used to exit out of current loop completely
before the actual ending of loop. It comes handy when we don’t have prior
knowledge of how long will the loop last like when we user’s input is
required.
Example
#!/bin/bash
#breaking a loop
num=1
while [ $num –lt 10 ]
do
if [ $num –eq 5 ]
then
break
fi
done
echo “Loop is complete”
Example
#!/bin/bash
# using continue command
for i in 1 2 3 4 5 6 7 8 9
do
if [ $i –eq 5 ]
then
echo “skipping number 5”
continue
fi
echo “I is equal to $i”
done
EVALUATING EXPRESSIONS
The expr is a command line Unix utility which evaluates an
expression and outputs the corresponding value. expr evaluates integer or
string expressions, including pattern matching regular expressions. Most of
the challenge posed in writing expressions is preventing the invoking
command line shell from acting on characters intended for expr to process.
The operators available for integers: addition, subtraction, multiplication,
division and modulus for strings: find regular expression, find a set of
characters in a string; in some versions: find substring, length of string for
either: comparison (equal, not equal, less than, etc.)
Options Tag Description
--help Display a help message and exit.
--version Display version information and exit.
Examples:
To perform addition of To increament variable : To perform
two numbers: $ y=10 multiplication
$ expr 3 + 5 $ y=`expr $y + 1` $ expr 5 \* 3
output: 8 $ echo $y output:15
output:11
To find the To find substring of To find length of string
index/position of string: a=hello
character in a string a=hello b=`expr length $a`
a=hello b=`expr substr $a 2 3` echo $b
b=`expr index $a l` echo $b output: 5
echo $b output: ell
output: 3 ( as letter l is
at position 3.)
Syntax:
bc [ -hlwsqv ] [long-options] [ file ... ]
Options:
-h, {- -help } : Print the usage and exit
-i, {- -interactive } : Force interactive mode
-l, {- -mathlib } : Define the standard math library
-w, {- -warn } : Give warnings for extensions to POSIX bc
-s, {- -standard } : Process exactly the POSIX bc language
-q, {- -quiet } : Do not print the normal GNU bc welcome
-v, {- -version } : Print the version number and copyright and quit
Examples
Input : $ echo "12+5" | bc Input: Input: $ echo
Output : 17 $ x=`echo "12+5" | bc` "var=10;var" | bc
Input : $ echo "10^2" | bc $ echo $x Output: 10
Output : 100 Output:17
Input: $ echo "10>5" | bc Input: Input: $ echo
Output: 1 $ x=`echo "var=10;++var" | bc
"var=500;var%=7;var" | bc` Output: 11
Input: $ echo "1==2" | bc $ echo $x
Output: 0 Output:3
STRINGS
Finding length of a Finding substring of a Matching number of
string string characters in two
x=geeks strings
x=geeks sub=`expr substr $x 2
len=`expr length $x` 3` $ expr geeks : geek
echo $len echo $sub
GREP
The grep filter searches a file for a particular pattern of characters,
and displays all lines that contain that pattern. The pattern that is searched
in the file is referred to as the regular expression (grep stands for globally
search for regular expression and print out).
Syntax:
grep [options] pattern [files]
Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
with each such part on a separate output line.
Example
Display the file names that matches the pattern : We can just display
the files that contains the given string/pattern.
$grep -l "unix" *
Or $grep -l "unix" f1.txt f2.txt f3.xt f4.txt
Checking for the whole words in a file : By default, grep matches the
given string/pattern even if it found as a substring in a file. The -w
option to grep makes it match only the whole words.
$ grep -w "unix" geekfile.txt
ARRAYS
An array is a data structure consist multiple elements based on key
pair basis. Each array element is accessible via a key index number. An
Array is a data structure that stores a list (collection) of objects (elements)
that are accessible using zero-based index. In Linux shells, arrays are not
bound to a specific data type; there is no array of data type integer, and
array of data type float. An array can contain an integer value in one
element, and a string value in the element next to it.
If we are using the bash shell, here is the syntax of array initialization –