OS_LAB_03
OS_LAB_03
Operating Systems
Experiment 3
Implementation of LINUX Commands -II
(Linux)
Date Time
$ date
Description: Prints the system date and time.
Calendar
$ cal
Description: Prints an ASCII calendar of the current month
head
Description:
Examples
head –n 50 file.txt
Displays the first 50 lines of the file.txt
tail
Syntax: $ tail [option] [Filename]
Description:
Example
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
Syntax
ls –l [filename]
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
If you own a file, you can change its permissions with “chmod”.
Syntax:
Example
Let's say you are the owner of a file named myfile, and you want to set its permissions so that:
The equals sign ("=") means "set the permissions exactly like this," and the letters "r", "w",
The commas separate the different classes of permissions, and there are no spaces in
between them.
Here the digits 7, 5, and 4 each individually represent the permissions for the user, group,
Example 1
chmod 7 7 7 filename
r = 4; w = 2; x = 1; total = 7
Sort Command
Example
virk
ash
zebra
babu
$ 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
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
$ cat cap_names
Shell Scripts
1. Open editor
2. Type code
Type
A=15
To display an actual dollar sign, you must precede it with a backslash character:
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
in Linux shell scripting we are using two types of variables: System Defined Variables &
1. System variables
These are the variables which are created and maintained by Operating System (Linux)
itself.
$ echo $USERNAME
$ echo $HOME
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,
No spaces can appear between the variable, the equal sign, and the value (another trouble
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
var1=10
var3=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
Example:
Var1=10 # this is ok
20=Var1 # Error ?
Var1=10
Var2=20
echo $Var1
echo $Var2
OUTPUT:
10
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
20
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 (_),
2. Don't put spaces on either side of the equal sign when assigning value to variable.
$ 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
$ 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
5. You can define NULL variable as follows (NULL variable is variable which has no value at
$ vech=
$ vech=""
$ 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
Syntax: $variablename
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
Example:
myname=Ahmad
myos = OS_Linux
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
myno=5
Shell Arithmetic
Syntax
expr is keyword
Examples:
expr 1 + 3
var1=10
var2=20
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
echo `expr 6 + 3`
In above command, before expr keyword we used ` (back quote) sign not the (single quote
Back quote is generally found on the key under tilde (~) on PC keyboard OR to the above
of TAB key.
Here if you use double quote or single quote, it will NOT work.
Example
echo `expr 6 + 3`
It will print 9
It will print today's date as, Today is Tue Jan ....,Can you see that the `date` statement uses back
quote?
Exit Status
values which is used to see whether command or shell script executed is successful or not.
If return value is nonzero, command is not successful or some sort of error executing
command/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
$ echo $?
$ ls
$ echo $?
Lab Tasks
Task 1:
1. Create file name students.txt, pstudent.txt, fstudents.
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
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
X=20
Y=5
$X *$Y = 100
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING
20 * 5 = 100