Lab 4
Lab 4
Objective:
What is Shell?
Syntax of Shell Scripting.
What is a Shell?
A shell is a command-line interface that allows users to interact with the operating system by
typing commands. It acts as an intermediary between the user and the kernel of the operating
system. The shell interprets user commands and executes them, facilitating tasks such as file
management, program execution, and system configuration.
Shell scripting is a way to automate repetitive tasks and manage system operations by writing
scripts for the shell (command-line interface). Shell scripts are sequences of commands
written in a text file and executed by the shell.
Shell scripting is case-sensitive, meaning that variable names, commands, and keywords must
be used consistently in their exact case. It also requires precise spacing around syntax
elements, such as brackets in conditional statements.
For example, in an if condition, the spaces around the brackets are crucial:
“if [ condition ]; then ... fi” is correct, whereas “if[condition];then ... fi” will produce an
error.
Unlike Java, where the structure is more rigid, shell scripting demands careful attention to
spacing and formatting to ensure that commands and variables are interpreted correctly.
These are some fundamental points to remember when writing shell scripts:
Important Points:
1. Comments:
o Use # to write comments in your script.
o Comments are useful for explaining what the script does, making it easier to
understand and maintain.
o Example:
bash
Copy code
# This is a comment explaining the next line
echo "Hello, World!"
2. Command Separation:
A semicolon (;) separates multiple commands on the same line.
This allows you to execute multiple commands in a single line within your script.
Example:
bash
Copy code
echo "Hello"; echo "World"
The above example will run both commands separately, printing "Hello" and
"World".
3. Shebang (#!/bin/bash):
The line #!/bin/bash is called a shebang and is used at the beginning of the
script to specify which interpreter should execute the script.
While it's not always required (like in Kali Linux), using the shebang ensures that
the script runs with the specified shell (e.g., Bash) across different Linux
distributions.
Example:
bash
Copy code
#!/bin/bash
echo "This script runs with Bash!"
bash
Copy code
echo "Welcome to shell scripting!"
1. Variable Naming:
o Variable names must begin with an alphanumeric character (a-z, A-
Z) or an underscore (_), followed by one or more alphanumeric
characters.
o Example: name1, _myVariable
3. Case Sensitivity:
o Variables are case-sensitive, so VAR and var are considered different
variables.
o Example: NAME="Alice" is not the same as name="Alice"
4. Defining Null Variables:
You can create a variable without a value by using either of the following:
bash
Copy code
var=
var=""
Avoid using special characters like ?, *, and others in variable names as they may
have special meanings in the shell.
Here's a simple shell script demonstrating user-defined variables, environment variables, and
positional variables:
It prints Hello World in my case
Lab activities:
Activity 1.1:
Write a shell script that takes two numbers as input from the user
and prints the sum of those numbers.
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
Activity 1.2:
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
----------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
Syntax
if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi //ending of if.
Example
The above example can also be written using the if...else statement as
follows:
a=10
b=20
if [ $a -eq $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
If-elif… fi Statements:
Syntax
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ] // (elif means else-if as same in Java)
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi //ending of if
Relational Operators
Bourne Shell supports the following relational operators that are specific
to numeric values. These operators do not work for string values unless
their value is numeric.
Checks if the value of two operands are equal or not; if yes, then
-eq [ $a -eq $b ] is not true.
the condition becomes true.
Checks if the value of two operands are equal or not; if values are
-ne [ $a -ne $b ] is true.
not equal, then the condition becomes true.
Checks if the value of left operand is less than the value of right
-lt [ $a -lt $b ] is true.
operand; if yes, then the condition becomes true.
Activity 1.3:
Create a shell script that takes three numbers as input and displays
the largest number.
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
Activity 1.4:
Write a shell script that takes input from user and check if a number is even or
odd.
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
Activity 1.5:
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
---------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
-----------------------------------------