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

Operating System Lab 8

Lab 8

Uploaded by

Babar Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Operating System Lab 8

Lab 8

Uploaded by

Babar Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

OPERATING

SYSTEM LAB MANUAL


INSTRUCTOR NAME: ANEES ASGHAR

2021

LAB 08
LINUX OPERATING SYSTEM
Shell Scripting (Continue)

Variables in Shell Scripting:

 In the previous lab we have studied; how to use variables in shell


scripting.
 We also learned about local variables and environment variables.
 In this lab we will learn about the positional/ command line
parameters.
 We also discussed the read command in the previous lecture.
 Read command is used to provide some input from user.
 Read is a bash built-in command that reads the contents of a line into
a variable.
Taking value at run time from the users:
Till now, in earlier programs we assigned the values to the variables while
writing the script. Now what if you want to take the value at the run time
from the users?
Example:
 Write a shell script that creates two files.
 But the name of the file should be passed by the user.
 After file creation. Display a confirmation message.
 Use ls command to verify the files creation.
Example1
Write a shell script to create a file and enter some data in that file. But the
name of the file will be passed by the user. Also display a confirmation
message after file creation.

Output:
Example 2:
 Create an empty file. File name should be specified by the user.
 Display a confirmation message after the file creation.
 Create second file and enter your name and section in that file.
 Display a confirmation message after the file creation.
 Add some data in the first file. Display the data of both the files.
 Create a directory and display the confirmation message.
 Move the first file in the directory.
Solution:
Create a shell script file using an editor.
Output:

Positional Parameters / Command Line arguments

Positional parameters are also known as command line arguments.


Arguments passed to the script from the command line are called
command line arguments.
Positional parameters are essentially variables that contains the values of
data that are passed through the terminal when you execute the script (it
means enter values at run time).
As discussed in the previous lab the position parameters are a type of
variables. So, Positional parameters are also denoted by a dollar sign.
Example 3:
So, we have used many command till now. As an example ls. We knew
that the ls command is used to list the files or directories in Linux and
other Unix-based operating systems. Sometimes we use ls command like
in the below diagram:

So here in the above diagram, we passed some arguments with the ls


command. In the above example a b and c referred as command line
arguments. Where the ls command itself is the 0th argument “a” is the 1st
argument “b” is 2nd argument and “c” is the 3rd argument and so on.
In this lab we will learn how to use these command line arguments within
the script.
Positional parameter are variables and the data is going to be directly
stored into these variables, if we want to utilize it we don’t need to read it
because the data is going to be read and saved into these parameters
directly.
The values of these command line arguments are stored as below:

 $0 – stores the name of the script.


 $1 – stores the first argument.
 $2 – stores the second argument.
 $3 – stores the third argument.
 $# – stores the total number of argument.
 $* – stores the value of all argument.

Example 4:
Now we will implement all these concepts in a program.

For example we want to create multiple files let’s suppose 3 files using
command line argument.
First, create a script:

Now in editor we simply passed the three command line arguments with
the touch command.

The value of these command line arguments is stored as below:

 $0 – stores the name of the script.


 $1 – stores the first argument.
 $2 – stores the second argument.
 $3 – stores the third argument.
Remember one thing whenever you want to run a program for command
line argument then always pass the arguments next to the file-script name.
As shown in the below example:
Here in the above diagram we passed the arguments f1, f2, f3 next to the
file script name i.e. imp-args.

What if you passed less arguments? For example instead of 3 we passed


1 argument or 2 arguments.

In the above diagram we seen that if we pass only one argument then it
still executes and will create one file only. And doesn’t shows any error.
Similarly if we pass two arguments it will create two files. You can cross
verify with ls command.
But what if we add some extra arguments?
In this case it will not shows any error either. But in this case the files will
be created according to the arguments set in the shell script. It means if in
the shell script only three arguments passed with the touch command then
only three files will be created and the extra arguments will be neglected.
And when you try to access these files then only those files will be
accessible which are created according to the arguments set in the shell
script.
Now let’s extend this example and add some more arguments like $# and
$* to check how these arguments will work.
Sometimes you want to check how many arguments are passed by the user
at command line. In this case we will use $#.

If we pass extra arguments shell will not create extra files. But # will count
the total number of arguments passed.
Now let’s check the functionality of $*. As we know that the $* stores the
value of all the arguments. Now we will see how it works:

Output:
Example 5:
Write a shell script to rename a file the old name and new name must be
passed at command line.
Let’s check the list of all the files and directories first using ls command.

Now create a shell script to rename a file.


Run the program and pass the old name and new name of the file at
command line.

Again use ls command to verify that the name of the file is updated.

Example 5:
Write a shell script to create a file and a directory. The file name should
be passed as first argument and the directory name should be passed as
second argument.

You might also like