0% found this document useful (0 votes)
0 views

shell

Shell scripting is a method for automating tasks in Unix/Linux or Windows command prompts using scripts written in shell languages like Bash or PowerShell. It involves defining commands, variables, control flow, and functions, with a shebang indicating the interpreter. Key concepts include decision-making statements like if-else and case, as well as file operations and script execution permissions.

Uploaded by

kal1das4n10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

shell

Shell scripting is a method for automating tasks in Unix/Linux or Windows command prompts using scripts written in shell languages like Bash or PowerShell. It involves defining commands, variables, control flow, and functions, with a shebang indicating the interpreter. Key concepts include decision-making statements like if-else and case, as well as file operations and script execution permissions.

Uploaded by

kal1das4n10
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

SHELL SCRIPTING

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:

• The operators are:


Arithmetic Operators
Relational Operators
Boolean Operators
String Operators
File Test Operators
Decision Making in Shell Scripting:
• Two types of decision-making statements are used within shell scripting:
• if-else statement:
• The if-else statement is a conditional statement that can be used to execute
two different codes based on whether the given condition is satisfied or not
• Different types are:
if-fi
if-else-fi
if-elif-else-fi
nested if-else
Syntax for a simple if:
if [ expression ]; then
statements
fi

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

echo "Hello, $USER!" # Display a greeting message

echo "Files in the current directory:"


ls

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"

You might also like