Os Lab Assignment Complete Code Solution

 



GLA University, Mathura
Course: B. Tech (CSE/ IBM related courses) UNIX OS Lab/BCSC 0804
Session: 2020-21


  • Command Implementation On Shell


1. Implement the following basic commands used in LINUX/ UNIX OS ( Also perform all the commands with their switches as assignment)
ls  - List All Files Present In Current Directory
man - Online Manual(Help) About Command
pwd -  Prints Current Working Directory
who - Lists who is logged on system.
whoami - Lists the Username of the current user logged in
date - Print out Current Date
cal - Print Calender
mkdir - Make a directory
rm - Remove or delete file
rmdir - Remove Directory
cat - Opens an existing file
head - Display first 10 lines of the file
tail - Dispaly last 10 files of the file
more - Look at file
less - Allow Backward movement in the file.
cp - Copy file into directory
mv- Move File
echo - Display the Value

2. a. Implement the following basic commands used in LINUX/ UNIX OS ( Also perform all the commands with their switches)
cut - Display Only the columns from file
uniq - Filter out the repeated lines in file.
chmod - Change file permissions
du - list the disk usage of the given file.
diff - Check two files are same or not.
history - Lists command You've done recently
last - Find out who has recently log in or out
sort - sorts the content in a file
sed - use to replace text in a file.
grep - find which lines in a file.
pipe - redirect standard output,input or error of one process to another.




  • b. Implement the special shell scripting variables such as $$,$0,$1,$*,$@,$# etc

#!/bin/bash

#Implement some Commands $0,$$,$1,$*,$# Etc.

echo " File Name : $0"

echo "Process Id : $$"

echo " First Argument : $1"

echo "Second Argument : $2"

echo "Third Argument : $3"

echo "Quoted values : $@"

echo "Quoted values ; $*"

echo "Number Of Arguments : $#"


  • SHELL PROGRAMMING

3. Shell scripts that uses simple commands:


a. Write a shell script to display current date in a particular format, number of users currently login and current month’s calendar


#!/bin/bash
#Display Current Date,Calender and Number of User
echo "Current Date :"; date
echo "Current Month's of Calender"; cal
echo "Number of User :";who -q

b. Write a shell script to display the process name and its process id.

#!/bin/bash
#Display Process Name and Process Id
echo "Current Process Name: $0"
echo "Current Process Id : $$"

c. Write a shell script to take name as a input and display a greeting message to the user by checking system clock. (Ex :- Hello John Good Morning in morning time else Hello John Good Afternoon in afternoon time else Hello John Good Evening in Evening time).

#!/bin/bash
#Greeting User by Present Time
echo "Enter the Name Of User:"
read Name
Present_Time=$(date +%H)
if [ $Present_Time -lt 12 ]
then
echo " Good Morning! : $Name "
elif [ $Present_Time -gt 12 -a $Present_Time -lt 15 ]
then
echo " Good AfterNoon! : $Name "
elif [ $Present_Time -gt 15 -a $Present_Time -lt 18 ]
then
echo " Good Evening! : $Name "
else
echo " Good Night! : $Name "
fi

d. Write a shell script to merge the content of 2 files into one file.
#!/bin/bash
#concatenate Two Files
echo Enter first filename
read File1
echo Enter second filename
read File2
cat $File1 > third
cat $File2 >> third
echo After concatination of contents of entered two files
echo ----------------------------------------------------
cat third | more
echo ----------------------------------------------------


4. Decision based Shell scripts:


a. Write a shell script that finds whether an entered number is even or odd.
#!/bin/bash
#Check the number is even or odd
echo "Enter the Number"
read Num
test $(($Num % 2)) == 0 && echo "Number is Even" || echo "Number is Odd"

b. Write a shell script to input the name of a file as command line argument and display
whether it is a file, a directory or anything else.
#!/bin/bash
#check the argument is file or dir or not
if [ -f$1 ]
then
echo "It is a File"
elif [-d$1 ]
then
echo "It is a directory"
else
echo "It is not a file or dir"
fi

c. Write a shell script to input the marks of a student in 3 subjects and find his grade.

#!/bin/bash
#Find Grade Of student in 3 Subject
echo "Enter the Marks Of First Subject:"
read Mark1
echo "Enter the Marks Of second Subject:"
read Mark2
echo "Enter the Marks Of Third Subject:"
read Mark3
Total_Mark=$(($Mark1 + $Mark2 + $Mark3))
Avg=$((Total_Mark / 3))

if [ $Avg -ge 60 ]
then
echo "Grade : First Grade"
elif [ $Avg -lt 60 -a $Avg -ge 45 ]
then
echo "Grade : Second Grade"
elif [ $Avg -lt 45 -a $Avg -ge 30 ]
then
echo "Grade : Third Grade"
else
echo "Sorry You have No Any Grade!"
fi

d. Write a Shell script to accept a filename as argument and displays the last
modification time if the file exists and a suitable message if it doesn't.
#!/bin/bash
#print last modification time of file
if [ -f$1 ]
then
echo "Last Modify Time : $(stat -c %y $1)"
else
echo "it is not a File"
fi

e. Write the shell script to take file name as input and if the file exists then print the
number of lines and also print 10th line of that file.

#!/bin/bash

#Enter the file as input and if exist print the number of lines and 10th line of that file

echo "Enter the file name"

read file

if [ -f$file ]

then

l=`cat $file | wc -l`

ll=`cat $file | head -10 | tail -1`

echo "Number of lines = $l"

echo "10th line of file=$ll"

else

echo "File does not exist"

fi


5. Shell scripts related to loops and arrays



a. Write a shell script that print multiplication table of a given no.

#!/bin/bash
#Print Multiplication Table Of given no by user
echo "Enter the Number:"
read num
echo "Enter the Higher range of number You want to multiply"
read Num
echo "Multiplication Table"
for ((i=1;i<=$Num;i++))
do
echo "$num x $i=`expr $num \* $i`"
done

b. Write a shell script to implement a timer.

#!/bin/bash
#set timer
flag=1
while [ $flag -eq 1 ]
do
echo "Enter a Timer Limit"
read t
for ((i=$t;i>0;i--))
do
echo "Current Time : $(date +%T)"
sleep 1
wait
done
echo "Time Up : $(date +%T)"
echo "If You Want new Timer then press 1 for Exit press 0"
read flag
done

c. Write a shell script that print reverse of a given positive number.

#!/bin/bash
#print the Number in reverse order
echo "Enter the Number"
read Num
rem=0
res=0
while [ $Num -gt 0 ]
do
rem=$(($Num % 10))
res=`expr $res \* 10 + $rem`
Num=$(($Num / 10))
done
echo "Reverse No. : $res"

d. Write a shell script that print factorial of a given number.
#!/bin/bash
#factorial Of Number
echo "Enter the Number"
read Num
res=1
for ((i=$Num;i>0;i--))
do
res=`expr $res \* $i`
done
echo "Factorial of Num : $res"
e. Write a shell to demonstrate the working of array. Input 10 integer in array and
display maximum, minimum, total sum and average of an array.
#!/bin/bash
#Array Operations
SizeOfArr=10
count=$SizeOfArr
i=0
echo "Enter the Array Value"
while [ $i -lt $SizeOfArr ]
do
read arr[$i]
i=`expr $i + 1`
done
max=${arr[0]}
min=${arr[0]}
sum=0
for i in ${arr[*]}
do
if [ $i -gt $max ]
then
max=$i
fi
if [ $i -lt $min ]
then
min=$i
fi
sum=`expr $sum + $i`
done
avg=`expr $sum / $count`
echo "Maximum Value Of Arr : $max"
echo "Minimum Value Of Arr : $min"
echo "Total Sum Of Value : $sum"
echo "Average : $avg"
#printf %.2f $(echo "$avg | bc -1) this is not working

