Operating System Lab: Fall - 2020

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Operating System Lab Lab – 5

Fall - 2020 Streams, Redirections, Shell Programming

The objective of current lab is understanding the concept of streams, handling the
data. To implement the concept of redirections trough operators. There is also
portion in which we will see the concept of shell programming.
Date Time

$ date
Description: Prints the system date and time.

Calendar

$ cal
Description: Prints an ASCII calendar of the current month

cat Command

$ cat [option] [file name]


Description: Contents of the files are displayed sequentially with no break. Also files
display “Concatenated”

$cat > flie1 (”>” is called Redirection Operator)


It will create the file ‘file1’ and you can enter the text there. Press [Ctrl+d] for saving the
file. If file1 already exists then it over writes the contents of the file1.

You can see the file in home directory.

Page 1 of 6
Operating System Lab Lab – 5
Fall - 2020 Streams, Redirections, Shell Programming

$cat >>flie1 (”>>” is called Redirection Operator (Append))


The above command is used to append more text to already existing file. Press [Ctrl+d] for
saving the file.

You can see the content file by using cat command and see the updated
file.

head
Syntax: $ head [option] [Filename]
Description: “head” displays the top part of a file. By default it shows the first 10 lines. -n
allows you to change the number of lines to be shown.

Page 2 of 6
Operating System Lab Lab – 5
Fall - 2020 Streams, Redirections, Shell Programming

You can customize the number of lines to display just mentioned the
numbers of lines as below.

head –n2 studentName

Tail
Syntax: $ tail [option] [Filename]
Description: Display last 10 (by default) lines of a file. Same as head command.

You can customize the number of lines to display just mentioned the
numbers of lines as below.

tail –n2 studentName

TASK 01
Creating three files file1, file2 and file3. Place some contents and merge into new file.

Page 3 of 6
Operating System Lab Lab – 5
Fall - 2020 Streams, Redirections, Shell Programming

sort
Example: Create text file sname as follows
$cat > sname
b
a
c
d
Press CTRL + D to save.

Sort the alphabets.

LAB TASK 02
Create text file studentName as follows
virk
ash
zebra
babu

Sort the data in studentName file .

Page 4 of 6
Operating System Lab Lab – 5
Fall - 2020 Streams, Redirections, Shell Programming

LAB TASK 03

Try to capitalize the data in above studentName file.

$ tr "[a-z]" "[A-Z]" < studentName > NewStudentsName

LAB TASK 04

Create file name students.txt, pstudent.txt, fstudents. Enter students’ names in


pstudent.txt and fstudent.txt. Now create directory having name Riphah and copy all files
to this directory. Now append the file students.txt with first five sorted names from
pstudent.txt and last five sorted names from fstudent.txt. Then show the contents of sorted
names from file students.txt. Change the permissions of file students.txt read only and
both other files read and execute only.

Shell Scripts
Variables in Shell

(1)System variables –
Created and maintained by Linux itself. This type of variable defined in CAPITAL
LETTERS.
(2) User defined variables (UDV) –
Created and maintained by user. This type of variable defined in lower letters. You
can see system variables by giving command like $ set, some of the important
System variables are: HOME, USER
NOTE that some of the above settings can be different in your PC/Linux
environment. You can print any of the above variables contains as follows:
Execute following.

$ echo $USERNAME
$ echo $HOME

How to define User defined variables (UDV)


To define UDV use following syntax
Syntax: variable name=value 'value' is assigned to given 'variable name' and
Value must be on right side = sign. Example:
$ no=10 # this is ok

$ 10=no # Error, NOT Ok, Value must be on right side of = sign To define variable

Page 5 of 6
Operating System Lab Lab – 5
Fall - 2020 Streams, Redirections, Shell Programming

called 'vech' having value Bus


$ vech=Bus To define variable called n having value 10
$ n=10
Rules for Naming variable name (Both UDV and System Variable) 1. Variable name
must begin with Alphanumeric character or underscore character (_), followed by
one or more Alphanumeric character.

Class Activity:05

Exercise the following commands and check the output of these


commands.
$ no=10
$ No=11
$ NO=20
$ nO=2
$ vech=
$ vech=""
Note: Use $ sign to access the value of the variable.

Class Activity:06

(1) How to define variable x with value 10 and print it on screen?


(2) How to define variable xn with value Ali and print it on screen?
(3) How to print sum of two numbers, let's say 6 and 3?
(4) How to define two variable x=20, y=5 and then to print division of x and y?
(5) Modify above and store division of x and y to variable called z.

Instructor:
Engr.Khateeb Khan
Faculty of Computing, Riphah International University
Islamabad
Email: [email protected]

________________________ The End ________________________

Page 6 of 6

You might also like