Linux and Shell Programming
Practical File
B.E V Semester
Department Of Computer
Science and Engineering
Ujjain Engineering College, Ujjain
Indore Road, Ujjain, Madhya Pradesh 456010
Submitted to: Submitted by:- Vivek Ajmeri
Prof. YK Joshi Rollno:- 0701CS181059
Prof. Rahul Paldiya Branch:- Computer science
and enginering
(Asst. Professor)
Linux and Shell programming
List of Experiments
s.no List of Experiments Date of Date of Remark
Experiment submission
1
Introduction of Linux.
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.
9 Write a program to draw a pyramid pattern.
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.
VARIOUS DISTRIBUTIONS OF LINUX
• Red Hat enterprise:-it is having the commercial Linux distribution intendedfor
servers and workstations.
• Fedora:-sponsored by red hat, it is the foundation for the commercial RHEL.
• Debian:-debian is an OS composed only of free, open-source software.
• Other popular distributions include ubuntu ,Linux int, CentOS,openSUSE.
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.
cp: This command is used to copy files and directories.
1. cp -I: Enters interactive mode; CLI asks before overwritingfile
2. cp -n: Does not overwrite thefile.
3. cp -u: Updates the destination file only when source file is different from
destination file. (runs when names of 2 files aredifferent)
4. cp -R: Recursive copy for copying directories; Copies even hiddenfiles
5. cp -v: Verbose; Prints informativemessages
mv: This command is used to move files and directories.
1. mv -I: Enters interactive mode ;CLI ask before overwritingfile.
2. mv -u: Updates the destination file only when source file is different from
destination file.
3. mv -v: Verbose; Prints source and destinationfiles.
mkdir: This command is used to create a new directory
1. mkdir -p: Creates both a new parent directory and asub-directory.
2. mkdir -parents: This is also used for the sameprocess
3. mkdir -p file 1/ {f2, f3, f4}: This is used to create multiple sub-directories
inside the new parentdirectory.
rmdir: This command is used to removethespecific directory(empty)
1. rmdir -p: Removes both parent and childdirectory
2. rmdir -pv: Removes all parent and sub-directories along with theverbose
3. rm-r: Removes even non emptydirectories
4. rm-rp:Removesnonemptydirectoriesincludingparentandsubdirectories
PROGRAM – 3
Aim:
Write a program to find greatest number among the given numbers.
Program:
echo "enter first number"
read first
echo "enter second number"
read sec
echo "enter third number"
read third
if [ $first -gt $sec ] ; then
if [ $first -gt $third ] ;then
echo -e " $first is greatest number "
else
echo -e " $third is greatest number "
fi
else
if [ $sec -gt $third ] ; then
echo -e " $sec is greatest number "
else
echo -e " $third is greatest number "
fi
fi
Output :
PROGRAM – 4
Aim:
Write a program to find even or odd numbers among the given numbers.
Program:
echo "Enter a Number:"
read n
rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "Number is even"
else
echo "Number is odd"
fi
Output :
PROGRAM – 5
Aim:
Write a program to find the factorial of a number.
Program:
echo "Enter a number"
read num
fact=1
while [ $num -gt1 ]
do
fact=$((fact * num)) #fact = fact * num
num=$((num-1)) #num = num - 1
done
echo $fact
Output :
PROGRAM – 6
Aim:
Write a program to find the leap year among the given years.
Program:
echo "Enter Year:"
read y
year=$y
y=$(( $y % 4 ))
if [ $y -eq 0 ]
then
echo "$year is Leap Year!"
else
echo "$year is not a Leap Year!"
fi
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:
echo "Enter Two numbers : "
reada
readb
echo "Enter Choice :"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read ch
case $ch in
1) res=`echo $a + $b |bc`
;;
2) res=`echo $a - $b |bc`
;;
3) res=`echo $a \* $b |bc`
;;
4) res=`echo "scale=2; $a / $b" |bc`
;;
esac
echo "Result : $res"
Output :
PROGRAM – 9
Aim:
Write a program to draw a pyramid pattern.
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 :