Assignment 3

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

School of Computer Science Engineering and Technology

Course- BTech Type- Core


Course Code- CSET209 Course Name- Operating Systems (OS)
Semester- Even Date- 16/01/2025 Batch- 2023-2027

Lab Assignment No. 3


Objective: To understand the significance of shell scripting, write and execute a shell script in
Linux operating system.
Background: A shell script in Linux is a text file containing a sequence of commands written
in a scripting language interpreted by the shell. The shell acts as an interface between the user
and the operating system, interpreting commands entered by the user or read from a script file.
Linux offers a variety of powerful shells with robust functionality, including Bash, Zsh, Tcsh,
and Ksh. One good thing about these subsets of shell scripting is that they are all relatively easy
to program. Most of the shell scripting done on Linux involves the bash shell. We will stick
with bash scripts in our labs due to their widespread popularity and immense usability.
The very first line of a bash script needs to start with the SheBang (#!) followed by the path to
the bash executable program (/bin/bash). The bash script is written in an editor (such as, vim)
and it is saved using .sh file extension. To execute the shell script on terminal you need to
write: sh <filename.sh>

Here are some basic examples of shell scripts:

1. Writing a simple shell script to print Hello World string


Use an editor like vim to create the file hello-world.sh and write the following lines of code:
#!/bin/bash echo
"Hello World"

Save and quit the file. Next, you need to make this file executable using the below command:
$ chmod 755 hello-world.sh
Now, you can run this script using any of the below two commands:
$ sh hello-world.sh
$ ./hello-world.sh
2. Defining Variables in a Bash Script
Variables in shell scripting are containers for storing necessary information. In Bash Script,
declare a variable by assigning a value to its reference by = operator. Furthermore, print the
assigned values using echo $(VARIABLE_NAME).
Example:
#!/bin/bash
# Declaration of variables
name=Tom age=12
# Displaying variables
echo $name $age

Output: Tom 12

3. Read User Input with Prompt Message

The read command used with option -p allows you to prompt a message along with taking user
input. You can use echo $(VARIABLE_NAME) to display the user input on the screen.

Example: #!/bin/bash read -p


"Enter a number:" num
echo “The number is: $num”

Output:
Enter a number: 12
The number is: 12

4. Changing Internal Field Separator (IFS)/Delimiter

By changing the IFS, you will be able to access values separated by your desired delimiter.
First, store the default IFS in a variable using old_IFS = $IFS. Now, change IFS according to
your preference and complete the task. At the end, restore the original IFS using IFS =
$old_IFS. The below script shows how:

#!/bin/bash #store
default IFS
old_IFS= $IFS
IFS=,
read val1 val2 val3 <<< "5,60"
echo 1st value: $val1 echo 2nd
value: $val2
#restore default IFS
IFS= $old_IFS;

Output:
1st value: 5
2nd value: 60

5. The For Loop

A simple example is demonstrated below:

#!/bin/bash
for (( counter=1; counter<=10; counter++ )) do
echo -n "$counter "
done

This script should print out the numbers 1 to 10.

6. The While Loop

A simple example is demonstrated below:

#!/bin/bash counter=0
while [ $counter -le 10 ] do
echo Number: $counter ((counter++))
done

This script should print out the numbers 1 to 10.

7. How to set your $PATH variable

Let us say a file is located in a directory called /place/with/the/file. Then to add this path to the
$PATH variable use the following command:

export PATH=$PATH:/place/with/the/file

NOTE: If you are using Google cloud shell then for downloading any file to the local system
use the following command: cloudshell download <filename> Final Task for submission:

1. Using vim editor to write and save a simple shell script that outputs “Shell scripting
is an awesome way to carry out complex tasks easily.”
2. Make the script executable by changing its permissions using chmod command (chmod
755 will allow everyone to execute the script).
3. To run the script just by entering its name on the terminal, change the PATH variable
to contain the location of your present working directory.

4. Run the script by typing its name on terminal.


5. Use echo command to write the attendance record of your past semester in a text file
(named, <enrolment_number.txt>) as follows:
6. Write a shell script (named, attendance.sh) to take input from the
<enrolment_number.txt> file, and outputs an HTML file (named,
<your_name.html>) containing the attendance record of your past semester in tabular
format. The output HTML document when opened in any browser should display the
information in the form of a table as shown below:

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