shell
shell
Shell scripting
• Shell scripting is a method for automating tasks and executing a series
of commands in a Unix/Linux shell (or Windows command prompt). It
entails writing scripts using a shell language, such as Bash (Bourne
Again SHell) in Linux or PowerShell in Windows.
• These scripts are useful for automating repetitive tasks, managing
system operations, and processing data.
shell
• Shell:
• A command -line interface that allows users to interact with the
operating system.
• Common shells include Bash, Zsh, Ksh, and Fish on Linux/Unix
systems, and PowerShell on Windows.
Script:
• A plain text file containing a series of commands that the shell can
execute. These commands are written in a scripting language (e.g.,
Bash, PowerShell).
Shebang (#!)
• The shebang at the beginning of a shell script defines the interpreter
to use for executing the script.
• Example:
#!/bin/bash
• Tells the system to use Bash to interpret the script.
Basic Structure of a Shell Script:
• Shebang: Defines the interpreter.
• Commands: A series of shell commands.
• Variables: Used to store values (e.g., strings, numbers).
• Control Flow: Loops and conditionals.
• Comments: Lines starting with # are comments.
Basics of Scripting:
• echo
• Displays a message or value of the variable
• Eg: echo “hello world”
Basics of Scripting:
• Variables:
• Syntax:
• variablename=value
• Example: age=20
• Variables are accessible using $VariableName.
• Any variable written inside or outside a function by default is a global
variable.
• To make a variable local, use the keyword local.
Functions
• A function is a block of code that performs some tasks and can be
called multiple times for performing tasks.
#!/bin/bash
# It is a function
myFunction () {
echo Hello World from Test
}
# Function call
myFunction
Operators:
Example:
Name="Jovan"
if [ "$Name" = "Jovan" ]; then
echo "Hello Jovan."
fi
Case Statement in Shell Scripting:
• The case-esac statement works similarly to the switch statement in programming. It is useful
when there are multiple conditions to check, as using multiple if statements can become
complex.
case $var in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
Esac
Example:
Name="Shivanika"
case "$Name" in
"Anika") echo "Profession: Software Engineer" ;;
"Arnav") echo "Profession: Doctor" ;;
"Shivanika") echo "Profession: Lawyer" ;;
esac
Writing Bash Scripts:
• To write a Bash script, follow these steps:
• Create a file with the .sh extension:
• Use the gedit text editor to create and write the file.
• Syntax:
gedit scriptname.sh
• Add the Bash shebang:
• The first line of the script should be:
#!/bin/bash
Execution Permission and Running a
Bash Script:
• Grant Execution Permission:
• To make the script executable, use the following command:
chmod +x scriptname.sh
• Run the Script:
./scriptname.sh
File test operators
example
#!/bin/bash
if [ -d "/tmp" ]; then
echo "/tmp exists"
else
echo "/tmp does not exist"
fi
Shell script to find largest of two
numbers
#!/bin/bash
# Read two numbers from the user
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
# Compare the numbers and find the largest
if [ $num1 -gt $num2 ]; then
echo "The largest number is: $num1"
elif [ $num2 -gt $num1 ]; then
echo "The largest number is: $num2"
else
echo "Both numbers are equal."
fi
Shell script to find sum of digits of
two numbers
#!/bin/bash
# Read a number from the user
echo "Enter a number:"
read num
sum=0
# Loop to calculate the sum of digits
while [ $num -gt 0 ]; do
digit=$((num % 10)) # Get the last digit
sum=$((sum + digit)) # Add the digit to sum
num=$((num / 10)) # Remove the last digit
done
echo "Sum of digits: $sum"
To find the reverse of a number
#!/bin/bash
# Read a number from the user
echo "Enter a number:"
read num
reverse=0
# Loop to calculate the reverse of the number
while [ $num -gt 0 ]; do
digit=$((num % 10)) # Get the last digit
reverse=$((reverse * 10 + digit)) # Append the digit to reverse
num=$((num / 10)) # Remove the last digit
done
echo "Reverse of the number: $reverse"