|
Shell
Scripts
Output Command
echo
Input command
read
read <variable name>
Ex:
read a
read b
read a b
To create and execute a
program
First open a file using ‘cat’ or ‘vi’ or any other editor, then in
it give the program then save the file. Then give the
execute permission to that file using ‘ch
mod’. Then execute that program give ‘file name’ then Enter. Example (1) Write
a program to clear the screen and display in the screen ‘UNIX programming’
$ vi
f1
clear
echo UNIX programming
:
wq (it is for save and quit from the ‘vi’
file)
$
chmod 777 f1
$
f1, enter
or
$ sh f1
(2)
Write a program to enter a number and display it on the screen.
clear
echo enter the number:
read n
echo the number is $n
Arithmetic
Operators :-
There are 5 arithmetic operators they are ‘+’ for add, ‘-‘for
subtract, ‘/’ for divide, ‘*’ for multiplied, ‘%’ for reminder of a division.
- Write a program to find the
sum of two entering number clear
echo enter the number:
read n
echo enter another number:
read m
s=’expr
$ n + $ m’
echo the sum is: $ s
- Write a program to find the
multiplication of two entering numbers
clear
echo ENTER TWO NUMBERS:
read a b
S=’expv
$ a “*” $ b’
echo THE PRODUCT IS: $ S
Conditional Statement - ‘IF’
Form of ‘if’ statement :-
a)
if <condition>
then
statement
fi
b)
if <condition>
then
statements
else
statements
fi
Relational operators
>
(greater then) à
-gt
>=
(grater then or equal to) à
-ge
<
(less than) = -lt
<=
(less than or equal to) = -le
=
(equal to) = -eq
#
(not equal to) = -ne
Example 1) if test $ a –qt $ b (if a>b)
Then
Fi
if test $ a –eq 0 (if
a=0)
Then
fi
1)
Write shell script to input a number and
check if it is positive of negative.
Clear
Echo
enter the number
Read
a
If
text $ a –gt 0
Then
Echo
Positive Number
Else
Echo
Negative Number
Fi
:wq
2)
Write a shell scrift
input a number and check if it is even or odd.
VI F2
Clear
Echo
Number
Read
n
S = ‘expr
$ n % 2
If
text $ s –eq 0
Then
Echo
Even Number
Else
Echo
Odd Number
Fi
:wq
‘IF’
Condition
if ($ s –eq 0)
3)
To input two number and to find the greater
one.
Vi Greater
Clear
Echo
INPUT A NUMBER:
read b
If
[ $ a –gt $ b]
Then
Echo
FIRST NUMBER IS GREATER THAN SECOND
Else
Echo
SECOND NUMBER IS GRATER THAN FIRST.
Fi
Nested ‘if’ Statement
How
to give
If
<condition>
Then
If
<condition>
Then
Statements
Fi
Else
If
<condition>
Then
Statements
Fi
Fi
- To input two number and
find which one is greater or both are same or not.
Clear
Echo INPUT TWO NUMBERS;
Read a b
If [$ a –gt $ b]
Then
Echo FIRST NUMBER IS GREATER
Else
If [ $ b –gt
$ a ]
Then
Echo SECOND NUMBER IS GREATER
Else
Echo BOTH ARE EQUAL
Fi
Fi
:wq
- Write a program to find
that the entering or inputing number is Positive,
Negative, or Zero.
Clear
Echo ENTER A NUMBER
Read n
If
[$ n –gt 0]
Then
Echo Positive number
Else
If
[$ n –lt 0]
Then
Echo Negative Number
Else
Echo ZERO
Fi
Fi
:
wq
4)
To input an year
and to check if it is a leap year or not.
Clear
Echo
ENTER A YEAR
Read
y
S =
‘ expr $ y % 4 ‘
If
test $ s –eq 0
Then
Echo
IT IS A LEAP YEAR
Else
Echo
IT IS NOT A LEAP YEAR
Fi
:wq
5)
Write a shell script to input a number and
display the menu given below, then accept the choice of the user and display the
result on the screen.
MENU
1.
ADDITION
2.
SUBTRACTION
3.
DIVISION
4.
MULTIPLICATION
Clear
Echo
ENTER ANY SL.NO OF BELOW MENU
ECHO MENU
Echo1. ADDITION
Echo2. SUBTRACTION
Echo3. DIVISION
Echo4. MULTIPLICATION
Read
n
If [$
n –eq 1]
Then
S = ‘expr
$ a + $ b ‘
Echo
THE RESULT IS: $ S
FI
IF [$
N –EQ 2]
Then
S = ‘Expr
$ a / $ b’
Echo
THE RESULT IS: $ S
Fi
If [$
n –eq 4]
Then
S = ‘expr
$ a “*” $ b’
Echo
THE RESULT IS: $ S
Fi
:wq
Write
the program to input the Name and Marks for 2 different subject Marks in 100 of
the student calculate the total mark, percentage of mark and grade. The grade
is calculated as follows
(a)
If the percentage of mark is greater than 80
Grade ‘Distinction’
(b)
If the percentage of mark is between 60 and
80 Grade ‘First class’
(c)
If the percentage of mark is between 50 and
60 Grade ‘Second class’
(d)
If the percentage of mark is between 30 and
50 Grade ‘Third class’
(e)
If the percentage of mark is less than 30
Grade ‘Failed’
Clear
Echo ENTER YOUR NAME:
Read a
Echo ENTER THE MARKS OF TWO SUBJECT:
Read b c
S =
‘expr $ b + $ c.’
S =
‘expr $ S / 2’
Echo THE TOTAL MARK IS: $ S
Echo PERCENTAGE OF MARK IS: $ P
If
[ $p –ge 80]
Then
Echo DISTINCTION
Else
If
[$P –ge 60]
Then
Echo FIRST CLASS
Else
If
[$ P –ge 50]
Then
Echo SECOND CLASS
Else
If
[$ P –ge 30]
Then
Echo THIRD CLASS
Else
If
[$ P –it 30]
Then
Echo FAILED
FI
FI
FI
FI
FI
:WQ
Looping Statements
Statements which are used to execute commands repeatedly for
a specific number of lines.
(1)
While
(2)
Until
While
How
to give
While [Condition]
Do
Statements
Done
1.
C=1
While [$ C –le 10]
Do
echo Hello
Done
Display ‘Hello’ infinity until we press ‘Del’ key because ‘value of C’ is not
increasing it will always one and it is less than 10.
2.
C=1
While [$ c –le 10]
Do
Echo $ C
C
= ‘expr $ C + 1’
Done
:wq
Display 1 to 10 then come out
3.
Write shell scift
to print the serious 100, 98, 96,……….0.
Clear
A=100
While [$a –ge 0]
Do
Echo $ C
C =
‘expr $ C -2’
Done
:wq
4.
Write a shell script to input 10
number and to calculate their sum.
Clear
C=1
S=0
While [$ C –le 10]
Do
Echo ENTER NUMBER:
Read a
S
=’expr $ S a’
C =
‘expr $ C + 1’
Done echo SUM OF 10 NUMBERS IS: $ S
:wq
5.
Input 10 number.
Calculate the average.
Clear
C=1
S=0
While [$C –le 10]
Do
echo ENTER NUMBER:
Read a
S =
‘expr $ S + $ a’
C=
‘expr $ C + 1’
Done
F=’expr
$ S / 10’
Echo AVERAGE OF TEN NUMBERS IS: $ F
:wq
6.
To input 10 numbers and to count the number
of Positive numbers, negative numbers and zeros.
Clear
C=1
R=0
S=0
T=0
While [$C –le 10]
Do
echo ENTER NUMBER:
Read a
If
[$ a –gt 0]
Then
R=’expr
$ R + 1’
Fi
If
[$ a –it 0]
Then
S=’expr
$ S + 1’
Fi
If
[$ a –eq 0]
Then
T=’expr
$ + 1]
Fi
C=’expr
$ C + 1’
Done
Echo NUMBER OF POSITIVE NUMBER IS: $ R
Echo NUMBER OF NEGATIVE NUMBER IS: $ S
Echo ENTER OF ZERO IS: $ T
:wq
6)
Input 10 numbers calculate the sum of
positive numbers and negative numbers saperately and
number of zero’s input.
Clear
C=1
R=0
S=0
T=0
While
[$C –le 10]
Do
echo ENTER NUMBER:
Read
a
If [$
a –gt 0]
Then
r=’expr $a + $r’
Else
If
[$a –lt 0]
Then
S= ‘expr
$ a + $ s’
Else
T=’expr
$ a + 1’
Fi
Fi
C = ‘expr
$ c + 1’
Done
Echo
THE TOTAL OF POSITIVE NUMBERS IS: $ r
Echo
THE TOTAL OF NEGATIVE NUMBERS IS: $ s
Echo
THE TOTAL ZERO: $ t
:wq
7)
Input 10 numbers and find the highest
number.
Clear
Echo
INPUT THE FIRST NUMBER:
Read
n
High
= $ n
Echo
INPUT THE OTHER NUMBER:
C=1
While
[$C –le 9]
Do
Read
n
If [$
n –gt $ high]
Then
High
= $ n
Fi
C=’expr
$ c + 1’
Done
Echo
HIGHEST NUMBER IS: $ high
8)
Input 10 numbers and find the highest and
the smallest.
Clear
Echo
ENTER A NUMBER:
Read
n
High
= $ a
Low +
$ a
Echo
ENTER OTHER NUMBER:
C = 1
While
[$ c –le 9]
Do
Read
a
If [$
a –gt $ high]
Then
High
= $ n
Fi
If [$
a –it $ low]
Then
Low =
$ a
Fi
C = ‘expr
$ c + 1’
Done
Echo
highest is: $ high
Echo
SMALLEST IS: $ low
:wq
To
copy a file from one login (Cu01) to (Cu02) second login
Cp / usr/student +/Cu01/VINOD.
That
is file ‘VINOD’ is copy to CU02. First give the copy command that is ‘cp’ then
space the give the correct path that is ‘/usr/student/Cu01/VINOD ‘ with file
name, then space, then’dot’ that is ‘.’
How
to give
Cp <path>.
That
is path= /usr/student/Cu01/VINDO
‘Break’ and ‘continue’
commands
which
is used in looping statements
Break command
While [condition]
Do
Statements
Break
Done
Note;-
‘Break’ will stop at where we enter
Example 1)
C=1
While [$ c –le 10]
Do
Echo NUMBER
Read n
If [$n –lt
0]
Then
Echo ONLY POSITIVE NUMBERS
Break
Fi
C= ‘expr
$ c + 1’
Done
‘Continue’ command
It will cancel the current
execution of the loop and starts it once again
How to give
While [condition]
Do
Statements
Continue
Done
Example 1)
C=1
While [$ c –le 10]
Do echo NUMBER
Read n
If [$ n –lt
0]
Then
Echo ONLY POSITIVE NUMBERS
Continue
Fi
C=’expr
$ c + 1 ‘
Done
1)
Write a shell scrift
to input 10 numbers between 50 and 100, if any other number is input it should
give an error message and stop the execution.
Clear
C=1
While [$ c –le 10]
Do
Echo NUMBERS ONLY BETWEEN 50 AND 100
Break
Fi c=’expr $ c +1’
Done
:wq
And -> -a, &&
OR
-> -o, | |
When the program execute any error come and how we can exit from the program
Press ‘ctrl’ + ‘.’ ‘Delete’, ‘Delete’ key press.
UNIX Division
In UNIX when we divide any number its answer we get
only the part that is we cannot get any decimal. Decimal is don’t accept the
‘UNIX’
That is, If S=15, r=2, then m=’expr $ S /$ r’
Echo $ m , will be ‘r’ but the correct answer is
‘7.5’. So we want to care when we divide.
‘/C’ with ‘echo’ command
Slash ‘c’ option given with echo command will display the message and then
cursor will wait on the same line.
How to give
I)
echo <message> “/c”
while [condition]
………………….
………………….
Echo “$ a \ c “
II)
Echo “ $ a \ C “
Note:- In ‘I’ we can give message also with the
answer, but in ‘II’ we get answer only in the same line. That is in ‘I’ THE SUM
IS: 21 but in ‘II’ 21.
In
‘I’ we give the first echo that is echo with message, is give before the
‘while’. Then at the answer part give second ‘echo’ with variable name. But in
‘II’ give ‘echo’ at the answer part.
‘And’ and ‘or’ in while condition or give two condition together.
And -> ‘-a’, ‘&&’
Or
-> ‘-o’, ‘||’
It
is for give two condition in the ‘while’statements.
That is
While [$ a –gt 10 && $ a –lt
15]
Or
While [$ a –gt 10 –a $ a –lt
15]
That is number between 10 and 15. But in ‘OR’
While [$ b –gt 50 $ b –gt
100]
While [$ b –gt 50 | | $ b –gt
100]
That is number is less than 50 or greater than 100 only.
How to give
‘And’
While [condition –a condition]
While [condition && condition]
‘Or’
While [condition -0 condition]
While [condition | | condition]
1)
Write a shell script to input 10 odd
numbers. If an even number is input it should display an error message and
start the execution once again.
Clear
C
= 1
While [$ c –le 10]
Do
Echo NUMBER INPUT:
Read n
S
= ‘expr $ n % 2 ‘
If
[$ s –eq 0]
Then
Echo ERROR!!! INPUT ODD NUMBERS ONLY.
Continue
Fi
C
= ‘expr $ c + 1’
Done
;WQ
2)
Write a shell script to input a number and
to calculate its factorial
That is if we input 5, factorial is: 5x4x3x2x1=120
Clear
F
=1
Echo ENTER A NUMBER
Read n
While [$ n –ge 1]
Do
F
= ‘expr $ f “*” $ n ‘
N
= ‘expr $ n – 1 ‘
Done
Echo FACTORIAL IS: $ f
:wq
3)
Input the value, then revenue the value and
display on the screen? That is 102 as 201.
Clear
Echo NUMBER:
Read n
While [$ n –gt 0]
Do
R
= ‘expr $ n % 10 ‘
Echo “$ r \ c “
N
= ‘expr $ n / 10’
Done
:wq
4)
Input the value of ‘x’ and ‘y’ and find the
‘xy’. That is x=2, y=3, 2x2x2=8
Clear
F=1
Echo INPUT THE NUMBER:
Read x
Echo INPUT HOW MUCH TIME NUMBER SHOULD MULTIPLIE:
Read y
While [$ y –gt 0]
Do
F=’expr $ S “*” $ f ‘
Y=
‘expr $ y – 1’
Done
Echo THEN GET: $ f
:wq
5)
A shell script to input a number and
calculated the sum of its digits. That is 2893=2+8+9+3=21
Clear
S=0
Echo NUMBER:
Read n
Echo SUM IS: “\C”
While [$ n –gt 0]
Do
R=’expr $ n % 10’
S=
‘expr $ s + $ r ‘
N=
‘expr $ n / 10’
Done echo $ S
:wq
UNTIL
Until [condition]
Do
Statements
Done
In
until the condition is false it will continue, but when the condition give
become true it will stop the looping. But in while when the condition is true
it will do, but condition become false it will stop looping. So ‘Until’ is just
opposite of ‘while’.
C=1
Until [$ C –gt 0]
Do
Echo $ C
C
= ‘expr $ C + 1’
Done
1)
Write a shell script to print the series
100, 98,96,…..0 using ‘Until’.
C=100
Until [$ c –lt 0]
Do
Echo $ C
C=
‘expr $ C – 2’
Done
:WQ
‘FOR’ Statement.
For ,<Variable name> in <list>
Do
Statements
Done
‘For’, it is used for list more than one items. At the list place we can give
list of items, then we execute this we can get one by one will list at the
screen. We cannot give any condition at the ‘list’ place. But at ‘statements’
we can give.
S=0
For x in 10 20 30 40 50
Do
S=
‘expr $ S + $ x
Done
Echo $ S
Note:- Answer will be 150.
1)
Assume that there are three files A1,A2,A3
in the disk. Write a shell script to display the contents of these files.
Clear
For x in A1,A2,A3.
Do
Cat $x
Done
:Wq
2)
Write a shell script to input two file names
change the name of the file to the second file.
Clear
Echo ENTER THE OLD NAME AND NEW FILE NAME:
Read a b
Mv $ a $ b
3)
Write a shell script to input three file
name. The copy the contents of first two files to the third file and we can see
it on the screen also.
Clear
Echo ENTER A NEW FILE NAME:
Read a
Echo ENTER TWO EXISTING FILE NAMES:
Read b c
Cat $ b $ C > $ a
Cat $ a
:wq
How we can give number to the lines in the file.
:
Set nu
Or
:
Set number
To
remove the line number from the file
:
set nonu
Or
:
set no number
Copy one file to another file
(1)
$ Cat file A > file B
(2)
$ Cat file x file y > file z
(3)
$ Cat file R file S >> file T
(1)
It is to copy the contents of File A to the
‘File B’. (2) It is to copy the contents of two file that is ‘File X’ and
‘File Y’ to the another file ‘File Z’. (3) It is to
copy the contents of two file but the another file
also have the contents that is in the three files there is contents. This
command will give the contents of ‘File T’, ‘File R’ and ‘File S’ in the file
‘File T’. That is it will
oppend the file.
File checking
-r
<file name>To check the read permission
-w
<file name>To check the write permission
-x
<file name>To check the executable permission
-f
<file name> To check the file is exist and is an
ordinary file
-d
<file name> To check if it is a directory.
If
[-r $ f] It is to check that the file in the variable ‘f’ have read permission
If
[-w $ f] It is to check that the file in the variable ‘f’ have write permission
If
[-x $ f] It is to check that the file in the variable ‘f’ have execute
permission
If
[-f $ f] It is to check that the file in the variable ‘f’ is exist or not
If
[-d $ f] It is to check that the name in the variable ‘f’ is a directory or not.
1)
Write a shell script to input a file name
and to check its permissions.
Clear
Echo ENTER THE FILE NAME
Read h
If
[-f $ h]
Then
If
[-r $ h]
Then
Echo IT HAS READ PERMISSION
Else
Echo IT HAS NO READ
Fi
If
[-w $ h]
Then
Echo IT HAS WRITE PERMISSION
Else
Echo IT HAS NO WRITE
Fi
If
[-x $ h]
Then
Echo IT HAS EXECUTE PERMISSION
Else
Echo IT HAS NO EXECUTE PERMISSION
Fi
Else
Echo NO SUCH FILE
Fi
:wq
2)
Write a shell script to input a file name
check if it is a directory or and ordinary file. If it is a directory display
the contents of the directory, if it is a file display the contents of the file.
Clear
Echo FILE NAME
Read r
If
[-d $ r]
Then
Ls
$ r
Else
If
[-f $ r]
Then
Cat $ r
Fi
Fi
:Wq
3)
Write a shell script to print a first ‘n’
number of the Fibonacci serious. We input the number ‘n’
then we get the list of that number. That is 0,1,2,3,5,8,…..
Clear
A=0
B=1
Echo ENTER THE NUMBER
Read n
Clear
Echo $ a
Echo $ b
C=2
While [$ c –lt $ n]
Do
S=’expr $ a + $ b ‘
Echo $ S
A=$ b
B=$ s
C=’expr $ c + 1’
Done
:wq
4) Write a shell script to copy two file’s contents to an another existing file
that is file have to contents and it can be see on the screen also.
Clear
Echo ENTER FILE NAME:
Read a
Echo ENTER ANOTHER TWO FILE:
Read b c
Cat $ b $ c >> $ a
Cat $ a
:wq
Command Line Arguments
It
is values which are given in the command line along with the file name. At a
time we can give ‘nine’ command line arguments [by default]. The first command
line argument will be stored in the variable $ 1, $ 0 stores the file name or
program file’s name.
$1) First command line argument.
$2) Second command line argument.
$3) Third command line argument.
$4) Forth command line argument.
$5) Fifth command line argument.
$6) Sixth command line argument.
$7) Seventh command line argument.
$8) Eight command line argument.
$9) Ninth command line argument.
$0) Name of the program file.
Vi F1
Clear
Echo $ 1
:wq
$
Ch mod 777 F1
$
F1 AAA
AAA it will display on the screen.
1)
Write a shell script to find the sum of two
numbers, using command line argument.
Vi
LESH
clear
S=
‘expr $ 1 + $ 2’
Echo SUM IS : $ S
:wq
$
Ch mod 777 F1
$
LESH 100 50
SUM IS: 150. It will display on the screen.
2)
Write a shell script to display the contents
of a file given as command line argument.
Clear
Cat $ 1
Command line Arguments.
$
0 Name of the program file
$ # Number of command line Arguments.
$ * List of command line Arguments.
Vi
VINU
Clear
Echo THE NAME OF THE PROGRAM IS: $0
Echo THERE ARE 4 3 COMMAND LINE ARGUMENTS
ECHO the command line arguments are 4 8
:Wq
$
Ch mod 777 VINU
$
VINU 1 2 3 4 5 6
ANSWER ON THE SCREEN
THE NAME OF THE PROGRAM IS: VINU
THERE ARE 6 COMMAND LINE ARGUMENTS
THE COMMAND LINE ARGUMENTS ARE 1 2 3 4 5 6
3)
Write a shell script to copy the contents of
one file to another file and remove the first file. [The file names are given
like command line arguments]
Vi
VINOD
Clear
Cp
$ 1 $ 2
Rm $ 1
:wq
$
Ch mod 777 VINOD
$
VINOD a b c d
4)
Write a shell script to calculate and
display the sum of the number given as command line arguments.
Clear
S=0
For x in $ *
Do
S=’expr $ S + $ x’
Done
Echo $ S
:wq
5)
Write a shell script to calculate the length
of string.
Clear
Echo ENTER THE STRING:
Read S
A=’echo $ S | wc –c’
A=’expr $ a -1’
Echo $ a
:wq
String Comparisons
If
we want to compare the two string are equal, we give
like this:
If
[$ a = $ b]
If
we want to known the two strings are not equal, we give like this:
If
[$ a ! = b]
String:- It is a group of character.
1)
Input a string then find the length of the
string and reverse the string.
Clear
Echo INPUT STRING
Read s
L=
‘expr $ s |
wc –c ‘
L
= ‘expr $ l -1’
Echo $ l
While [$ l –ge 1]
Do
X=
‘echo $ S | Cut –c $ l’
Echo “$ x \ C”
L=’expr $ l – 1’
Done
:wq
Grep
It
will display the lines in which the word is
grep is used to find the words.
Grep “pattern” <file name>
Example grep “ manager”
LESH:- It will display all lines in which the word ‘manager’ is found in the
file ‘LESH’.
1)
Input a string and a character in that
string. Then print the position of occurance of
that character in the string.
Clear
Echo ENTER A STRING
Read S
L=’echo 4 S | wc –c’
L=’expr $ l – 1 ‘
Echo ENTER A CHARACTER IN THE STRING
READ H
C=1
WHILE [$ C –LE $ L]
DO
X=’ECHO $ S | cut –c $ c’
If
[$ x = $ h]
Then
Echo POSITION IS: $ C
Fi
C=
‘expr $ C + 1’
Done
:wq
2)
A shell script to input a string and a
character in that string. Then display its position of the first
occurance of the character in the string.
Clear
Echo ENTER A STRING
Read s
L=’echo $ S | Wc –C’
L=’expr $ l – 1 ‘
Echo ENTER A CHARACTER IN THE STRING
Read h
C=1
While [$ C –le $ l]
Do
X=’echo $ S | Cut –C $ C ‘
If
[$ x = $ h]
Then
Echo POSITION IS: $ C
Break
Fi
C=’expr $ C + 1’
Done
:wq.
HOME
<<PREVIOUS |