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

Topic 7 Linux Unit 3

Uploaded by

Disguised Minion
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Topic 7 Linux Unit 3

Uploaded by

Disguised Minion
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Topic: 7 Shell Scripting

a. What is Shell Scripting?


Shell is an interface of the operating system. It accepts commands from users and interprets
them to the operating system.

Steps to Create, Modify, and Run a Shell Script

1. Create an empty shell script file:

touch script.sh

o Explanation: touch script.sh creates an empty file named script.sh. This file will
contain our shell script commands.
2. Give executable permission to the shell script file:

chmod +x script.sh

o Explanation: chmod +x script.sh makes the file executable. Without this


permission, the script can’t be run directly. Here, +x adds execute permissions for
the user.
3. Edit the script using a text editor (e.g., nano):

nano script.sh

oExplanation: nano script.sh opens the script.sh file in the nano text editor,
allowing you to write or edit the script’s content.
4. Add content to the script: Inside the editor, add the following line:

#!/bin/bash

# Addition of two numbers

# Prompt the user for two numbers

echo "Enter the first number:"

read num1

echo "Enter the second number:"


read num2

# Calculate the sum

sum=$((num1 + num2))

# Display the result

echo "The sum of $num1 and $num2 is: $sum"

Explanation of the code:

 #!/bin/bash: This tells the system to use the bash shell to execute the script.
 echo: Prints messages to the terminal.
 read: Reads user input and stores it in variables (num1 and num2).
 $((...)): Performs arithmetic operations; in this case, it adds num1 and num2.
 echo: Prints the result of the addition.

5. Save and exit the nano editor:


o To save: Press Ctrl + O, then hit Enter.
o To exit: Press Ctrl + X.
6. Run the shell script:

./script.sh

o Explanation: ./script.sh executes the script. The ./ before the filename tells the
shell to look for the file in the current directory and execute it.

Shell variables
In shell scripting, variables are used to store data values which can be accessed and modified
throughout the script.

1. Defining Variables

 A variable is created simply by assigning a value to a name, with no spaces around the =
sign.
 Example:
name="Alice"

age=25

2. Accessing Variable Values

 Use the $ symbol followed by the variable name to retrieve its value.
 Example:

echo "Name: $name"

echo "Age: $age"

3. Types of Shell Variables

 Local Variables: These are defined within a script or function and are accessible only
within that scope.
 Environment Variables: These are available to any child processes created by the shell.
They are typically uppercase by convention (e.g., PATH, HOME).
 Positional Parameters: Special variables for accessing command-line arguments passed
to the script (e.g., $1, $2, etc.).
 Special Variables: Variables like $? (last command's exit status), $$ (current script's
process ID), $# (number of arguments passed), etc.

4. Positional Parameters

 Positional parameters are used to pass arguments to a shell script.

Example:

echo "First argument: $1"

echo "Second argument: $2"

5. Assigning Values

 Variables can be assigned directly, or their values can be derived from commands.
 Example:

current_date=$(date) # Assigns the output of `date` to `current_date`

echo "Today is: $current_date"

6. Add two variables directly using $(...) or by using external program expr

1. Add two variables without using expr in shell script


#shell program to add two variables

var1=10

var2=20

sum=$(($var1 + $var2))

echo $sum

Output:

30

2. Add two variables using expr in shell script

#shell program to add two variables

var1=10

var2=20

sum=`expr $var1 + $var2`

echo $sum

Output:

30

7. Exit Status of a Command ($?)


$? captures the exit status of the last command (0 for success, non-zero for failure).

Example:
echo "Exit status: $?"
Sleep and Wait
In shell scripting, the commands sleep and wait are used for controlling the timing and execution
flow of scripts.

1. sleep Command

 The sleep command pauses the execution of the script for a specified number of
seconds.
 It is often used to delay operations or give time for other processes to complete.
 You can specify the time in seconds (e.g., sleep 5), or with a suffix for minutes (m),
hours (h), or days (d).

Example:

echo "Starting..."

sleep 3 # Pauses execution for 3 seconds

echo "Resumed after 3 seconds."

Usage Scenarios for sleep:

 To add delays between tasks or actions within a script.


 Useful in retry mechanisms when waiting for resources to become available.

2. wait Command

 The wait command pauses the execution of the script until specified background
processes finish.
 By default, wait will wait for all background jobs to complete if no job ID or PID is
provided.
 It’s commonly used to synchronize processes within a script, ensuring certain processes
complete before moving on.

Example of wait without arguments:

# Run two background processes

sleep 5 &

pid1=$!

sleep 10 &
pid2=$!

echo "Waiting for processes to complete..."

wait $pid1 $pid2 # Waits for both background jobs to finish

echo "Both processes completed."

Usage Scenarios for wait:

 Waiting for a group of background jobs to complete before proceeding.


 Ensuring that dependent tasks run only after specific background tasks have finished.

Script Termination

 Termination with exit: The exit command terminates the script and optionally returns
a status code. This is often used to end the script if a critical error occurs or to indicate
successful completion.
 Exit Codes: Exit codes help other scripts or commands understand whether a script
executed successfully (exit 0) or encountered an error (exit with a non-zero code).

Example of Using exit:

echo "Starting the script..."

# Check if a file exists

if [ ! -f "/path/to/file" ]; then

echo "File not found! Exiting..."

exit 1 # Non-zero exit code indicates an error

fi

echo "File found. Continuing script..."

# Additional script commands

exit 0 # Indicates successful script completion


b. Taking Decision

In shell scripting, taking decisions typically involves using conditional statements to perform
different actions based on specific conditions. The main structures for decision-making in shell
scripting are if, if-else, and case statements.

1. if Statement

The if statement allows you to execute a block of code only if a specified condition is true.

Syntax:

if [ condition ]; then

# code to execute if condition is true

fi

Example:

number=5

if [ $number -gt 0 ]; then

echo "The number is positive."

fi

2. if-else Statement

The if-else statement is used when you want to perform one action if a condition is true and a
different action if it is false.

Syntax:

if [ condition ]; then

# code to execute if condition is true

else

# code to execute if condition is false

fi
Example:

number=-3

if [ $number -gt 0 ]; then

echo "The number is positive."

else

echo "The number is not positive."

fi

3. if-elif-else Statement

The if-elif-else statement allows you to check multiple conditions in sequence, executing
different code blocks based on the first condition that evaluates to true.

Syntax:

if [ condition1 ]; then

# code to execute if condition1 is true

elif [ condition2 ]; then

# code to execute if condition2 is true

else

# code to execute if none of the above conditions are true

fi
Example:

number=0

if [ $number -gt 0 ]; then

echo "The number is positive."

elif [ $number -lt 0 ]; then


echo "The number is negative."

else

echo "The number is zero."

fi

4. case Statement

The case statement is useful for handling multiple possible values for a single variable. It's often
used as an alternative to if-elif-else when there are many conditions to check.

Note: In shell scripting, esac is the closing keyword for a case statement. It’s simply “case”
spelled backward, and it signifies the end of the case block.

Syntax:

case variable in

pattern1)

# code to execute if variable matches pattern1

;;

pattern2)

# code to execute if variable matches pattern2

;;

*)

# code to execute if no pattern matches

;;

esac

Conditional Operators:

 String Comparisons:
o =: Equal
o !=: Not equal
o -z: String is empty
o -n: String is not empty
 Numeric Comparisons:
o -eq: Equal
o -ne: Not equal
o -lt: Less than
o -gt: Greater than
o -le: Less than or equal
o -ge: Greater than or equal
 File Tests:
o -e file: File exists
o -d file: File is a directory
o -f file: File is a regular file
o -s file: File is not empty
c. Loop Control Structure

In shell scripting, loop control structures allow you to repeat a set of commands multiple times.
The most common loop structures in shell scripting are for, while, and until. Each has its own
use case, depending on the condition you want to check for repeated execution.

1. for Loop

The for loop is used to iterate over a list of items, executing a set of commands for each item.

Syntax:

for variable in list

do

# Commands to execute for each item in list

done

Example:

# Loop through a list of items

for fruit in apple banana cherry

do

echo "I like $fruit"

done

Use Case for for Loop:

 Iterating over files in a directory


 Processing each item in a list of strings or numbers

2. while Loop

The while loop executes a block of commands as long as a specified condition is true.

Syntax:

while [ condition ]

do
# Commands to execute while condition is true

done

Example:

# Print numbers from 1 to 5

count=1

while [ $count -le 5 ]

do

echo "Count is: $count"

count=$((count + 1))

done

Use Case for while Loop:

 Repeating tasks while a condition holds true, like waiting for a process to complete
 Counting or generating a sequence until a specific limit is reached

3. until Loop

The until loop is similar to the while loop but with an opposite condition; it runs until the
specified condition becomes true.

Syntax:

until [ condition ]

do

# Commands to execute until condition becomes true

done

Example:

# Print numbers from 1 to 5

