Operating System Lab 8
Operating System Lab 8
2021
LAB 08
LINUX OPERATING SYSTEM
Shell Scripting (Continue)
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:
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.
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.
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.