Unix Notes Bca
Unix Notes Bca
History of UNIX:
By 1969, Ken Thompson , one of the member of AT & T Bell Labs felt MULTICS
is too complex , slow progress and had many problems so, he wrote first version of
UNIX as UNICS ( UNIPLEXED INFORMATION COMPUTING SYSTEM) in
Assembly Language.
In 1972, Ken Thompson and Dennis Ritchie rewrote the UNIX in C language to
achieve the portability.
AT&T made Unix available to universities and commercial firms, as well as the
United States government, under licenses.
There are various UNIX variants in the market like Solaris UNIX, AIX, HP UNIX
BSD etc.
Features of UNIX:
The features of UNIX are:
1. Multitasking:
A user can run multiple programs at the same time so UNIX is called as
Multasking .
2. Multi-user System:
Several users can use the UNIX System at the same time.so it is called as
Multiuser system.
3. Portability:
The System hides the machine architecture from the user, making it easier to write
applications that can run on micros, mins and mainframes.
11. Windowing System :
XWINDOWS is the first Graphical User Interface for UNIX. It supports network
applications and also can use multiple windows to run the programs
2. Directory Files:
A directory contains no data but keeps all details of the files and sub directories.
Directories store both special and ordinary files. A directory file contains an entry
for every file and subdirectory that it stores. Each entry has two components.
Files in Unix System are organized into multi-level hierarchy structure known as a
directory tree. At the very top of the file system is a directory called “root” which
is represented by a “/”. All other files are “descendants” of root.
/:
The slash / denotes the root of the filesystem tree.
/bin :
The /bin directory contains certain fundamental utilities, such as ls or cp.
This is a directory where all commonly used unix commands are stored.
/sbin: or /usr/bin :
This directory stores all binary programs..There are some commands where user
cannot execute but System Administrator can execute. Such commands are
stored in this directory.
/etc :
This directory contains system configuration files and system databases. It also
contained maintenance utilities such as init. Login name and passwords etc.
/boot :
This directory contains all the files that are required for successful booting
process.
/dev :
This directory contains all device files such as fdsk, rdsk etc.
/home :
On many systems user files are stored in this directory. It contains the home
directories for the users.
/lib : or usr/lib:
This directory contains system libraries, and some critical files such as kernel
modules or device drivers.it also stores the required libraries and data files for
programs stored within /usr
/tmp:
This directory contains all temporary files
/mnt :
Stands for “mount”. contains filesystem mount points.
/proc :
This directory contains virtual filesystem showing information about processes
as files.
/root :
The home directory for the superuser “root” – that is, the system administrator.
/var :
This directory contains all user printing jobs and incoming and outgoing mails.
• /var/mail : This is the place where all the incoming mails and outgoing mails
are stored.
• /var/spool : Spool directory. Contains print jobs, mail spools and other
queued tasks.
Unix Architecture:
Kernel
The kernel is a part of the operating system. It interacts directly with the hardware
of the computer and manages all operations.
The main functions of the kernel are,
• Memory management
• process management
• Controlling access to the computer.
• Maintaining the file system.
• Handling interrupts.
• Handling errors.
• Performing input and output services.
• Allocate the resources of the computer among users.
The kernel is the main part of the Architecture of Unix OS.
The Shell
It is an interface between the kernel and the user. It reads the commands and then
interpret them and sends a request to execute a program. So, the shell is also called
a command interpreter.Users can interact with the shell using shell commands.
Shell is present in the /bin/sh directory. There are different types of shells. They are
1. Bourne Shell
2. C shell
3. Korn Shell
Functions of shell:
1. File opening.
2.File writing.
3.Executing programs.
4.Obtaining detailed information about the program.
5.Termination of the process.
6. Getting information about time and date.
System Utilities:
System Utilities are powerful tools that performs a special task.System Utilities
also includes System processes called Daemons.
Utility programs are software programs that add functionality to your computer or
help your computer perform better. These include antivirus, backup, disk repair,
file management, security, and networking programs.
Application Programs:
The applications and utility layer in Unix includes the word processors, graphics
programs, database management programs, commands etc.
The application programs provide an application to the end users. For example, a
web browser is used to find information while gaming software is used to play
games
File System:
UNIX File System includes hardware devices which can be a file, directory or sub
directory.These are the files that contain data, text and program instructions. Store
user information like an image drawn or some content written.UNIX files are local
or distributed.
Shell:
It is an interface between the kernel and the user. It reads the commands and then
interpret them and sends a request to execute a program. So, the shell is also called
a command interpreter.Users can interact with the shell using shell commands.
Types of shells:
There are mainly 4 shells namely
1. Bourne Shell (sh)
2. C shell (csh)
3. inode block
4. data block
1. Boot block:
The Boot block contains bootstrap program that is used to load unix Operating
System.The bootstap program is stored in ROM.The bootstrap program gets
activated as soon as system is booted. The kernel is loaded into main memory
during the booting process.
2. Super Block:
The Super block contains the actual information of entire UNIX File System.It
maintains all free inode blocks and data blocks.
It contains the number of data blocks allocated.
It contains the number of free data blocks available.
It contains the number of free inode blocks available.
It contains the size of UNIX File System.
3.Inode block:
An inode is a datastructure in unix that contains information about a file within the
file systeem.
It contains the following information
1. attributes of the file
2. contains file permisions
3. contains the size of the file
4. date and time of last modification of the file.
$ ls -i prime.sh
4. data block:
The data block actually contains the content of a file.The address of data blocks are
available in linked list in the inode block.
There are 10 direct blocks and 3 indirect blocks.
Unix Commands:
Unix commands are inbuilt programs that can be invoked in multiple ways.
Unix has large set of commands.
Syntax:
$ command options filenames
Example:
$ ls -i fact.sh <enter>
5467 is inode number of fact.sh
$ls -i <enter>
prime.sh 2345 fact.sh 5467 programs 6789 even.sh 5677
$ ls <enter>
it lists all file and directories
$wc fact.sh <enter>
6 15 45 fact.sh
$ wc -l fact.sh<enter>
6
$ wc -w fact.sh <enter>
15
$ wc -c fact.sh<enter>
45
prime.sh fact.sh programs even.sh menu.sh unixprogs
features of unix command:
1.A unix command is a program that performs a specific task
2. All unix commands are written in lowercase letters only
3. Almost all unix commands are cryptic
Ex: cat for concatenation
wc for word count
4. options present in a command are indicated by hyphen symbol (-)
Types of commands in UNIX:
There are 2 types of commands in unix. They are
1. Internal commands
2. External commands
Internal commands:
The commands that are built into the shell are called internal commands.
Internal commands are executed directly by the shell.Internal commands will not
have a separate process. They will not find in PATH directory. All Internal
commands are also called as shell built in commands. The execution speed is high.
Example:
echo cd mkdir rmdir PATH etc.
External commands:
External commands are the commands that are executed by the kernel. These
commands will have a process id running for it. External Command existed in
any PATH directory like /bin, /usr/bin etc. like ls, sed, mv. These commands having
independent existence in /bin, /usr/bin, or any other directory. External commands
paths can be found using type command
Example:
ls sed cat who etc.
1.pwd:
This command is used to display the path of current working directory.
$ pwd <enter>
/ home / raju
$ cd raj <enter>
$ pwd <enter>
/ home / raju/ raj
$ cd\ <enter>
$ pwd <enter>
/ home/raju
mkdir:
the mkdir command is used to create a directory.
$mkdir labprgs
It will create a directory called labprgs
rmdir:
The rmdir is used to remove or to delete the directory
$rmdir labprgs
It will remove the directory labprgs
4. cd (change directory)
The cd commandis used to move from one directory to another directory
$ pwd <enter>
/ home / raju
$ cd raj <enter>
$pwd
/ home / raju / raj
--------------------
--------------------
press <ctrl +d >
$
4. appending files:
$ cat file1 >> file4 <enter>
$cat file4 <enter>
file4
file1
$
$ls-l:
The ls -l option is used to display long listing of files
$ ls -l<enter>
-rw_rw_ r__ 1 raju student 48 2021.05.10 11:30:35 fact.sh
drw_rw_ r__ 1 raju student 148 2021.05.10 11:30:35 prgs
-F:
$ ls -F
It displays the directories with / which indicates they are directories.
3. wc: ( word count )
The wc command is used to count number of characters, words, lines in one or
more files.
Options:
there are 3 options with wc command.
-l: for counting lines
-w: for counting words
-c: for counting characters
$ wc fact.sh<enter>
6 12 45 fact.sh
$ wc -l fact.sh<enter>
6 fact.sh
$ wc -w fact.sh<enter>
12 fact.sh
$wc -c fact.sh<enter>
45 fact.sh
$ wc < fact.sh<enter>
6 12 45
4. head:
head cmd is used to display the top 10 lines of the file by default.
Ex:
$ head Filename <enter>
It displays first ten lines of the file filename by default.
5. tail
tail cmd is used to display the last 10 lines of the file by default.
Ex:
$ tail Filename <enter>
It displays last ten lines of the file filename.
6.cp: (copy)
The cp command is used to copy a file or group of files.
Ex:
$cp file1 file2 <enter>
the contents of file1 will be copied to file2.
7. rm (remove)
The rm is used to remove or delete one or more files.
$rm file1 <enter>
it will delete the file file1
$rm f* <enter>
it will delete all the files starting with letter f.
$rm -r * <enter>
it removes all the files along with directories and subdirectories of current
directory
8.mv(move):
the mv command is used to move or rename files and directories.
It takes two arguements
it does two things
1. it renames a file or directory
9. touch :
the touch command is used to create new empty files with zero bytes
$touch f1 f2 f3 f4
It will create empty files f1 f2 f3 and f4 with 0 bytes
It removes or deletes all files that begins with first letter ‘b’.
2. ?:
‘?’ represents any one character
$ls ?a?n <enter>
main rain pain
$ ls ???
aaa bbb
3. [...] :
it displays any one character from the enclosed list.
It indicates set of characters and will match a single character within the enclosed
list.
$ls chap[123] <enter>
chap1 chap2 chap3
$ls chap[abc]<enter>
chapa chapb chapc
$ls chap[a-z] <enter>
chapa chapb chapc chapg chapk chapm chapp chapr chapx chapz
4.[!...]
it displays any one character except those enclosed in the list.
Positional parameters:
The command line arguments can be accessed using $1,$2,$3,$4,$5,$6,$7,$8 and
$9
There are 9 positional parameters.They are
$1 to $9
The values of command line arguments are stored in special varaibles called
positional parameters.
First argument is stored $1 and second argument is strored in $2 and so on.
Set command:
set cmd is used to store the value of command line arguments into positional
parameters.
$ set Srinivas is studing 2nd bca in pims. He is from tumkur
$echo $1
Srinivas
$echo $9<enter>
He
$ shift 4
$ echo $1
bca
special parameters:
3. execute(x)
read(r):
The read permission allows the user to view the contents of the file.
The read permission on directory will list the contents of a directory.
The octal value for read permission is 4.
write(w):
The write permission allows the userS to modify the contents of the file.
The write permission on directory allows user to edit (add or delete) the
Contents of a directory. The octal value for write permission is 2.
execute(x):
The execute permission allows user to execute or run the file.
The execute permission on directory allows users to access files or
subdirectories.The octal value for execute permission is 1.
Representation of file permissions:
There are 2 ways to represent file permissions. They are
1. Textual representation (rw-rw-r--)
2. Octal representation (664)
chmod command:
The chmod command allows user to set, add or delete the file permissions.
The chmod command is used in 2 ways
1. Symbolic mode or Textual representation
2. absolute mode or octal representation
1.Symbolic mode:
It is easiest way to modify the file or directory permissions.
Using this mode , user can add, delete or specify the permissions by using
operators
+ adds the permissions to the file or directory
- removes the permissions from a file or directory
= sets the designated permissions
2. absolute mode:
The absolute mode uses octal representation to set permissions.
Each permission is assigned a value.
Number octal permission reference
0 no permission ---
1 Execute Permission --x
2 Write Permission -w-
3 execute and write permissions -wx
4 read permission r--
5 read and execute permissions r-x
6 read and write permissions rw-
7 read,write and execute permissions rwx
Filters:
Filters are set of commands that takes input from standard input, performs the
operation and sends the output to the standard output.
Filter is a small program that transforms plain data that can be used with other
filters and pipes to form a series of operations to produce the results.
The most common filters are:
1. cat
2. wc
3. head
4. tail
5. grep
6. tr
7. uniq
8. cut
9. paste
10. sort
Advanced Filters:
1. sed
2. awk
1. grep command:
grep stands for global regular expression print.
It is most popular filter.
Grep command filters the contents of the file that makes search easy.
It scans a file for occurrence of a pattern and displays the selected pattern.
grep command can also selects the lines not containing the pattern.
Syntax:
$ grep options pattern filenames
$ cat emp.lst <enter>
111 | Anil | Director | Sales | 25000
222 | Charan | director | production | 35000
333 | Siva | teacher | computers | 30000
444 | Shiva | Manager | Sales | 40000
555 | Kiran | Manager | production | 20000
$ grep “Sales” emp.lst <enter>
111 | Anil | Director | Sales | 25000
444 | Shiva | Manager | Sales | 40000
$ grep “teacher” emp.lst <enter>
333 | Siva | teacher | computers | 30000
options:
-c:
-v:
The -v option is used to display all the lines except the lines containing the pattern.
$ grep -v “Sales” emp.lst <enter>
222 | Charan | director | production | 35000
333 | Siva | teacher | computers | 30000
555 | Kiran | Manager | production | 20000
-i:
It displays the output ignoring the case of the pattern. It treats uppercase and
lowercase letters same.
$ grep “director” emp.lst <enter>
222 | Charan | director | production | 35000
$ grep -i “director” emp.lst <enter>
111 | Anil | Director | Sales | 25000
222 | Charan | director | production | 35000
-e:
The -e option is used to search multiple patterns at a time.
$grep -e “Sales” -e “teacher” emp.lst <enter>
111 | Anil | Director | Sales | 25000
333 | Siva | teacher | computers | 30000
444 | Shiva | Manager | Sales | 40000
-l:
2. sort command:
sort command is used to sort the contents of a file either in ascending order or
descending order.
It is also used to merge two or more sorted files.
By default, sort command sorts the date in ASCII sequence.
That is,
whitespace
numeric data
uppercase letters
lowercase letters
options:
-r:
the -r option is used to sort the file in descending order
-d: (dictionary sort)
it is used sort the data according to the dictionary
whitespace
special characters
numeric data
uppercase letters
lowercase letters
-m: (merge)
the -m option is used to merge two or more sorted files
tr: translate:
comparing files:
There are 3 unix commands for comparing the files.
1. cmp (compare)
2. diff(difference)
3. comm(common)
1. cmp:
the cmp command compares the files byte by byte.
The cmp command without any option stops at when first byte difference
is reported.
When files are identical, no message is displayed.
Syntax:
$cmp options file1 file2
Ex:
$cmp file1 file2
options:
-l:
the -l option is used to list all the differences found in the files byte by byte
$cmp -l file1 file2
2. diff:(differnce)
The diff command shows line by line differences between two files.
The differences are identified such that the first file can be modified making it to
match the second file.
$diff file1 file2 <enter>
2 c 2 (2-line to be changed of a file)
<789
>as9
3. comm:(common)
The comm command is used to find the identical data in two files.
It works on two sorted files.
It compares the files line by line and displays the results in 3 columns.
The first column contains the unique lines in file1
The second column contains the unique lines in file2
The third column contains the unique lines in both file1 and file2.
Syntax:
$comm options file1 file2
$ cat file1
agra
bangalore
chennai
$cat file2
bangalore
chennai
delhi
$comm file1 file2
agra bangalore
chennai
delhi
Shell Programming
Shell:
Every Unix System consists of at least one shell.
Shell acts as an interface between user and kernel.
When user logs into the Unix System,a prompt called shell prompt appears on the
screen.
The shell prompt could be $,% or #
Types of shells:
The most popular shells in unix or linux sre
1. Bourne Shell (sh)
2. C shell (csh)
3. Korn Shell (ksh)
4. Bourne-again shell (bash)
1. Bourne Shell:
It is most popular shell used in UNIX System. It is the original UNIX Shell. It is
introduced by the author called Stephen Bourne at AT & T Bell Labs in 1977.The
source code is developed in C language. The executable filename of Bourne Shell
is sh. Default prompt is $
2. C Shell:
The C Shell was developed by Bill Joy at University of California in 1978. It is
similar to C language.C shell is not avialable in all machines.tc shell is a free
version of C shell. It include c type expression syntax and built in arithemetic.The
executable filename of C shell is csh.Default prompt is %.
3. Korn Shell:
Korn Shell was developed by David Korn in 1983 at AT & T Bell Labs. Korn Shell
combines the features of both bourne shell and c shell. It has c type array, functions
and string manipulating facilities.The executable filename of Korn Shell is
ksh.Default prompt is $
4. Bourne-again Shell:
The Bourne Shell was developed by Brain Fox in 1989.it is most widely used
shells today.It inherited all the features from Bourne Shell and Korn shell . This
shell supports piping, control statements,command substitution etc.The executable
filename of Bourne again shell is bash.Default prompt is $.
shell variables:
There are 3 shell variables
1.System variables or environmental variables
2. user defined variables or local variables
3. readonly vaiables or constants
1.System variables or environmental variables:
System variables should be written in UPPERCASE letters
they are predefined variables in the Syste,
ex: PATH, PS1, IFS,HOME,SHELL,LOGNAME
LOCAL VARIABLES:
THESE are the variables that are defined by the user. They will exist only during
the execution of the program.
Ex:
i=10
here i is called a local variable.
Readonly variables(constants)
the values of readonly variables are always fixed and does not change during the
execution of a program.
To define a constant in unix, the keyword ‘readonly’ is used.
x=100; #local variable
readonly x=100; #constant
hello
to print values of the variables
$ echo $variable
Ex:
$i=10
$ echo $i
10
options:
-n:
The -n option is used to remove the newline (\n) from the output
-e:
The -e option is used to recognize the escape sequences like \t, \n, in the input
string.
read:
The read statement is used to accept the input and stores in a variable.
read is a built in command
Syntax:
$read variable1 variable2 variable3 ------variable n
Ex:
$ read a b c
# here a,b and c are variables
Example:
clear
echo “Enter any value:” 10
read a
test command:
The test command in unix is used to evaluate the expression
test is a built in command that returns true(0) or false (1) depending on the
evaluation of the expression.
The test command is used to find the equality of strings or file check and compares
the values.
Syntax:
test expression
(or)
[expression]
Types of tests:
There are 3 tests in unix. They are
1. numeric test
2. string test
3. file test
1. numeric test:
The numeric test comprises of relational and logical operators.
Relational operators:
There are 6 relational operators. They are
operator meaning
-eq equals to
-ne not equals to
-lt less than
-le less than or equals to
Example:
# Shell Script to find sum of two numbers
clear
echo “Enter any two numbers:”
read a b
sum=`expr $a + $b`
echo “sum of $a and $b is: $sum`
commands or statements
fi
(or)
if [ condition ];then
commands or statements
fi
Example:
clear
echo “enter any number(1-100):” 50
read n
if [ $n -le 100 ]
then
echo “ given number is less than 100”
fi
if-then-else-fi statement:
Syntax:
if [ condition ]
then
commands or statements (true block)
else
commands or statements (false block)
fi
Example:01
clear
echo “Enter any number:” 20
read n
if [ $n -lt 0 ]
echo “$n is negative number”
else
echo “$n is positive number”
fi
Example:02
clear
echo “Enter your age:
read age
if [ $age -ge 18 ]
echo “ Eligible for voting”
else
echo “ not eligible for voting”
fi
Example :03:
clear
echo “Enter any year:” 2020
read year
r=`expr $year % 4`
if [ $r -eq 0 ]
then
echo “$year is a leap year”
else
echo “$year is not leap year”
fi
Example :04
clear
echo “Enter any 2 numbers:”
read a b
if [ $a -gt $b ]
then
echo “$a is large number”
else
echo “$b is large number”
fi
Example :05
-
-
-
elif [ condition n ]
then
commands to execute if condition n is true
else
commands to execute if no condition is true
fi
Syntax :02
if [ condition1 ]
then
commands to execute if condition1 is true
else if [ condition2 ]
then
commands to execute if condition 2 is true
else if [ condition3 ]
then
commands to execute if condition3 is true
-
-
-
else if [ condition n ]
then
commands to execute if condition n is true
else
commands to execute if no condition is true
fi
fi
fi
fi
# shell program to find largest of 3 numbers (part B 2nd Program)
clear
echo “Enter any 3 numbers:”
read a b c
if [ $a -gt $b -a $a -gt $c ]
then
echo “$a is largest number”
elif [ $b -gt $a -a $b -gt $c ]
then
echo “$b is largest number”
else
echo “$c is largest number”
fi
then
echo “$a is smallest number”
elif [ $b -lt $a -a $b -lt $c ]
then
echo “$b is smallest number”
else
echo “$c is smallest number”
fi
commands or statements
else
commands or statements
fi
else
commands or statements
fi
Example:
clear
echo “Enter username:”
read user
if [ $user = “abcd” ]
then
echo “Enter password:”
read ps
if [ $ps = “1234”]
then
echo “Hello $user”
else
echo “Wrong Password”
fi
else
echo “wrong username”
fi
case-esac statement:
It is similar to switch statement in C language.
It is used to select one choice among multiple choices.
Syntax:
case $variable in
value1) statements or commands
;;
value2) statements or commands
;;
-
-
-
value n) statements or commands
;;
*) default statements or commands
esac
statement x
Explanation:
The value of variable will match the case values,if match is found then the
statements or commands under that case value will be executed and transfers the
control to statement x
if the value of variable does not match any of the case values then
the statements or commands under default section *) will be executed.
Example:
clear
echo “Enter day value (1-7):” 3
read d
case $d in
1) echo “Monday”
;;
2) echo “Tuesday”
:;
3) echo “wednesday”
:;
4) echo “Thursday”
:;
5) echo “friday”
:;
6) echo “saturday”
:;
7) echo “sunday”
;;
*) echo “invalid choice”
esac
Example:02
clear
echo “Enter any alphabet”
read ch
case $ch in
[aeiouAEIOU]) echo “ $ch is a vowel”
;;
*) echo “$ch is a consonant”
esac
Example:03
clear
echo “Enter any character:” ^
read ch
case $ch in
[0-9]) echo “$ch is a digit”
;;
[a-zA-Z]) echo “$ch is an alphabet”
;;
?) echo “$ch is a special character”
;;
*) echo “invalid choice”
esac
4) pwd ;;
5) exit ;;
*) echo “ invalid choice”
esac
looping statements or iteration statements:
The process of executing the one or more statements repeatedly until
some condition is satishfied.
There are 3 looping statements
1. while
2. until
3. for
while loop:
Syntax:
while [ condition ]
do
statements or commands
done
statement x
Explanation:
The statements or commands between do and done will be executed as long as
2. until loop:
Syntax:
until [ condition ]
do
commands or statements
done
stateement x
Explanation:
The statements or commands between do and done will be executed as long as the
condition is false. If condition is true, the control flow will transfer to the statement
x
Example:
clear i
i=1 1
until [ $i -gt 5] 2
do 3
echo $i 4
i=`expr $i + 1` 5
done 6
o/p:
1
2
3
4
5
3. for loop:
Syntax:
for variable in list
do
commands or statements
done
statement x
The for loop operates on list of items.It repeats set of commands or statements for
i=`expr $i + 1`
done
echo “Factorial of $n is:$fact”
or
clear
echo “Enter any positive number:”
read n
num=$n num n fact
fact=1 4 4 1
while [ $n -ge 1] 3 4
do 2 12
fact=`expr $fact \* $n` 1 24
n=`expr $n – 1` 0 24
done
echo “factorial of $num is:$fact”
rem=`expr $n % 10`
rev=`expr $rev \* 10 + $rem`
n=`expr $n / 10`
done
echo “The reverse of $num is $rev
Syntax:
while [condition]
do
if [ condition ]
then
break;
fi
done
statement x
Example:
clear
i=1
while [ $i -le 100 ]
do
echo $i
i=`expr $i + 1`
done
o/p: prints 1 to 100
clear
i=1
while [ $i -le 100 ]
do
echo $i
if [ $i -eq 10 ]
then
break
fi
i=`expr $i + 1`
done
o/p: prints 1 to 10
continue:
continue is used to skip the rest of the statement in a loop . The loop does not
terminate but the program execution transfers directly to next iteration
of the loop.
Syntax:
while [ condition ]
do
statements
continue
skipped statements
done
Example:
clear
i=0 i
while [ $i -le 50 ] 0
do 1
i=`expr $i + 1` 2
if [ $i -lt 5 ] 3
then 4
continue 5
fi
echo $i # 5
done
o/p: prints 5 to 50
clear
i=0 i
while [ $i -le 50 ] 0
do 1
i=`expr $i + 1` 2
if [ $i -gt 5 ] 3
then 4
continue 5
fi 6
echo $i
done
o/p: prints
clear
echo “Enter any String:”
read str
len=`echo $str | wc -c`
echo “length of $str is :$len”
len=`expr $len – 1`
echo -e “The reverse of $str is:\c”
while [ $len -ge 1 ]
do
done
echo “Factorial of $n is:$fact”
or
clear
echo “Enter any positive number:”
read n
num=$n num n fact
fact=1 4 4 1
while [ $n -ge 1] 3 4
do 2 12
fact=`expr $fact \* $n` 1 24
n=`expr $n – 1` 0 24
done
echo “factorial of $num is:$fact”
i=2;flag=1 11 0
while [ $i -lt m ] 12
do 13
r=`expr $m % i` 14
if [ $r -eq 0 ] 17
then
flag=0;
break;
else
i=`expr $i + 1`
fi
done
if [ $flag -eq 1 ]
then
echo -e “$m\t” 11 13 17 19
fi
m=`expr $m + 1`
done
read n 5 23 1 23 23
i=1 10 2 45 10
echo “Enter first number:” 45 3 08
read num 08 4
max=$num 30 5
min=$num
echo “Enter `expr $n – 1` numbers:”
while [ $i -lt $n ]
do
read num
if [ $num -gt $max ];then
max=$num
fi
if [$num -lt $min ];then
min=$num
fi
i=`expr $i + 1`
done
echo “maximum value is:$max”
echo “minimum value is $min”
clear
echo “Enter any Number:” n num rev rem
read n 121 121 0 1
num=$n;rev=0 12 1 2
while [ $n -gt 0 ] 1 12 1
do 0 121
rem=`expr $n % 10`
rev=`expr $rev \* 10 + $rem 123 123 321
n=`expr $n / 10`
done
if [ $num -eq $rev ]
then
echo “$num is a palindrome”
else
echo “$num is not a palindrome”
fi
# Shell Program to count number of vowels in the given string
# PART A 5th PROGRAM
clear
echo “Enter any string:”
read str
len=`echo $str | wc -c`
len=`expr $len – 1`
i=1;vowels=0;other=0
while [ $i -le $len ]
do
ch=`echo $str | cut -c $ch`
case $ch in
[aeiouAEIOU]) vowels=`expr $vowels + 1`
;;
*) others=`expr $others + 1`
esac
i=`expr $i + 1`
done
echo “The number of vowels=$vowels”
echo “ The number of other characters is:$others
i=3;f1=0;f2=1 5 3 0 1 1
echo -e “The Fibonacci Series are:\c” 4 1 1 2
echo -e “$f1\t$f2\c” 5 1 2 3
while [ $i -le $n ] 6 2 3
do
f3=`expr $f1 + $f2`
echo -e “\c$f3\t”
f1=$f2
f2=$f3
i=`expr $i + 1`
done
o/p: The Fibonacci series are:0 1 1 2 3
1 2 3
clear
echo “Enter number of lines to display:”
read n
i=1
while [ $i -le $n ]
do
j=1
while [ $j -le $i ]
do
echo -e “\c $j”
j=`expr $j + 1`
done
i=`expr $i + 1`
echo
done
clear
echo “Enter number of lines to display:”
read n
i=1
while [ $i -le $n ]
do
j=1
while [ $j -ge $i ]
do
echo -e “\c $j%2”
j=`expr $j + 1`
done
i=`expr $i + 1`
echo
done
#Shell Program to copy one file to another file using command line
clear
echo “ File name to copy is:$1”
echo “New File name to copy:$2”
if [ $# -gt 2 -o $# -lt 2 ]
then
echo “Insufficient arguments”
exit
fi
cp $1 $2
echo “ Given File is:”
cat $1
# Shell Program to accept file name and check whether it is regular file or
directory file and display its permissions
clear
echo “Enter any file name:”
read name
if [ -f $name ]
then
echo “ $file is an ordinary file”
fi
if [ -d $name ]
then
echo “$file is a directory file”
fi
if [ -r $file ]
then
echo “$file is a readable”
fi
if [ -w $file ]
then
echo “$file is writable”
fi
if [ -x $file ]
then
echo “$file is executable
fi
if [ -w $file ]
then
echo “$file is writable”
fi
if [ -x $file ]
then
echo “$file is executable
fi
done
(or)
clear
for i in *
do
if [ -f $i ]
then
file $i
ls -l $i
fi
done
Simple Examples:
Example 1:
Shell program to list all files and directories
clear
files=`ls`
for i in $files
do
echo $i
done
Example 2:
clear
set `date`
if [ $1 = “Tue” ]
then
echo “It is Tuesday”
elif [ $1 = “Sat” -o $1 = “Sun” ]
then
echo “It is weekend! Enjoy “
else
echo “ It is not weekend , Goto Work”
fi
Example : 03
clear
set `date`
case $1 in
Tue) echo “It is Tuesday”
;;
Sat | Sun) echo “It is Weekend!Enjoy”
;;
Redirection:
The Shell will associate with three files with the terminal , two for display and one
for keyboard.
These files are streams of characters which many commands see as input and
output.
The process of changing the input source or output destination is called
Redirection.
In UNIX, It is accomplished with following operators.
Input redirection (>)
Output redirection (>)
Output redirection with append (>>)
pipe (|)
20 50 120
3. sort data | tail -2
kiran
siva
3.Standard Error:
The file(stream) representing the error messages also connected to the
display.
Example:
$ cat emp.lst <enter> if emp.lst is not created
cat:emp.lst:No Such file or directory