count=1

until [ $count -gt 5 ]


do

echo "Count is: $count"

count=$((count + 1))

done

Use Case for until Loop:

 Performing tasks until a specific condition is met, like waiting for a service to start
 Generally used when you want to run the loop as long as a condition is false

4. Loop Control with break and continue

 break: Exits the loop completely, stopping any further iteration.


 continue: Skips the current iteration and proceeds to the next one.

Example of break and continue:

# Loop through numbers 1 to 5

for i in 1 2 3 4 5

do

if [ $i -eq 3 ]; then

echo "Skipping $i"

continue # Skips this iteration

fi

if [ $i -eq 4 ]; then

echo "Breaking at $i"

break # Exits the loop

fi

echo "Number is: $i"

done
Output:

Number is: 1

Number is: 2

Skipping 3

Breaking at 4

5. Nested Loops

You can place one loop inside another, known as a nested loop. This is helpful for processing
multi-dimensional data, like tables or matrices.

Example:

for i in 1 2

do

for j in A B

do

echo "i=$i, j=$j"

done

done

Summary of Loop Control Structures

Loop Type Purpose

for Iterates over a list of items

while Repeats as long as a condition is true

until Repeats until a condition becomes true

break Exits a loop immediately

continue Skips the current iteration and moves to the next


d. Shell metacharacters

Shell metacharacters are special characters in shell scripting that have a unique function, often
used to control input, output, and other aspects of how commands are executed. Here's a
breakdown of some commonly used shell metacharacters:

1. Wildcards

 *: Matches zero or more characters in filenames.

ls *.txt # Lists all files with the .txt extension

 ?: Matches exactly one character in filenames.

ls file?.txt # Matches file1.txt, file2.txt, etc.

 []: Matches any one of the enclosed characters.

ls file[1-3].txt # Matches file1.txt, file2.txt, file3.txt

2. Redirection Operators

 >: Redirects standard output to a file, overwriting the file if it exists.

echo "Hello" > output.txt # Writes "Hello" to output.txt

 >>: Redirects standard output to a file, appending if the file already exists.

echo "Hello" >> output.txt # Appends "Hello" to output.txt

 <: Redirects input from a file.

wc -l < input.txt # Counts lines in input.txt

 2>: Redirects standard error to a file.

ls /nonexistent 2> error.log # Writes error message to error.log

3. Pipes

 |: Sends the output of one command to another command as input.

ls | grep "pattern" # Lists files and filters names containing "pattern"


4. Logical Operators

 &&: Executes the next command only if the previous command succeeds.

mkdir test && cd test # Creates and changes directory to "test" if mkdir succeeds

 ||: Executes the next command only if the previous command fails.

cd nonexistent || echo "Directory does not exist." # Prints message if directory change
fails

5. Command Substitution

 `command` or $(command): Substitutes the output of a command.

current_date=$(date) # Stores current date in variable current_date

6. Quoting

 ' ' (Single Quotes): Prevents interpretation of special characters.

echo 'This $is a literal string' # Outputs: This $is a literal string

 " " (Double Quotes): Preserves spaces and special characters but allows variable
expansion.

name="World"
echo "Hello $name" # Outputs: Hello World

 \ (Backslash): Escapes a single character.

echo "Hello \"World\"" # Outputs: Hello "World"

7. Background Execution

 &: Runs a command in the background.

sleep 10 & # Runs sleep in the background, allowing further commands to execute
immediately

8. Grouping Commands

 ( ): Runs commands in a subshell.

(cd /tmp && ls) # Changes directory to /tmp and lists contents in a subshell
 { }: Groups commands in the current shell.

{ echo "Hello"; echo "World"; } # Groups commands without creating a subshell

9. Conditional Expressions

 [ ]: Used for test conditions.

if [ -f "file.txt" ]; then echo "File exists"; fi

 [[ ]]: Extended test expression for conditional checks.

if [[ "$var" == "value" ]]; then echo "Match"; fi

10. Process Substitution

 <() and >(): Substitutes the output of a command as a file input.

diff <(ls dir1) <(ls dir2) # Compares contents of two directories

Summary Table

Metacharacter Description
*, ?, [] Wildcards for matching filenames
>, >>, < Redirect output/input
` `
`command` or $(command) Command substitution
'' Single quotes, prevent variable expansion
"" Double quotes, allow variable expansion
\ Escape character
& Run command in background
( ), { } Group commands in subshell or current shell
[ ], [[ ]] Conditional expressions
<( ), >( ) Process substitution

You might also like