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

OS_LAB_03

The document outlines an experiment on implementing Linux commands, detailing various commands such as date, cal, head, tail, and chmod for file permissions. It also explains shell scripting, variable definitions, and arithmetic operations in shell scripts, along with examples for clarity. Additionally, it includes lab tasks for practical application of the discussed concepts.

Uploaded by

Nabeel Chaudhry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

OS_LAB_03

The document outlines an experiment on implementing Linux commands, detailing various commands such as date, cal, head, tail, and chmod for file permissions. It also explains shell scripting, variable definitions, and arithmetic operations in shell scripts, along with examples for clarity. Additionally, it includes lab tasks for practical application of the discussed concepts.

Uploaded by

Nabeel Chaudhry
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Operating Systems
Experiment 3
Implementation of LINUX Commands -II

(Linux)

CLO 2. Use modern tools and languages.


CLO 3. Demonstrate an original solution of problem under discussion.
CLO 4. Work individually as well as in teams
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Implementing Linux Commands

Date Time

$ date
Description: Prints the system date and time.

Calendar

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

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.

Examples

head –n 50 file.txt
 Displays the first 50 lines of the file.txt

$head -18 filename


 Displays the first 18 lines of the file called filename.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

tail
Syntax: $ tail [option] [Filename]

Description:

 Display last 10 (by default) lines of a file.

 Same as head command.

Example

$tail -12 filename

Displays the last 12 lines from the ending

File Permissions
 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.

Description

 To find permission level of the file.

Syntax

ls –l [filename]
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

The permission levels are

 “r” means “read only” permission.

 “w” means “write” permission.

 “x” means “execute” permission.

 In case of directory, “x” grants permission to list directory contents.

“chmod” Command to change Permission Level of file:

 If you own a file, you can change its permissions with “chmod”.

Syntax:

$ chmod [user/group/others/all] +[permission] filename

Example

Let's say you are the owner of a file named myfile, and you want to set its permissions so that:

1. the user can read, write, ande xecute it;

2. members of your group can read ande xecute it; and

3. others may only read it.


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

This command will do the trick:

chmod u=rwx,g=rx,o=r myfile

1. Symbolic permissions notation

 The above command uses Symbolic permissions notation

 letters u, g, and o stand for "user", "group", and "other".

 The equals sign ("=") means "set the permissions exactly like this," and the letters "r", "w",

and "x" stand for "read", "write", and "execute", respectively.

 The commas separate the different classes of permissions, and there are no spaces in

between them.

2. Octal permissions notation

 Here is the equivalent command using octal permissions notation:

chmod 754 myfile

 Here the digits 7, 5, and 4 each individually represent the permissions for the user, group,

and others, in that order.

 Each digit is a combination of the numbers 4, 2, 1, and 0:

 4 stands for "read",


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 2 stands for "write",

 1 stands for "execute", and

 0 stands for "no permission.

 7 is the combination of permissions 4+2+1 (read, write, and execute)

 5 is 4+0+1 (read, no write, and execute)

 4 is 4+0+0 (read, no write, and no execute).

Example 1

chmod 7 7 7 filename

user group others

Gives user, group and others r, w, x permissions

$chmod 750 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

Execute this chmod 514 filename.?????


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Sort Command

Example

 Create text file sname as follows

$cat > sname

virk

ash

zebra

babu

Press CTRL + D to save.

 Now issue following command.

$ sort < sname > sorted_names

$ cat sorted_names

Output:

ash

babu

virk

zebra

 in above example sort ($ sort < sname > sorted_names) command takes input from sname

file and output of sort command (i.e. sorted names) is redirected to sorted_names file.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

tr Command

 tr stands for translate. is used to translate all lower-case characters to upper-case letters.

 It takes input from sname file, and tr's output is redirected to cap_names file.

Example

$ tr “[a-z]” “[A-Z]” <snames> cap_names

$ cat cap_names

Shell Scripts
1. Open editor

2. Type code

3. save with .sh extension

4. run command as “sh lab3.sh”


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Type
A=15

$ echo “The cost of the item is $A”

The cost of the item is 15

 To display an actual dollar sign, you must precede it with a backslash character:

$ echo “The cost of the item is \$15”

The cost of the item is $15

Variables in Shell
 A variable in a shell script is a means of referencing a numeric or character value.

 And unlike formal programming languages, a shell script doesn’t require you to declare a

type for your variables.

 in Linux shell scripting we are using two types of variables: System Defined Variables &

User Defined Variables.

1. System variables

 These are the variables which are created and maintained by Operating System (Linux)

itself.

 Generally, these variables are defined in CAPITAL LETTERS.

 We can see these variables by using the command “$ set “.


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 some of the important System variables are: HOME, USER

 You can print any of the above variables contains as follows:

$ echo $USERNAME

$ echo $HOME

2. User defined variables (UDV)

 Created and maintained by user. These variables are defined by users.

 A shell script allows us to set and use our own variables within the script.

 Setting variables allows you to temporarily store data and use it throughout the script,

making the shell script more like a real computer program

