[go: up one dir, main page]

0% found this document useful (0 votes)
11 views28 pages

Saloniyadav OSpracticle File

This document outlines practical exercises for an Operating System course, detailing various shell commands and scripts. It includes usage examples for commands like ls, pwd, and chmod, as well as scripts for tasks such as displaying the date, calculating factorials, and checking for prime numbers. Each section provides the code and expected output for the respective tasks.

Uploaded by

x7rftm5gbk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views28 pages

Saloniyadav OSpracticle File

This document outlines practical exercises for an Operating System course, detailing various shell commands and scripts. It includes usage examples for commands like ls, pwd, and chmod, as well as scripts for tasks such as displaying the date, calculating factorials, and checking for prime numbers. Each section provides the code and expected output for the respective tasks.

Uploaded by

x7rftm5gbk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Operating System practical

File
Name-Saloni Yadav
Course-B.A.program(CS +Maths)
Roll No - 2022022
Year - Second
Submission Date - 3 May 2024

Q1.Usage of following commands: ls, pwd, cat, who, rm,


mkdir, rmdir,cd.

CODE-

ls # lists all files and directories in the current


directory

pwd # prints the current directory path

cat file.txt # displays the content of file.txt

who # shows who is logged on

rm file.txt # removes file.txt

mkdir newdir # creates a new directory named newdir

rmdir newdir # removes the directory named newdir

cd newdir # changes the current directory to newdir


output-

Q2. Usage of following commands: cal, cat(append), cat/


concatenate), mv, cp, man, date.

Code:
cal # shows the current month's calendar

cat file1.txt >> file2.txt # appends the content of file1.txt


to file2.txt

cat file1.txt file2.txt > combined.txt # concatenates file1


and file2 into combined.txt

cp file1.txt file2.txt # copies file1.txt to file2.txt

man ls # shows the manual page for the ls command

date # displays the current date and time

Output:-
Q3. Usage of following commands: chmod, Grep, bc

Code:

chmod 755 file.txt # changes permissions of file.txt to 755

grep "pattern" file.txt # searches for "pattern" in file.txt


echo "scale=2; 22/7" | bc # uses bc to calculate 22 divided
by 7 with 2 decimal placesO

Output:-

Q4. Write a shell script to display date in the mm/dd/yy


format.

Code :

formatted_date=$(date i =+%m/%d/%y)

echo "Today's date is: $formatted_date"

Output:-

Q5.Write a shell script to display the multiplication table any


number.

Code-

n=3

echo"output"

i=1

while [ $i -le 10 ]

do
echo "$(( n * i))"

i=$(( i+1 ))

done

Output-

6. Write a shell script to find the factorial of a given number.

Code-

factorial() {

if [ $1 -eq 0 ]; then

echo 1

else echo $(( $1 * $(factorial $(($1-1))) ))


fi

read -p "enter a number : " num

result=$(factorial $num)

echo " the factorial of $num is : $result"

Output:-

7. Program to show the pyramid of special character “*”.

Code-

p =6;

for ( (m=1; m<=p; m++) )

do

for((a=m; a<=p; a++))

do

echo -ne "";

done

for((n=1; n<=m; n++))


do

echo -ne "*";

done

for((i=1; i<m; i++))

do

echo -ne "*';

done

echo;

done

Output:-

8. Write a shell script to find the sum of digits of a given


number.

Code-

a=10

b=20

#display the result

sum=$(($a+b))#calculate sum

Output:-

9. Write a shell script to perform the tasks of basic


calculator.

Code:-

echo "welcome to basic calculator"

read -p"enter first number : "num1

read -p "enter second number : "num2

echo " choose an operation:"

echo "1. Addition"

echo "2. subtraction"

echo "3.Multiplication "


echo "4.division"

read -p "enter yor choice (1-4): " choice

case $choice in

1)echo "result: $(($num1 + $num2))" ;;

2)echo "result: $(($num1 - $num2))" ;;

3)echo "result: $(($num1 * $num2))" ;;

4)if [ $nu2 -eq 0];then

echo"error: Division by zero!"

else

echo "result: $(echo "scaler=2; $num1 / $num2" | bc)"

fi

;;

*)echo "invalid choice";;

esac

Output:-
10. Write a shell script to find the power of a given number.

Code-

pow()

{ # value of a

a=$1

b=$2
c=1

res=1

if((b==0));

then

res=1

fi

if((a==0));

then

res=1

fi

if((a >=1 && b >= 1));

then

while((c <= b))

do

res=$((res * a))

c=$((c +1))

done

fi

echo "$1 to the power $2 is $res"

}
Output:-

11. Write a shell script to check whether the number is


Armstrong or not.

Code:-

echo-n "enter a number :"

read -r n

arm=0

tem=$n

while [ "$n" -ne 0 ] ; do

r=$((n % 10))

arm=$((arm + r * r* r))

n=$((n / 10))
done

echo $arm

if [ $arm-eq "$temp" ] ; them

echo "Amstrong"

else

echo "Not Amstrong"

fi

Output:-

12. Write a shell script to find the GCD (greatest common


divisor) of two numbers.
Code :-

gcd() {

local a=$1

local b=$2

while [[ $b -ne 0 ]]; do

local temp=$b

b=$((a % b))

a=$temp

done

echo "$a"

echo "Enter first number:"

read num1

echo $num1

echo "Enter second number:"

echo $num2

result=$(gcd $num1 $num2)

echo "The GCD of $num1 and $num2 is $result"

Output:-
13. Write a shell script to check if the number entered at the
command line is prime or not.

Code:-

is_prime() {

local num=$1

if [ $num -lt2 ]; then

return 1

fi

for ((i=2; i*i<=num; i++)); do

if [ $((num % i)) -eq 0 ]; then

return 1

fi

done

return 0

echo "Enter the number:"

read num

echo $num

if is_prime "$num"; then


echo "Prime"

else

echo "Not Prime"

fi

Output:-

14. Write a shell script to display on the screen sorted


output of “who” command along with the total number of
users.

Code:-

who_output=$(who)

num_users=$(echo "$who_output" | wc -l)


echo "Total number of users: $num_users"

Output:-

15. Write a shell script to accept a login name. If not a valid


login name display message – “Entered login name is
invalid”.

Code-

read -p "enterusername"

if [ $name=$username ]

the

echo "valid"

else
echo "enter loginname is invalid"

fi

Output:-

Q16. Write a shell script to compare two files and if found


equal asks the user to delete the

duplicate file

Code-

file1="Welcome to File1."

file2="Welcome to File2."

if [ "$file1" = "$file2" ]; then

echo "The files are Equal."

echo "Do you want to delete one of the files? (y/n)"


read res

if [ "$res" = "y" ]; then

echo "Which file do you want to delete? (1/2)"

read filedelete

if [ "$filedelete" = "1" ]; then

echo "File deleted"

elif [ "$filedelete" = "2" ]; then

echo "File2 deleted"

else

echo "Invalid choice. No File deleted"

fi

else

echo "No File deleted"

fi

else

echo "The Files are NOT Equal"

fi

Output:-
Q17. Write a shell script to merge the contents Of three sort
the contents and then

display them page by page.

Code:

file1="apple

banana

grape"

file2="orange

kiwi

pineapple"

file3="mango

pear
watermelon"

mergecontent=$(echo -e "$file1\n$file2\n$file3")

echo "merged content = $mergecontent"

sortcontent=$(echo "$mergecontent" | sort)

echo "sorted content = $sortcontent"

output:

Q18. Write a shell script to check whether the tile have all
the permissions or not.

Code:

ls –ltr

output:
Q19. Write a shell script to modify "cal" command to
display calendars of the specified

months.

Code:

echo "Enter the month (1-12):"

read month

echo $month

echo "Enter the year (e.g., 2024):"

read year

echo $year

if ! [[ "$month" =~ ^[1-9]|1[0-2]$ ]]; then

echo "Invalid month. Month must be a number between 1


and 12."

exit 1

fi

if ! [[ "$year" =~ ^[0-9]{4}$ ]]; then

echo "Invalid year. Year must be a 4-digit number."

exit 1

fi

cal $month $year

Output:-
Q20. Write a shell script to modify "cal” command to
display calendars of the specified

range of months.

Code:

echo "Enter the starting month (1-12):"

read startmonth

echo "Enter the ending month (1-12):"

read endmonth

echo "$startmonth - $endmonth"

if ! [[ "$startmonth" =~ ^[1-9]|1[0-2]$ ]]; then

echo "Invalid start month"

exit 1

fi
if ! [[ "$endmonth" =~ ^[1-9]|1[0-2]$ ]]; then

echo "Invalid end month"

exit 1

fi

if [ "$startmonth" -gt "$endmonth" ]; then

echo "Invalid range"

exit 1

fi

for ((month=$startmonth; month<=$endmonth; month++)); do

cal $month $(date +%Y)

echo "========================"

done

Output:-

You might also like