0% found this document useful (0 votes)
29 views

Os Lab 3

The document discusses Linux commands like date, cal, cat, head, tail, ls, chmod, sort and tr. It also discusses shell scripting concepts like variables, arithmetic operations, redirection and permissions. The tasks involve: 1) Creating files students.txt, gstudent.txt and pgstudents.txt, copying them to a new directory, appending sorted names from other files to students.txt and changing file permissions. 2) Defining variables x and y, performing arithmetic operations on them and printing the results.

Uploaded by

sdra maan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Os Lab 3

The document discusses Linux commands like date, cal, cat, head, tail, ls, chmod, sort and tr. It also discusses shell scripting concepts like variables, arithmetic operations, redirection and permissions. The tasks involve: 1) Creating files students.txt, gstudent.txt and pgstudents.txt, copying them to a new directory, appending sorted names from other files to students.txt and changing file permissions. 2) Defining variables x and y, performing arithmetic operations on them and printing the results.

Uploaded by

sdra maan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

SIDRA ZAHID

2019-BCS-043

OS LAB # 03
 Implementing Linux Commands

Command: date
This command is used to print system date and time.

Example:

Date

Command: cal
"cal" stands for calendar. It displays the calendar of the current month.
Example:
cal

Command: cat
It displays the content of the file sequentially with no break.
Example:
o cat > file1 (”>” is called Redirection Operator)
It will create the file ‘file1’ and you can enter the text there. Then press “Ctrl+d” for saving
the file. If file1 already exists then it over writes the contents of the file1.
cat >>file1
This example appends more text to already existing file. Then press “Ctrl+d” for saving the
file.

Command: head
It displays the top part/lines of the file. By default it allows 10 lines, but you can change the
settings of the number of lines to be displayed by using –n.
Examples:
o head –n25 file1.txt
This example displays the first 25 lines of the file named “file1.txt”.

Command: tail
It displays the last part/lines of the file. By default it allows 10 lines, but you can change the
settings of the number of lines to be displayed by using –n.

Examples:
o tail –n25 file1.txt
This example displays the last 25 lines of the file named “file1.txt”.
Command: ls -l
This command is use to find permission level of the file.

Command: chmod
Each file in UNIX/LINUX has an associated permission level. This allows the user to prevent
others from reading/writing/executing their files or directories.
If you own a file, you can change its permissions with “chmod”.

Example
ochmod 7 7 7 filename
user group others
Gives user, group and others r, w, x permissions
o chmod 770 filename
Gives the user read, write and execute.
Gives group members read and execute.
Gives others no permissions.
Using numeric representations for permissions:
r = 4; w = 2; x = 1; total = 7
Redirection of Input and Output
Mostly all command gives output on screen or take input from keyboard, but in Linux (and in
other OSs also) it's possible to send output to file or to read input from file.

Command: ls
“ls” command is used to output to screen.
Examples:
o ls > file1
This command put output of ls to file1.
It outputs Linux-commands result (output of command or shell script) to file. Note that if file
already exist, it will be overwritten else new file is created.

o ls >> file1
This command put output of ls at the end of file1.
It output Linux-commands result (output of command or shell script) to end of file. If file exists,
it will be opened and new information/data will be written to end of file, without losing previous
information/data, and if file does not exist, then new file is created.
o ls < file1
This command put contents to command or shell script from the file.
It takes input to Linux-command from file instead of key-board.

Command: sort
“sort” is used to sort the contents in the file.
Examples:
o sort file1
This command sorts the contents in the file1.
o sort < file1 >sort_names

o cat sort_names
In above example sort command takes input from “file1” file and output of sort command (i.e.
sort_name) is redirected to sort_name file.
o tr "[a-z]" "[A-Z]" < file1 >cap_names
o cat cap_names
tr command is used to translate all lower case characters to upper-case letters. It takes input from
“file1” file, and tr's output is redirected to cap_names file.

Shell Scripts and C programming

Variables in Shell

To process our data/information, data must be kept in computers RAM memory. RAM memory
is divided into small locations, and each location had unique number called memory
location/address, which is used to hold our data. Programmer can give a unique name to this
memory location/address called memory variable or variable

In Linux (Shell), there are two types of variable:

o System variables - Created and maintained by Linux itself. This type of variable defined
in CAPITAL LETTERS.
o User defined variables (UDV) - Created and maintained by user. This type of variable
defined in lower letters.

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:

o no=10
It define variable called ‘no’ having value 10.

o vech=Bus
Itdefine variable called 'vech' having value Bus

There are rules for naming variable name (Both UDV and System Variable)

o Variable name must begin with Alphanumeric character or underscore character


(_), followed by one or more Alphanumeric character.
o Don't put spaces on either side of the equal sign when assigning value to variable. e.g. In
following variable declaration there will be no error.
o Variables are case-sensitive, just like filename in Linux. For example:
To print or access value of UDV (User defined variables)

Example:

o vech=Bus
o echo $vech
It will print the value of vech.

Shell Arithmetic

Use to perform arithmetic operations.

Examples:

o expr 1 + 3
o expr 2 - 1

These examples print the result.

By default in Linux if particular command/shell script is executed, it return two type of values
which is used to see whether command or shell script executed is successful or not.

(1) If return value is zero (0), command is successful.

(2) If return value is nonzero, command is not successful or some sort of error executing
command/shell script.
LAB TASKS

Task 1:

Create file name students.txt, gstudent.txt, pgstudents. Enter students’ names in gstudent.txt and
pgstudent.txt. Now create directory having name OSLAB 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 gstudent.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.
Task 2:
Define variable x and y with value 20 and 30and print it on screen, then sum, subtract, divide
these numbers and print it on the screen.

You might also like