06 OS06 Shell Scripting LAB

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

Course: Operating Systems Date: 30/09/2024

Lab: Shell Scripting

1. What is a Shell?

 A shell is a command-line interface (CLI) that provides a way for users to interact with the operating
system.
 Shell is a program that takes your commands (text-based) and sends them to the operating system
to execute.
 Examples of shells include Bourne Shell (sh), Bash (Bourne-Again Shell), rbash, dash

2. Which shells does my system support?

 Cat /etc/shells
 Write the output

3. Where bash is located?

 which bash
 Write the output

4. On the command line interface, what is the current working directory of your system?

 pwd
 Write the output

5. Navigate to the current user directory and create a new directory called ‘ostasks’ and change the
directory to ostasks

 cd /home/{user name}
 mkdir ostasks
 cd ostasks

Page 1 of 6
6. Create a new file with .sh extension named ‘firstprogram’

 touch {file name}.sh


 open this file in any editor for example gedit, nano, vim and display the following message “Hello”
 Execute the file and see the output

7. Give the executable permissions to the ‘firstprogram.sh’ file

 chmod +x {file name}.extension

8. Edit the same file and write a command that first saves the current date and time of the system and
then displays it.

#!/bin/bash

echo “hello”
current_datetime=$(date)
echo “Current Date and Time: $current_datetime”

9. To write a comment in a shell script use ‘#” hash symbol before the comment

 # this is a comment

10. Two types of variables

a) System Variables (Upper case letters)


b) User defined Variable (lower case letters)
# Following are some system variable examples

echo “$BASH” #displays the name of the shell


echo “$BASH_VERSION” #displays the version of bash
echo “$HOME” #displays the home directory
echo “$PWD” #displays the current working directory

# User define variable

temp=10
echo “the value of temp = $temp”

11. Identify the name of user defined variable in section 8? [Reply: ]

12. How to read inputs from the terminal?

#! /bin/bash

echo “Enter name: ”


read name
echo “Entered name: $name”

Page 2 of 6
# how to read from the same line

read –p “username: “ username_val


# -p flag is use to get the input from the same line
read –sp “password: “ password_val
# -s flag is use to silently enter the password
echo “username : $username_val”
echo “password: $password_val”

13. Arrays

# reading the inputs and saving in arrays

echo “enter names: “


read –a names # -a flag is use to read inputs as arrays
echo “names: ${name[0]}, ${name[1]}”

14. If statement in bash script


#!/bin/bash

if [ condition ]
then
statement
fi

integer comparison operators

-eq is equal to if [ “$a” –eq “$b” ]


-ne is not equal to if [ “$a” –ne “$b” ]
-gt is greater than if [ “$a” –gt “$b” ]
-ge is greater than or equal to if [ “$a” –ge “$b” ]
-lt is less than if [ “$a” –lt “$b” ]
-le is less than or equal to if [ “$a” –le “$b” ]
< is less than if ((“$a” < “$b”))
<= is less than or equal to if ((“$a” <= “$b”))
> is greater than if ((“$a” > “$b”))
>= is greater than or equal to if ((“$a” >= “$b”))

string comparison operators

= is equal to if [ “$a” = “$b” ]


== is equal to if [ “$a” == “$b” ]
!= is not equal to if [ “$a” != “$b” ]
< is less than, in ASCII alphabet order if [[ “$a” < “$b” ] ]
> is greater than, in ASCII alphabet order If [ [ “$a” > “$b” ]]
-z string is null, zero length If [ -z “$string”]

Page 3 of 6
# comparing the two strings

word=”school”
if [ “$word” = “school”]
then
echo “condition is true”
fi

15. if else statement in bash


if [ condition ]
then
statement
else
statement
fi

16. if, elif, else statement in bash


if [ condition ]
then
statement
elif [ condition ]
then
statement
else
statement
fi

17. Logical AND, OR operator


if [ condition ] && [ condition ] # logical AND
then
statement
fi

if [ condition –a condition ] # logical AND


then
statement
fi

if [[ condition && condition ]] # logical AND


then
statement
fi

Page 4 of 6
if [ condition ] || [ condition ] # logical OR
then
statement
fi

if [ condition -o condition ] # logical OR


then
statement
fi

if [[ condition || condition ]] # logical OR


then
statement
fi

18. while loop in bash scripting


while [ condition ]
do
command 1
command 2
done

# for example the following while loop prints the values up to 10

n=1
while [ $n –le 10 ] OR (( $n <= 10 ))
do
echo “$n”
n=$(( n+1 ))OR (( n++ ))
done

19. for loop in bash scripting

for (( initialization; test condition; loop counter ))


do
command 1
command 2
done

Page 5 of 6
Task 1: Run the following command and explain its purpose. 'lshw'. If the command doesn't work, try to find

the solution.

Task 2: Write a bash script that finds the first 500 prime numbers.

Task 3: Write a bash script that displays the number of cores of your machine.

Task 4: By default, on your system, all cores are online. Online core means that the core is active. Find the

command that tells the status of the core.

Task 5: The system's cores are numbered from 0 to n. Find which is the default core of your system.

Task 6: Write a bash script using a for loop that disables or offline all the cores except the default core.

Task 7: Write a bash script using a for loop that enables or online all the cores except the default core.

Task 8: Write a bash script that reads the names of five files and creates five text files in the current working

directory.

Task 9: Write a bash script that deletes the files (created in task 7).

Task 10: Write a bash script that reads the integer input' n' from the keyboard. It displays the current date

and time system 'n' times with a delay of 2 seconds.

Task 11: Write a bash script that reads the two strings from the keyboard and compares them for equality.

Page 6 of 6

You might also like