Professional Documents
Culture Documents
Lab 6
Lab 6
Lab 6
ls
In order to execute this command from a C-program the system call system() is
used. The general form of system command is:
This function is declared in <stdio.h>. the system returns the value –1 if the
system() does not executed, and 0 if executed successfully .
Example:
The following C program lists all the files in the current directory using C Program.
#include<stdio.h>
int main(void)
{
system("ls");
return 0;
}
Exercise 1:
echo Command:
Command Description
echo Echoes back(print) whatever you type on the command line.
Page 1 of 6
Operating System Lab Lab – 6
Fall - 2019 Kernel Properties, File Sorting
Formatting Echo:
30 Black
31 Red
32 Green
33 Yellow
34 Blue
35 Magenta
36 Cyan
37 White
40 Black
41 Red
42 Green
43 Yellow
44 Blue
45 Magenta
46 Cyan
47 White
So to get a blinking line with Blue foreground and Green background, the
combination to be used should be
echo –e "\033[5;34;42m In color"
which actually is very ugly. :-) Revert back with
echo –e "\033[0;37;40m"
Exercise 2:
Try the following
clear
echo -e "\033[1m Hello World" # bold effect
Page 2 of 6
Operating System Lab Lab – 6
Fall - 2019 Kernel Properties, File Sorting
echo -e "\033[5m Blink" # blink effect
echo -e "\033[0m Hello World" # back to normal
Who Command:
Command Description
who Who command lists the login names, terminal lines, and
login times of the users who are currently logged on to the
Example:
Who am i Command:
Command Description
who am i If you type who am I, Linux displays who the system thinks you
are
Example:
System/Kernel Properties:
Page 3 of 6
Operating System Lab Lab – 6
Fall - 2019 Kernel Properties, File Sorting
Linux Kernel comes with complete versioning number with fixed format as given
below
<major>.<minor>.<path>-<build>.<linux>.<architecture>
[note dot (.) and dash(-) in between]
Exercise 3:
Print(ls) contents of /lib/modules directory by pressing command
ls /lib/modules
Now run following command
uname -r
Note output of both above commands.
Uname Command:
Command Description
uname Print Operating System Name like Linux or Unix
Exercise 4:
Run all above switches with uname command.
File Sorting:
Sort Command:
Command Description
Page 4 of 6
Operating System Lab Lab – 6
Fall - 2019 Kernel Properties, File Sorting
sort Sorts a column in a file in alphabetical order. By default the output
is displayed on your terminal, but you can specify filename as the
argument or redirect the output to a file.
options You can use following option with sort command
Example:
From lab-4, we know how to create files. Now create new file named labsort and
insert following text
End of file
6 apples
file to be sorted
apple on the tabl
25 years old
File To be Sorted
Now execute
sort labsort
Page 5 of 6
Operating System Lab Lab – 6
Fall - 2019 Kernel Properties, File Sorting
Exercise 5:
Now use other option with sort command and observe output.
Exercises 6:
12 Operation payback
6 Network security
14 Hacking is fun
7 Tiger lives in jungle
13 Pakistan will win today
One column contains numeric values while other contain text. Now perform sorting
on both column separately, also perform -n switch on both columns again and
observe the pattern.
I/O Redirection:
We have used concept of I/O redirection in previous lab, keeping lab-5 in mind
Understand and perform following exercise.
Exercise 7:
What would be the result of the following commands.
- cat filename > new // filename is name of file that exist at current path
- date > new
- ls | sort –r
- ls | sort -r >> new
------------------------------------------------------------------------------------------------
Instructor: Atizaz Ahmad (atizaz.ahmad@riphah.edu.pk)
Page 6 of 6