OS Lab
OS Lab
Practical 1
3. man Command
● It is the interface used to view the system's reference manuals.
● Syntax : man [command name]
● Example
4. Echo Command:
● Display a line of text/string on standard output or a file.
● Syntax : echo [option] [string]
echo -n Do not output a trailing newline
echo -e Enable interpretation of backslash escape sequences
Example :
5. Cal command:
● Displays a simple, formatted calendar in your terminal.
● Syntax : cal [options] [[[day] month] year]
Example:
6. Date Command
● Print or set the system date and time.
● Syntax : date [OPTION]... [+FORMAT]
date +%a The abbreviated weekday name (e.g., Sun)
date +%A The full weekday name (e.g., Sunday)
date +%b The abbreviated month name (e.g., Jan)
date +%B Locale's full month name (e.g., January)
date +%C The current century; like %Y, except omit last two digits (e.g., 20)
date +%w day of week (0..6); 0 is Sunday 25 date Command Option Use
date +%d Display the day of the month
date +%m Displays the month of year (01 to 12)
date +%y Displays last two digits of the year(00 to 99)
date +%Y Display four-digit year.
date +%T Display the time in 24 hour format as HH:MM:SS
date +%H Display the hour
date +%M Display the minute
date +%S Display the seconds
date +%V ISO week number, with Monday as first day of week (01..53)
date +%P locale's equivalent of either AM or PM
Example:
7. Clear Command
● Clear the terminal screen.
● If you take a detailed look after running the clear command, you'll find that it
doesn't really clear the terminal. The tool just shifts the text upwards, out of the
viewable area.
● Syntax : clear
Example:
8. cat command:
● It is used to create, display and concatenate file contents.
● Syntax : cat [OPTION] [FILE]
Example :
$cat file.txt > newfile.txt
• Read the contents of file.txt and write them to newfile.txt, overwriting anything
newfile.txt previously contained. If newfile.txt does not exist, it will be created.
$cat file.txt >> newfile.txt
• Read the contents of file.txt and append them to the end of newfile.txt. If
newfile.txt does not exist, it will be created
cat file1.txt file2.txt
• It will read the contents of file1.txt and file2.txt and display the result in the
terminal.
cat -b:: Omits line numbers for blank space in the output
27. ps Command
● Reports a snapshot of the status of currently running processes.
● Syntax : ps [option]
Example
1. wc Command
● It s used to find out number of newline count, word count, byte and characters
count in a files specified by the file arguments.
● Syntax : wc [options] filenames
2. ln Command
● ln creates links between files.
● ln creates hard links by default, or symbolic links if the -s (-- symbolic) option is
specified. When creating hard links, each TARGET must exist.
● Syntax : ln [OPTION]… [-T] TARGET LINK_NAME
3. nl command
● nl command numbers the lines in a file.
● Syntax : nl [OPTION]... [FILE]...
Example :
4. head Command
● head makes it easy to output the first part (10 lines by default) of files.
● Syntax : head [OPTION]... [FILE]...
● Example :
5..tail Command
● tail is a command which prints the last few number of lines (10 lines by default) of
a certain file, then terminates.
● Syntax : tail [OPTION]... [FILE]...
6.sort Command
● sort command is used to sort a file, arranging the records in a particular order.
● By default, the sort command sorts file assuming the contents are ASCII. Using
options in sort command, it can also be used to sort numerically.
● Syntax : sort [OPTION]... [FILE]...
7. find Command
● find command searches for files in a directory hierarchy.
● Syntax : find [option] [path...] [expression]
8.uniq Command
● uniq reports or filters out repeated lines in a file.
● It can remove duplicates, show a count of occurrences, show only repeated lines,
ignore certain characters and compare on specific fields.
● Syntax : uniq [OPTION]... [INPUT [OUTPUT]]
9.grep Command
● The grep filter searches a file for a particular pattern of characters, and displays
all lines that contain that pattern.
● The pattern that is searched in the file is referred to as the regular expression
● . • grep stands for globally search for regular expression and print out.
● Syntax : grep [options] pattern [files]
10.pipe (|) Command
● It redirects the command STDOUT or standard output into the given next
command STDIN or standard input.
● In short, the output of each process directly as input to the next one like a
pipeline.
● The symbol '|' denotes a pipe.
● Pipes help you mash-up two or more commands at the same time and run them
consecutively. Syntax : command_1 | command_2 | command_3 | .... |
command_N
Practical-3
Program:
echo "*****************"
echo "Student Marksheet"
echo "*****************"
echo "Enter Operating System Marks:"
read os
echo "Enter C++ Marks:"
read cpp
echo "Enter Java Marks:"
read java
echo "*****************"
total=`expr $os + $cpp + $java`
echo "Total Marks:"$total
percentage=`expr $total / 3`
echo "Percentage:" $percentage %
if [ $percentage -ge 60 ]
then
echo "Class: First Class Distinction"
elif [ $percentage -ge 50 ]
then
echo "Class: First class"
elif [ $percentage -ge 40 ]
then
echo "Class: Second class"
else
echo "Class: Fail"
fi
echo "*****************"
output:
Practical-4
Practical-5
Aim:Write a shell script which will generate first n fibonnacci numbers like: 1, 1, 2,
3, 5, 13, …
Program:
Output:
Practical 8:
Aim:Write a menu driven shell script which will print the following menu and
execute the given task.
Program:
echo "Select Anyone Option"
echo "**************************************************"
echo "1)Display Calener of current month"
echo "2)Display Today's Date and Time"
echo "3)Display Username who are currently logged in"
echo "4)Display your name on given x,y position"
echo "5)Display your terminal Number"
echo "6)Exit"
echo "Enter your choice:"
read ch
case $ch in
1)cal;;
2)date;;
3)who;;
4)row=$(tput lines)
col=$(tput cols)
echo "Terminal Window has Rows=$row Cols=$col"
echo "Enter desired X,Y position"
echo "X position="
read x
echo "Y position="
read y
echo "Enter the name"
read name
tput cup $x $y
echo "$name";;
5)tty;;
6)echo "Exit";;
*)echo "Enter valid choice";;
esac
Output:
Practical 9:
Aim:Write a shell script to read n numbers as command arguments and sort them
in descending order.
Program:
Output:
Practical 10:
Aim:Write a shell script to display all executable files, directories and zero sized
files from current directory.
Program:
echo Executable files
files=$(find lab_solutions -executable -type f)
echo $files
echo
echo List of Directories
dir=$(ls -d )
echo $dir
echo
echo List of zero sized files
zero=$(find -size 0)
echo $zero
Output:
Practical 11:
Program:
Output:
Practical 12:
Aim:Shell programming using filters (including grep, egrep, fgrep)
Program:
echo "Main Menu"
echo "Press 1 for using Grep Command"
echo "Press 2 for using Egrep Command"
echo "Press 3 for using Fgrep Command"
read a
case $a in
1) echo "For single pattern search, Enter Pattern below :"
read b
grep "$b" City_names.txt
;;
2) echo "For double Pattern search, Enter b,c pattern :"
read b
read c
egrep "$b" City_names.txt
grep -E "$c" City_names.txt
;;
3) echo "For Pattern From a File, Enter Pattern :"
read b
grep -F "$b" City_names.txt
;;
esac
Output:
Practical 14:
Aim: Write a shell script to validate the entered date. (eg. Date format is :
dd-mm-yyyy)
Program:
echo "Enter Valid Date"
read date
echo "You have entered $date"
date -d $date
if [ $? -eq 0 ]
then
echo "Enter Date is Valid"
else
echo "Enter Date is Invalid"
if
Output:
Practical 15:
Aim:Write an awk program using function, which convert each word in a given
text into capital
Program:
echo "Enter the String"
a=$(awk 'BEGIN{
getline str;
print toupper(str);
}')
echo $a
Output:
Practical 16:
Program:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main() {
pid_t pid, mypid, myppid;
pid = getpid();
printf("Before fork: Process id is %d\n", pid);
pid = fork();
if (pid < 0) {
perror("fork() failure\n");
return 1;
}
// Child process
if (pid == 0) {
printf("This is child process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
} else { // Parent process
sleep(2);
printf("This is parent process\n");
mypid = getpid();
myppid = getppid();
printf("Process id is %d and PPID is %d\n", mypid, myppid);
printf("Newly created process id or child pid is %d\n", pid);
}
return 0;
}
Output: