Linux and Shell Programming Practical File B.E V Semester
Linux and Shell Programming Practical File B.E V Semester
Practical File
B.E V Semester
Department Of Computer
Science and Engineering
Ujjain Engineering College, Ujjain
Indore Road, Ujjain, Madhya Pradesh 456010
List of Experiments
2
Write a program to study basic and user status commands in Linux.
3
Write a program to find greatest number among the given numbers.
4
Write a program to find even or odd numbers among the given
numbers.
5
Write a program to find the factorial of a number.
6
Write a program to find the leap year among the given years.
7
Write a program to find the Fibonacci Series from the given number.
8
Write a program to develop a calculator with switch case.
10
Write a program to get marks and division of student’s subjects.
PROGRAM – 1
Aim:
Introduction of Linux.
Theory:
HISTORY
• 1969: - Birth of C and UnixOS
• 1970’s: - Growth of Unix because of open source collaboration.
• 1980'S: - companies developing their own UNIX-IBM(AIX), Solaris(Sun OS),
HP(HP-UX)....Mid to late 1980’s Birth of free software movement -> GNU
project
• 1990’s:- Linux Torvalds put the Linux kernel source code online. Resulted in
usage of Linux GNU.
ADVANTAGES OF LINUX
• It has better malware protection.
• It has multiple distributions like fedora, debianetc.
• It provides free software licensing.
• It provides access to source code.
• it provides simplified updates for all installedS/W.
PROGRAM – 2
Aim:
Write a program to study basic and user status commands in Linux.
Theory:
Linux provide a CLI (Command Line Interface) to communicate with OS,
CLI is better for task which cannot be performed with GUI
1. pwd: Display the current working directory of theterminal
2. / : Rootdirectory
3. echo : Commands that writes its arguments to standardoutput
4. su:Usedtoswitchtorootuser(sothatsuperuserpermissioncanbe used to
executecommands)
5. su username: Used to switch to differentuser
6. sudo: Execute only that command with root/super user privileges
(used withls)
7. clear: This command is used to clear the terminal screen. Contents
will not be deleted but scrolleddown.
ls: This command lists all the contents in the current
working directory
1. ls path: By specifying the path after ls, the content in that path
will bedisplayed.
2. ls -l: Using “l” flag, list all the content along with its owner setting,
Permission and time stamp (long format)
3. ls -a: Using “a” flag, lists all hidden contents in the
specificdirectory.
4. ls -author: Using “—author” flag, lists the contents in the specific
directory along with isowner.
5. ls -s: Using “S” flag, Sorts and lists all the contents inspecified
directory bysize
6. ls *.html: Using “*” flag, lists only the contents in the directory of
a particularformat
7. ls -lS> file.txt: Using “>” flag, Copies the result of ls command
into a text file. //ls -lS/home>File1.txt
cd: Change the directory
1. cd ~: This command also changes the directory to homedirectory
2. cd/: Changes the directory to rootdirectory
3. cd.: Changes the directory to its parentdirectory
4. cd ‘xx yy’: if there is space in folder name (e.g. programfile)
Cat: This command is used to display the contents of text files and concatenate
several files into one
1. cat -b: This is used to add line numbers to non-blanklines
2. cat -n: This is used to add line numbers to alllines
3. cat -s: This is used to squeeze blank lines into oneline
4. cat -E: Show $ at the end ofline
5. cat >file1.txt: The ‘>’ flag can be used to create a new file and enter text
contents fromterminal.
6. cat file1.txt>>file2.txt: The ‘>>’ flag can be used to append text contents
to an existing file from the terminal.
Grep: this command is used to search for a particular string/word in a text file.
1. $grep options file1.txt: Returns results of matching stringoptions.
2. $ grep -I options file1.txt: Returns the results of case insensitivestrings.
3. $grep-noptionsfile1.txt:Returnsthematchingstringsalongwiththeirline
number.
4. $grep -v options file1.txt: Returns the results of lines not matching the
search string.
5. $grep -c options file1.txt: Returns no. of lines in which the results match
search string.
$ sort file1.txt: we use ‘sort’ command to sort the results of a search either
alphabetically or numerically.Files, file contents and directories can be
stored
1. $ sort File1.txt File2.txt: Sorts the contents of both file1.txt andfile2.txt
2. $ sort -r file1.txt: Returns the results in reverseorder.
3. $ sort -f file1.txt: Does case insensitivesorting.
4. $sort -n file1.txt: Returns the results in numericalorder.
$grep dh File 1.txt File2.txt | sort: Searches forstring‘dh’ from both files
and sorts theresults.
1. grep dh File 1.txt File2.txt |sort -r: Sorts the results in reverseorder.
Program:
Program:
Output :
PROGRAM – 5
Aim:
Write a program to find the factorial of a number.
Program:
Output :
PROGRAM – 6
Aim:
Write a program to find the leap year among the given years.
Program:
Output :
PROGRAM – 7
Aim:
Write a program to find the Fabonacci Series from the given number.
Program:
if [ $# -eq 1 ]
then
Num=$1
else
echo -n "Enter a Number :"
read Num
fi
f1=0
f2=1
echo "The Fibonacci sequence for the number $Num is : "
for (( i=0;i<=Num;i++ ))
do
echo -n "$f1 "
fn=$((f1+f2))
f1=$f2
f2=$fn
done
Output :
PROGRAM – 8
Aim:
Write a program to develop a calculator with switch case.
Program:
Program:
for ((i=1;i<=5;i++))
do
for ((j=1;j<=i;j++))
do
echo -e "$i \c"
done
echo ""
done
Output :
PROGRAM – 10
Aim:
Write a program to get marks and division of student’s subjects.
Program:
echo "Enter the five subject marks for the
student"read m1 m2 m3 m4 m5
sum1=`expr $m1 + $m2 + $m3 + $m4 +
$m5`echo "Sum of 5 subjects are: "
$sum1per=`expr $sum1 / 5`
echo " Percentage: "
$perif [ $per -ge60]
then
echo "You get
Distinction”elif [ $per -
ge50 ]
then echo
“You get First class”elif [
$per -ge40]
then
echo "You get Second
class"else
echo "You get
Fail"fi
Output :