f. You are given a file with four space separated columns containing the scores of
students in three subjects. The first column contains a single character (A-Z), the
student identifier. The next three columns have three numbers each. The numbers
are between 0 and 100, both inclusive. These numbers denote the scores of the
students in English, Mathematics, and Science, respectively. Your task is to identify
whether each of the students has passed or failed.
A student is considered to have passed if (s) he has a score 50 or more in each of
the three subjects.
#!/bin/bash
#Identify Student is pass or Fail
echo "Enter the No. of Student"
read num_Std
num=$num_Std
i=1
count=0
while [ $i -le $num_Std ]
do
echo "Enter the student$i name only single char"
read Std[$i]
echo "Enter the Marks(0 - 100) Of English"
read eng[$i]
echo "Enter the marks(0 - 100) of Math"
read Math[$i]
echo "Enter the mark(0 - 100) of Science"
read Sci[$i]
i=`expr $i + 1`
done
echo "Student Mark's Details"
for ((k=1;k<=${#Std[@]};k++))
do
echo "Std ${Std[k]}" "Eng ${eng[k]}" "Mat ${Math[k]}" "Sci ${Sci[k]}"
done
echo "Final Result"
for ((j=1;j<=$num;j++))
do
if [ ${eng[$j]} -ge 50 ]
then
count=`expr $count + 1`
fi
if [ ${Math[$j]} -ge 50 ]
then
count=`expr $count + 1`
fi
if [ ${Sci[$j]} -ge 50 ]
then
count=`expr $count + 1`
fi
if [ $count == 3 ]
then
echo "Student $j is Pass in All"
else
echo "Student $j is Fail"
fi
count=0
done


6. Shell scripts related to strings and pipes:



a. Write a shell script to input two strings from the user and determine whether they are
same or not.
#!/bin/bash
#Check the String input by user is same or Not
echo "Enter the 1st String"
read str1
echo "Enter the 2nd String"
read str2
if [ $str1 = $str2 ]
then
echo "Strings Are Same"
else
echo "Strings Are Not Same"
fi

b. Write a shell script to input a string from the user and determine its length.

#!/bin/bash
#find a length of String
echo "Enter the String"
read str
echo "Length Of String : ${#str}"

c. Write a shell script to input two strings from the user and find the occurrences of
string2 in string 1.
#!/bin/bash
#find the occurence between two strings
echo "Enter the 1st String"
read str1
echo "Enter the 2nd String"
read str2
grep -o -w "$str2" <<< "$str1" | wc -w

d. Write a shell script to input the name of a file as command line argument and display
the number of characters, words and lines in the file.

#!/bin/bash
#Enter a file using command line count word,char &line
if [ -f$1 ]
then
w=`cat $1 | wc -w`
c=`cat $1 | wc -c`
l=`cat $1 | wc -l`
echo "Number of words = $w"
echo "Number of Char = $c"
echo "Number of lines = $l"
else
echo "Invalid File"
fi

e. Write a shell script to display a list of directories within the current directory and how
much space they consume, sorted from the largest to the smallest.

#!/bin/bash

#Print the list of dir and how much space consume in sorted order

du -sh */ | sort -nr

f. Write a short script count txt to count the total number of .txt files in the current
directory, and print out this number to screen.
#!/bin/bash
#Count txt file in your dir and print number
if [ -d$1 ];then
echo "Number Of files is $(find "$1" -type f | wc -l)"
else
echo "Error"
fi

e. Write a shell script to create a tsv file containing name, roll no. and age of 10 students. Then use that tsv file to display only the names of the students in alphabetical order.

#!/bin/bash

echo "Enter filename"

read file

echo "Sorted Name of Students"

sort $file | awk '{print $1}'

f. You are given a file of tab-delimited weather data (TSV). There is no header
column in this data file. The first five columns of this data are: (a) the name of the
city (b) the average monthly temperature in Jan (in Fahrenheit). (c) The average
monthly temperature in April (in Fahrenheit). (d) The average monthly temperature in
July (in Fahrenheit). (e) the average monthly temperature in October (in
Fahrenheit).You need to sort this file on the basis of average monthly temperature in
April.

#!/bin/bash

echo "Enter filename"

read file

#To get April Column of file sorted

sort -k3n $file

  • Note:
This Code is Verified by CodexRitik.If any error occurs then Comment correct code Below in comment box.

Os Lab Assignment Complete Code Solution Os Lab Assignment Complete Code Solution Reviewed by CodexRitik on November 15, 2020 Rating: 5

11 comments:

  1. Great work buddy keep it up👏👍

    ReplyDelete
  2. Great work, but solution f is wrong, it is returning total no of files, while we need to count only .txt files

    ReplyDelete
    Replies
    1. Yaa You are right in that ques i have written to calculate only number of files where f denotes any types of files so you may use there .txt extension to calculate that file.

      Delete
  3. In some solutions there is an error of bad for loop and something like this . How to resolve that ?

    ReplyDelete
    Replies
    1. i think this is error of compiler or may be linux version pls try to some change in that code.Thanks For Your Valuable Feedback.

      Delete
  4. o bhaie tera section wala to khus ho gya hoga.

    ReplyDelete
  5. Our money assignment help specialists are accessible 24x7 to answer every one of your questions and clear the entirety of your questions. Simply call us now and benefit the best administrations in the town. how to write a reflection paper

    ReplyDelete
  6. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. Class 9 Assignment

    ReplyDelete
  7. I was reading some of your content on this website and I conceive this internet site is really informative ! Keep on putting up. Assignment Answer

    ReplyDelete

Powered by Blogger.