How to define User defined variables (UDV)

 Values are assigned to user variables using an equal sign.

 No spaces can appear between the variable, the equal sign, and the value (another trouble

spot for novices).

 Here are a few examples of assigning values to user variables.

Syntax:

variable name=value

'value' is assigned to given 'variable name' and Value must be on right side = sign.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

var1=10

var3=testing

var4=“still more testing”

 The shell script automatically determines the data type used for the variable value.

 Variables defined within the shell script maintain their values throughout the life of the shell

script but are deleted when the shell script completes.

Example:

Var1=10 # this is ok

20=Var1 # Error ?

To define variable called 'vech' having value Bus

Var1=10

Var2=20

to print the value of vatable type:

echo $Var1

echo $Var2

OUTPUT:

10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

20

How to print this ?

Var1=10

Var2=20

Note:

 It’s important to remember that when referencing a variable value you use the dollar sign,

 when referencing the variable to assign a value to it, you do not use the dollar sign.

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.

2. Don't put spaces on either side of the equal sign when assigning value to variable.

3. In following variable declaration there will be no error.

$ no=10

But there will be problem for any of the following variable declaration:

$ no =10

$ no= 10

$ no = 10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

4. Variables are case-sensitive, just like filename in Linux. For e.g.

$ no=10

$ No=11

$ NO=20

$ nO=2

 Above all are different variable name, so to print value 20 we have to use “echo $NO” and

not any of the following

$ echo $no # will print ? but not 20

$ echo $No# will print ? but not 20

$ echo $nO# will print ? but not 20

5. You can define NULL variable as follows (NULL variable is variable which has no value at

the time of definition). e.g.

$ vech=

$ vech=""

 Try to print its value by issuing following command

$ echo $vech

Nothing will be shown because variable has no value i.e. NULL variable.
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

How to print or access value of UDV (User defined variables)

 To print or access UDV use following syntax

Syntax: $variablename

 Define variable vech and n as follows:

vech=Bus

n=10

echo $vech

echo $n

Caution

 Do not try $ echo vech, as it will print vech instead its value 'Bus' and $ echo n, as it will

print n instead its value '10',

 You must use $ followed by variable name.

Example:

 Open editor and write the following code.

myname=Ahmad

myos = OS_Linux
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

myno=5

echo "My name is $myname"

echo "My os is $myos"

echo "My number is myno, can you see this number"

Shell Arithmetic

 Use to perform arithmetic operations.

 Syntax

expr op1 math-operator op2 ,

expr is keyword

Examples:

expr 1 + 3

expr $var1 + $var2

expr 20 %3 - Remainder read as 20 mod 3 and remainder is 2.

expr 10 \* 3 - Multiplication use \* and not * since its wild card.

var1=10

var2=20
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

var3=`expr $var1 + $var2`

echo var3= $var3

 For the last statement not the following points

echo `expr 6 + 3`

 In above command, before expr keyword we used ` (back quote) sign not the (single quote

i.e. ') sign.

 Back quote is generally found on the key under tilde (~) on PC keyboard OR to the above

of TAB key.

 Second, expr is also end with ` i.e. back quote.

 Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum.

 Here if you use double quote or single quote, it will NOT work.

Example

$ echo "expr 6 + 3"

It will print expr 6 + 3


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

$ echo 'expr 6 + 3'

It will print expr 6 + 3

echo `expr 6 + 3`

It will print 9

$ echo "Today is date"

Can't print message with today's date.

$ echo "Today is `date`".

It will print today's date as, Today is Tue Jan ....,Can you see that the `date` statement uses back

quote?

More about Quotes

 There are three types of quotes


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Exit Status

 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.

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

 If return value is nonzero, command is not successful or some sort of error executing

command/shell script.

 But how to find out exit status of command or shell script?

 Simple, to determine this exit Status you can use echo $? special variable of shell.

Example

This example assumes that “unknow1file” does not exist on your hard drive

$ rm unknow1file
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

 It will show error as follows

rm: cannot remove `unkowm1file': No such file or directory

and after that if you give command

$ echo $?

 it will print nonzero value to indicate error.

 Now give command

$ ls

$ echo $?

It will print 0 to indicate command is successful.


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Lab Tasks
Task 1:
1. Create file name students.txt, pstudent.txt, fstudents.

2. Enter students’ names in pstudent.txt and fstudent.txt.

3. Now create directory having name UET and copy all files to this directory.

4. Now append the file students.txt with first five sorted names from pstudent.txt and last

five sorted names from fstudent.txt.

5. Then show the contents of sorted names from file students.txt.

6. Change the permissions of file students.txt read only and both other files read and

execute only.

Task 2:
1. How to define variable x with value 10 and print it on screen with Variable name and Value?

2. How to define variable xn with value Ali and print with Variable name and Value?

3. How to define two variable x=20, y=5 and then to print multiplication result of x and y with

multiplication expression? See below:

Result should be:

X=20

Y=5

$X *$Y = 100
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

20 * 5 = 100

4. Modify above and store division of x and y to variable called z.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy