Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

Computer Science and

Engineering KSET KPGU


Enrollment number: 2201201073

Practical: 01
Aim : Study of Unix/
Linux
Unix is an Operating System that is truly the base of all Operating Systems
like Ubuntu, Solaris, POSIX, etc. It was developed in the 1970s by Ken
Thompson, Dennis Ritchie, and others in the AT&T Laboratories. It was
originally meant for programmers developing software rather than non-
programmers.

Some of the key features of UNIX include:

Multiuser support: UNIX allows multiple users to simultaneously access the


same system and share resources.

Multitasking: UNIX is capable of running multiple processes at the same


time.

Shell scripting: UNIX provides a powerful scripting language that allows


users to automate tasks.

Security: UNIX has a robust security model that includes file permissions,
user accounts, and network security features.

Portability: UNIX can run on a wide variety of hardware platforms, from


small embedded systems to large mainframe computers.

Linux is a powerful and flexible family of operating systems that are free to
use and share. It was created by a person named Linus Torvalds in 1991.

Some key features of linux:

Free and Open-Source

Linux is completely free of cost, and expenses are never a hindrance to using
it as an operating system.

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Extremely Flexible

Linux has incorporated itself into embedded products like watches, digital
equipment and supercomputing servers.

Lightweight Infrastructure

Linux consumes lesser storage space, and its installation requires around
4GB to 8GB of disk space.

Graphical User Interface (GUI)

Even though Linux works on using the command line interface but it can be
converted to be used like windows having a Graphical user interface. This is
done mostly by installing packages. The most common way of having a GUI
on the Linux environment is to log in to Ubuntu server and install its desktop
environment.

End-to-end encryption

Linux allows end-to-end encryption while accessing data thus storing public
keys in the server. All data is password protected and provides
authentication to users. It also allows many security features and provides
file permissions, a secure shell, etc.

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Linux Unix

The source code of Linux is The source code of Unix is


freely available to its users not freely available general
public
It has graphical user interface It only has command line interface
along with command line interface

Linux OS is portable, flexible, and Unix OS is not portable


can be executed in different hard
drives

Different versions of Linux OS Different version of Unix are


are Ubuntu, Linux Mint, RedHat AIS, HP-UX, BSD, Iris, etc.
Enterprise Linux, Solaris, etc.

Linux is an open-source Unix is a proprietary operating


operating system that was first system that was originally
released in 1991 by Linus developed by AT&T Bell Labs
Torvalds. in the mid 1960s.

The Linux kernel is monolithic, The Unix kernel is modular,


meaning that all of its services meaning that it is made up of a
are provided by a single kernel. collection of independent modules
that can be loaded and unloaded
dynamically.

User Interface of Linux is User Interface of unix is


Graphical or text-based. text- based.

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Practical: 02
Aim: Study of Basic commands to understand the system and
working of Linux/Unix.

1. ls: List directory contents.


Example: ls, ls -l (long listing format), ls -a (including hidden files), ls -
lh (human-readable sizes).
2. cd: Change directory.
Example: cd /path/to/directory, cd .. (move to the parent directory), cd ~
(move to the home directory).
3. pwd: Print working directory.
Example: pwd (prints the current directory path).
4. mkdir: Make directory.
Example: mkdir
directory_name.
5. rmdir: Remove directory.
Example: rmdir
directory_name.
6. touch: Create an empty
file.- Example: touch filename.
7. rm: Remove files or directories.
Example: rm filename, rm -r directory_name (recursive deletion).
8. cp: Copy files or directories.
Example: cp source_file destination_file, cp -r source_directory
destination_directory (recursive copy).

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073
9. mv: Move or rename files or directories.

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Example: mv old_filename new_filename, mv source_file


destination_directory.
10. cat: Concatenate and display the content of
files. Example: cat filename.
11. more/less: View file content one screen at a
time. Example: more filename, less filename.
12. head: Display the first few lines of a
file. Example: head filename.
13. tail: Display the last few lines of a
file. Example: tail filename.
14. grep: Search for patterns in
files. Example: grep pattern filename.
15. chmod: Change file
permissions. Example: chmod
permissions filename.
16. chown: Change file
ownership. Example: chown
user:group filename.
17. ps: Display information about running
processes. Example: ps aux.
18. kill: Terminate processes.
Example: kill PID, where PID is the process ID.
19. man: Display manual pages for
commands. Example: man command.
20. df: Display disk space usage.

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073
Example: df -h (human-readable
format).

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Practical: 03
Aim:1. Write a script to find the smallest of three numbers as
well as largest among three numbers.
echo "Enter three
numbers:" read num1 num2
num3
if [ $num1 -lt $num2 ] && [ $num1 -lt $num3 ]; then
smallest=$num1
elif [ $num2 -lt $num1 ] && [ $num2 -lt $num3 ]; then
smallest=$num2
else
smallest=$num3
fi
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]; then
largest=$num1
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]; then
largest=$num2
else
largest=$num3
fi
echo "Smallest number:
$smallest" echo "Largest number:
$largest"

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

OUTPUT:

Figure: 01

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Practical: 04
Aim:1 Write a shell script to generate marksheet of a student.Take
5 subjects, calculate and display total marks, percentage and
Class obtained by the student.
total_subjects=6
total_marks=600
declare -a subject_marks
for ((i = 1; i <= $total_subjects; i++)); do
read -p "Enter marks for Subject $i: "
subject_marks[$i] done
total_obtained=0
for ((i = 1; i <= $total_subjects; i++)); do
total_obtained=$((total_obtained + subject_marks[$i]))
done
percentage=$((total_obtained * 100 /
total_marks)) if ((percentage >= 90)); then
grade="A"
elif ((percentage >= 80)); then
grade="B"
elif ((percentage >= 70)); then
grade="C"
elif ((percentage >= 60)); then
grade="D"
else
grade="F"
fi
echo "Total Marks Obtained: $total_obtained out of $total_marks"

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

echo "Percentage: $percentage%"


echo "Grade: $grade"

OUTPUT

Figure: 01

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Practical: 05
Aim:1. 1. Write a shell script to find factorial of given
number n
read -p "Enter the number : "
number fact=1
while [ $number -gt 1
] do
fact=$((fact * number))
number=$((number - 1))
done
echo "Result:" $fact
OUTPUT:

Figure: 01

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

2. Write a script to reverse a number and string given by user


echo -n "Enter a number:
" read n
reversed=0
while [ $n -gt 0 ];
do digit=$((n %
10))
reversed=$((reversed * 10 + digit)) n=$
((n / 10))
done
echo "Reversed number:
$reversed" echo "Enter a string:"
read user_string
reversed_string=""
for ((i = ${#user_string} - 1; i >= 0; i--)); do reversed_string="$
{reversed_string}${user_string:$i:1}"
done
echo "Reversed string: $reversed_string"

OUTPUT:

Figure: 01

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Figure: 02

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Practical: 06
Aim:1. Write a shell script to check whether number is
prime or not.
echo "Enter a number: "
read num
if [ $num -le 1 ]; then
echo "$num is not a prime
number" elif [ $num -gt 1 ]; then
for ((i = 2; i < num; i++)); do
if [ $((num % i)) -eq 0 ]; then
echo "$num is not a prime number"
echo "$i times $((num / i)) is $num"
break
fi
done
if [ $i -eq $num ]; then
echo "$num is a prime number"
fi
fi

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

OUTPUT:

Figure: 01

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Practical: 07
Aim:1. Write a shell script to check whether number is
palindrome or not.
echo -n "Enter a number:
" read num
original=$num
reversed=0
while [ $num -gt 0 ]; do digit=$
((num % 10)) reversed=$
((reversed * 10 + digit)) num=$
((num / 10))
done
if [ $original -eq $reversed ]; then
echo "Number is a
palindrome"
else
echo "Number is NOT a palindrome"
fi

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

OUTPUT:

Figure: 01

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Practical:
Aim:1. Write a shell script to check whether number is even or odd.
echo -n "Enter a number:
" read num
rem=$((num % 2))
if [ $rem == 0 ];
then
echo "$num is an even
number" else
echo "$num is an odd number"
fi
OUTPUT:

Figure: 01

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Practical:
Aim:1. Write a shell script to print Fibonacci series up to
nth element.
read -p "Enter the value of n (number of elements): " n
a=0
b=1
for ((i = 0; i < n; i++));
do echo -n "$a "
next=$((a + b))
a=$b
b=$next
done
echo

OUTPUT:

Figure: 01

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

Practical: 10
Aim:1. Write a shell script to choose one option from the
given menu and to display it.
echo "Menu
Options:" echo "1.
Option A" echo "2.
Option B" echo "3.
Option C" echo "4.
Exit"
read -p "Enter your choice (1/2/3/4): " choice
case $choice in
1)
echo "You selected Option
A" 2)
echo "You selected Option
B" 3)
echo "You selected Option
C" 4)
echo "Exiting the menu"
*)
echo "Invalid choice. Please select a valid option (1/2/3/4)."
esac

OS(21CS2404)/4th Page
Computer Science and
Engineering KSET KPGU
Enrollment number: 2201201073

OUTPUT:

Figure: 01

OS(21CS2404)/4th Page

You might also like