0% found this document useful (0 votes)
13 views131 pages

Usp Mod 4

Uploaded by

r8978428
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)
13 views131 pages

Usp Mod 4

Uploaded by

r8978428
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/ 131

UNIX AND

SHELL
PROGRAMMIN
G
MODULE 4

U N I X C O N C EP TS &
AP P L I C ATI O N S 4 T H ED I TI O N
BY S U M I TAB H A D AS
The Shell’s Interpretive Cycle
The shell in UNIX acts as an interpreter between the user and the
operating system. Its role is to take commands from the user,
interpret them, and pass them to the operating system for
execution. Once the commands are executed, it displays the output to
the user. This cycle of interaction is known as the shell's interpretive
cycle.
The Shell’s Interpretive Cycle
Steps in the Shell's Interpretive Cycle
Prompting: The shell displays a prompt (e.g., $) to indicate that it is
ready to accept a command.
Reading: The user enters a command, and the shell reads the input.
Parsing: The shell breaks the command into parts (tokens) such as
the command name, options, and arguments.
The Shell’s Interpretive Cycle
Steps in the Shell's Interpretive Cycle
Substitution/Expansion: It processes variables ($VAR), wildcards (*), and
command substitutions (e.g., $(date)).
Command Lookup:
The shell identifies whether the command is:
A shell built-in (e.g., cd).
An executable file located in directories specified by $PATH.
The Shell’s Interpretive Cycle
Steps in the Shell's Interpretive Cycle
Execution: For external commands: The shell creates a new process using
fork(). The child process replaces itself with the command using exec(). For
built-in commands, the shell directly executes them.
Output: The shell receives and displays the output (or errors) of the executed
command.
Return to Prompt: After command execution, the shell returns to the prompt,
ready for the next command.
The Shell’s Interpretive Cycle
Wildcards:
Wildcards are special characters used in UNIX shell commands to
represent multiple files or directories without typing their names
explicitly. They are part of the shell’s substitution/expansion step
during the interpretive cycle.
The Shell’s Interpretive Cycle
Wildcards: The shell interprets the wildcard (*) and matches it with
filenames in the current directory that meet the pattern.
For example:
*.txt matches all files ending with .txt.
* matches all files.
a?c matches files like abc, aac (where ? matches any single character)
The Shell’s Interpretive Cycle
Common Wildcards in UNIX
Wildcard Meaning Example Matches
* Matches zero or more characters *.txt file.txt,
notes.txt, a.txt
? Matches exactly one character file?.txt file1.txt,

file2.txt
The Shell’s Interpretive Cycle
Common Wildcards in UNIX
Wildcard Meaning Example Matches
[] Matches any one character inside the brackets file[12].txt
file1.txt, file2.txt
[! ] Matches any character not in the brackets file[!1].txt
file2.txt, but not file1.txt
{} Matches a list of patterns separated by commas file{1,2}.txt
file1.txt, file2.txt
The Shell’s Interpretive Cycle
Example: Using Wildcards
Command: ls *.c
Explanation: Lists all files with the .c extension in the current directory.
Expansion: *.c → main.c, utils.c, test.c.
Execution: The shell runs: ls main.c utils.c test.c.
The Shell’s Interpretive Cycle
Example: Using Wildcards
Command: rm file[1-3].txt
Explanation: Deletes files named file1.txt, file2.txt, and file3.txt.
Command: mv data_?.csv backup/
Explanation: Moves files like data_1.csv and data_2.csv (but not data_10.csv)
to the backup/ directory.
The Shell’s Interpretive Cycle
Escaping:
In UNIX, escaping is the process of preventing the shell from
interpreting special characters (like *, ?, $, etc.) in a command. It
tells the shell to treat these characters literally, rather than using
their special meanings (e.g., as wildcards or variable expansions).
The Shell’s Interpretive Cycle
Why Escaping is Needed
The shell interprets special characters like:
* (wildcard for matching multiple files),
$ (variable expansion),
\n (newline), and many others.
If you want the shell to treat these characters as regular text, you use
escaping.
The Shell’s Interpretive Cycle
Ways to Escape Characters
Backslash (\): Place a backslash before the special character to
escape it.
Example: echo Hello\*World
Output: Hello*World
(The * is not treated as a wildcard.)
The Shell’s Interpretive Cycle
Ways to Escape Characters
Single Quotes ('): Enclose the string in single quotes to escape all
special characters.
Example: echo 'Hello $USER'
Output: Hello $USER
(The $USER is not expanded as a variable.)
The Shell’s Interpretive Cycle
Ways to Escape Characters
Double Quotes ("): Enclose the string in double quotes to escape most
special characters, except $, \, and `.
Example: echo "Hello \$USER"
Output: Hello $USER
(Here, $USER is escaped, but double quotes allow expansion by
default.)
The Shell’s Interpretive Cycle
Ways to Escape Characters
Escape Command (\): Use \ as a general escape character for
commands.
Example: touch file\ name.txt
Creates a file named file name.txt (the space is escaped).
The Shell’s Interpretive Cycle
Quoting:
Quoting in the UNIX shell is a way to control how the shell interprets
special characters (such as $, *, ?, and |) within a command. By
enclosing text in quotes, you can either allow or suppress the shell's
interpretation of these characters.
The Shell’s Interpretive Cycle
Types of Quoting
1. Single Quotes (')
Purpose: Preserves the literal meaning of everything inside the quotes. All special characters are
treated as plain text.
Usage: Protects the string from shell interpretation, including variables ($) and commands ($( )).
Example: echo 'Hello $USER'
Output: Hello $USER
(The $USER variable is not expanded.)
The Shell’s Interpretive Cycle
2. Double Quotes (")
Purpose: Allows some interpretation while preserving others. Special characters like $
(variable expansion), ` (command substitution), and \ (escape character) retain their meaning.
Wildcards (*), pipes (|), and others are treated as plain text.
Usage: Used when you want to expand variables or execute commands but treat other special
characters literally.
Example: echo "Hello $USER"
Output: Hello your_username (The $USER variable is expanded.)
echo "Today is $(date)"
Output: Today is Mon Nov 19 12:34:56 UTC 2024 (The $(date) command is executed.)
The Shell’s Interpretive Cycle
3. Backslash (\)
Purpose: Escapes a single character. Prevents the shell from interpreting the
following character.
Usage: Useful for escaping one or two characters instead of quoting an entire string.
Example: echo Hello\*World
Output: Hello*World
(The * is treated as plain text.)
The Shell’s Interpretive Cycle
Three standard files:
In UNIX, every process has three default file descriptors (or standard files) connected to
the terminal by default,
Standard Input (stdin):
File Descriptor: 0
Purpose: Accepts input data (usually from the keyboard).
Example: cat
The cat command waits for user input via stdin.
The Shell’s Interpretive Cycle
Standard Output (stdout):
File Descriptor: 1
Purpose: Displays output data (usually to the terminal screen).
Example: echo "Hello, World!"
The text Hello, World! is displayed on the terminal via stdout.
The Shell’s Interpretive Cycle
Standard Error (stderr):
File Descriptor: 2
Purpose: Displays error messages (usually to the terminal screen).
Example: ls nonexistent_file
Error message: ls: cannot access 'nonexistent_file': No such file or
directory is sent to stderr.
The Shell’s Interpretive Cycle
Redirection:
Redirection allows you to change where stdin, stdout, or stderr are
read from or written to. This is often used to store outputs or handle
errors in files.
The Shell’s Interpretive Cycle
Types of Redirection
Redirecting stdout: Syntax: command > file
Sends the standard output to a file, overwriting its contents.
Example: echo "Hello, World!" > output.txt
Writes Hello, World! to the file output.txt.
Append to the file: echo "New Line" >> output.txt
Adds New Line to the file without overwriting.
The Shell’s Interpretive Cycle
Redirecting stderr:
Syntax: command 2> file
Sends error messages to a file.
Example: ls nonexistent_file 2> errors.txt
Writes the error message to errors.txt.
The Shell’s Interpretive Cycle
Redirecting Both stdout and stderr:
Syntax: command > file 2>&1 or command &> file
Sends both outputs and errors to the same file.
Example:
ls existing_file nonexistent_file > output_and_errors.txt 2>&1
Writes both output and error messages to output_and_errors.txt.
The Shell’s Interpretive Cycle
Redirecting stdin:
Syntax: command < file
Reads input from a file instead of the keyboard.
Example: cat < input.txt
Reads and displays the contents of input.txt.
The Shell’s Interpretive Cycle
A pipe: in Unix is a powerful inter-process communication
mechanism that allows the output of one command to be used as
the input to another command. It enables you to chain multiple
commands together, creating a pipeline for processing data step by
step.
The symbol for a pipe is the vertical bar (|).
The Shell’s Interpretive Cycle
How a Pipe Works
A pipe connects the standard output (stdout) of one process to the
standard input (stdin) of another.
It allows commands to work together in a streamlined and efficient
manner without needing intermediate files.
The Shell’s Interpretive Cycle
Basic Syntax
command1 | command2 | command3
command1: Generates the output.
command2: Takes the output of command1 as input and processes it.
command3: Further processes the output of command2.
The Shell’s Interpretive Cycle
Count the Number of Lines in a File
cat file.txt | wc -l
cat file.txt: Displays the content of the file.
wc -l: Counts the number of lines.
The Shell’s Interpretive Cycle
Sort and Display Unique Words in a File
cat file.txt | tr ' ' '\n' | sort | uniq
tr ' ' '\n': Converts spaces to newlines to separate words.
sort: Sorts the words alphabetically.
uniq: Removes duplicate entries.
The Shell’s Interpretive Cycle
Common Commands Used with Pipes
grep: Search for patterns in text.
sort: Sort lines of text.
uniq: Remove duplicate lines.
wc: Count words, lines, and characters.
awk: Process and analyze text data.
sed: Perform text transformations.
The Shell’s Interpretive Cycle
The tee command: in Unix is used to read from the standard
input (stdin) and simultaneously write the output to both standard
output (stdout) and one or more files. It is especially useful for
logging or saving intermediate results in a pipeline without
interrupting the flow.
The Shell’s Interpretive Cycle
Basic Syntax
command | tee [options] file_name
command: Any command whose output you want to process.
file_name: The file where the output will be saved.
The Shell’s Interpretive Cycle
How tee Works
Input: Takes the output of a command (or any data stream).
Output:
Displays it on the screen (stdout).
Saves it to the specified file(s).
The Shell’s Interpretive Cycle
Save and Display Output Simultaneously
ls -l | tee output.txt
Action: Lists files in long format.
Result:
The output is displayed on the terminal.
The same output is saved to output.txt.
The Shell’s Interpretive Cycle
Append to a File
To append the output to an existing file instead of overwriting it:
ls -l | tee -a output.txt
-a option: Appends to output.txt instead of overwriting.
The Shell’s Interpretive Cycle
Command substitution :
in Unix is a feature that allows the output of a command to be used as
an input to another command. It is a powerful tool for automating
tasks and making scripts more dynamic by using the result of one
command directly in another.
The Shell’s Interpretive Cycle
Syntax of Command Substitution
There are two common syntaxes for command substitution:
Backticks (``):
command_output=`command`
$(...) (Preferred Modern Style):
command_output=$(command)
The Shell’s Interpretive Cycle
Examples of Command Substitution
1. Assign Command Output to a Variable
current_date=$(date)
echo "Today's date is: $current_date"
date: Generates the current date and time.
Result: Stores the output in the variable current_date and prints it.
The Shell’s Interpretive Cycle
Use Command Output as an Argument
echo "Number of files in current directory: $(ls | wc -l)"
ls: Lists files in the current directory.
wc -l: Counts the number of lines (files).
The Shell’s Interpretive Cycle
Embed Command Output in a String
echo "The current working directory is $(pwd)"
pwd: Prints the current working directory.
The Shell’s Interpretive Cycle
Nested Command Substitution
echo "Largest file: $(ls -S | head -1)"
ls -S: Lists files sorted by size (largest first).
head -1: Retrieves the first file from the sorted list.
The Shell’s Interpretive Cycle
Run Command Inside a Script
#!/bin/bash
username=$(whoami)
echo "Hello, $username! Welcome to the system."
whoami: Retrieves the current username.
Shell Programming
Ordinary Variables
These are variables created and used within the shell or a shell script.
They are:
Local to the current shell session or script.
Not accessible by child processes or other shells.
Shell Programming
Key Features
Ordinary variables are used to store temporary data.
They exist only during the execution of the current shell or script.
They do not affect the environment of other shells or programs.
Shell Programming
Syntax :
variable_name=value
Accessing an ordinary variable:
Use the $ symbol.
echo $variable_name
Shell Programming
Examples:
name="Zaheed"
echo $name
Output: Zaheed
The variable name is an ordinary variable, valid only in the current
shell.
Shell Programming
Environment Variables
Environment variables are a type of variable that:
Are global and can be accessed by child processes or subshells.
Define system-wide or shell-wide configurations.
Shell Programming
Key Features
Used to configure the behavior of the shell and system programs.
Passed from the parent process to child processes.
Examples include PATH, HOME, USER, SHELL, etc.
Shell Programming
Syntax
To create an environment variable:
export variable_name=value
Examples:
export name="Zaheed"
echo $name # Output: Zaheed
The export command makes the variable available to child processes.
Shell Programming
Feature Ordinary Variables Environment Variables

Global, accessible to child


Scope Local to the shell or script.
processes.

Temporary or script-specific Configuring programs or


Usage
data. shells.
export
Creation Syntax variable_name=value
variable_name=value
Not inherited by child
Inheritance Inherited by child processes.
processes.
Shell Programming
Ordinary Variable :
#!/bin/bash
name="Zaheed"
echo "Inside the script: $name"
# Run a child shell
bash -c 'echo "Child shell: $name" ‘
Shell Programming
Environment Variable
#!/bin/bash
export name="Zaheed"
echo "Inside the script: $name"
# Run a child shell
bash -c 'echo "Child shell: $name"'
Shell Programming
The read command:
In shell programming is used to take input from the user. It allows
you to read a line of text from standard input (the keyboard by
default) and assign it to one or more variables.
Shell Programming
Syntax
read [options] [variable_name...]
variable_name: The variable(s) where the input will be stored.
If no variable is specified, the input is stored in the special variable
REPLY.
Shell Programming
Basic Usage Output:
#!/bin/bash Enter your name:
echo "Enter your name:" Zaheed
read name Hello, Zaheed!
echo "Hello, $name!"
Shell Programming
Reading Multiple Inputs
#!/bin/bash
echo "Enter your first and last name:"
read first_name last_name
echo "First Name: $first_name"
echo "Last Name: $last_name"
Shell Programming
Output:
Enter your first and last name:
Zaheed Hussain
First Name: Zaheed
Last Name: Hussain
Shell Programming
Using the Default REPLY Output:
Variable
Enter your favorite color:
#!/bin/bash
Blue
echo "Enter your favorite color:"
Your favorite color is Blue
read
echo "Your favorite color is
$REPLY"
Shell Programming
Prompting on the Same Line Output:
#!/bin/bash Enter your age: 25
read -p "Enter your age: " age You are 25 years old.
echo "You are $age years old."
Shell Programming
Reading Hidden Input (Password) Output:
#!/bin/bash Enter your password:
read -sp "Enter your password: " password Password received.
echo
echo "Password received."
Shell Programming
Reading Input with a Delimiter
Use the -d option to specify a custom delimiter instead of the default
newline:
#!/bin/bash
read -d ":" -p "Enter your name (end with :): " name
echo "Your name is $name"
Shell Programming
Output:

Enter your name (end with :): Zaheed:


Your name is Zaheed
Shell Programming
Limiting Input Length
#!/bin/bash
read -n 4 -p "Enter a 4-digit PIN: " pin
echo
echo "Your PIN is $pin"
Shell Programming
Output:

Enter a 4-digit PIN: 1234


Your PIN is 1234
Shell Programming
Command-line arguments:
In shell programming, allow you to pass inputs to a script when you
run it. These inputs can be used within the script to control its
behavior or provide necessary data dynamically.
Shell Programming
Accessing Command-Line Arguments: Special Variables:
$0: The name of the script.
$1, $2, $3, ...: The first, second, third, etc., command-line arguments.
$#: The number of command-line arguments.
$@: All the arguments as a single string.
$*: All the arguments as separate words.
"$@": Each argument quoted individually.
"$*": All arguments quoted as a single string.
Shell Programming
Example: Basic Usage
Save the following script as arguments.sh:
#!/bin/bash
echo "Script Name: $0"
echo "First Argument: $1"
echo "Second Argument: $2"
echo "All Arguments (\$@): $@"
echo "All Arguments (\$*): $*"
echo "Number of Arguments: $#"
Shell Programming
Run the script with arguments: ./arguments.sh Hello World 123
Output:
Script Name: ./arguments.sh
First Argument: Hello
Second Argument: World
All Arguments ($@): Hello World 123
All Arguments ($*): Hello World 123
Number of Arguments: 3
Shell Programming
In shell programming, the exit command and the exit status are
fundamental concepts for understanding how scripts and commands behave
and communicate their success or failure.
- EXIT Command
- Exit Status
Shell Programming
The exit Command
The exit command is used to terminate a shell script or an interactive shell
session. It optionally takes an exit status code as an argument, which is used
to indicate the outcome of the script or command execution.
Syntax: exit [N]
N is an optional integer value (0-255) that represents the exit status. If
omitted, the script exits with the status of the last executed command.
Shell Programming
Examples:
Exit with success: exit 0
Exit with failure: exit 1
Exit using the status of the last command: ls non_existent_file
exit
Shell Programming
Exit Status of a Command
Every command in shell programming returns an exit status, which is a
numerical code indicating the success or failure of the command. This status
can be accessed using the special variable $?.
Exit Status Values:
0 : The command executed successfully.
Non-zero (1-255): The command encountered an error. Specific numbers may
indicate different errors depending on the command.
Shell Programming
Examples:
Check the exit status of a successful command: ls
echo $?
Output: 0
Check the exit status of a failed command: ls non_existent_file
echo $?
Output: 2
Shell Programming
In shell programming, logical operators are used for conditional execution
and decision-making. They allow combining multiple commands or
conditions in a script.
Shell Programming
Logical AND (&&): The && operator is used to execute a command only if the
preceding command succeeds (returns an exit status of 0).
Syntax: command1 && command2
command2 is executed only if command1 succeeds.
Example:
mkdir mydir && echo "Directory created successfully"
If mkdir mydir succeeds, the message "Directory created successfully" is
displayed.
Shell Programming
Logical OR (||): The || operator is used to execute a command only if the
preceding command fails (returns a non-zero exit status).
Syntax: command1 || command2
command2 is executed only if command1 fails.
Example:
rm non_existent_file || echo "File not found, skipping removal"
If rm fails, the message "File not found, skipping removal" is displayed.
Shell Programming
Combining AND and OR: You can combine && and || to build more complex conditions.
Syntax: command1 && command2 || command3
If command1 succeeds, command2 is executed.
If command1 fails, command3 is executed.
Example:
mkdir mydir && echo "Directory created" || echo "Failed to create directory"
If mkdir succeeds, "Directory created" is displayed.
If mkdir fails, "Failed to create directory" is displayed.
Shell Programming
Logical NOT (!): The ! operator is used to negate the exit status of a command or condition.
Syntax: ! command
If command succeeds, ! makes it return failure (non-zero).
If command fails, ! makes it return success (0).
Example:
if [ ! -d mydir ]; then
echo "Directory does not exist"
fi
If the directory mydir does not exist, the message "Directory does not exist" is displayed.
Shell Programming
In shell programming, the test command is used to evaluate conditional
expressions. It can check file types, compare strings and numbers, and
perform other condition checks. The shortcut for the test command is square
brackets ([ and ]).
Shell Programming
Basic Syntax
Using test: test condition
Using [ ] (shortcut for test): [ condition ]
Both forms work the same way.
Ensure there is a space after [ and before ].
Shell Programming
Examples
1. Checking if a file exists
Using test: if test -f "file.txt"; then
echo "file.txt exists"
fi
Using [ ] (shortcut): if [ -f "file.txt" ]; then
echo "file.txt exists"
fi
Shell Programming
Comparing Numbers
Using test:
if test 5 -eq 5; then
echo "Numbers are equal"
fi
Using [ ]:
if [ 5 -eq 5 ]; then
echo "Numbers are equal"
fi
Shell Programming
String Comparisons
Using test:
if test "$str1" = "$str2"; then
echo "Strings are equal"
fi
Using [ ]:
if [ "$str1" = "$str2" ]; then
echo "Strings are equal"
fi
Shell Programming
Common Test Operators
File Tests
Operator Meaning
-f True if the file exists and is a regular file.
-d True if the file exists and is a directory.
-r True if the file is readable.
-w True if the file is writable.
-x True if the file is executable.
Shell Programming
String Comparisons
Operator Meaning
= True if strings are equal.
!= True if strings are not equal.
-z True if the string is empty.
-n True if the string is not empty.
Shell Programming
Numerical Comparisons
Operator Meaning
-eq Equal to
-ne Not equal to
-lt Less than
-le Less than or equal to
-gt Greater than
-ge Greater than or equal to
Shell Programming
The if statement in shell programming is used to execute commands
conditionally. It evaluates a condition and executes the associated commands
only if the condition is true. The syntax and functionality can vary slightly
based on the shell (e.g., Bash, sh, zsh), but the general structure remains
consistent.
Shell Programming
Syntax of if Statement
Basic Syntax:
if [ condition ]; then
# Commands to execute if condition is true
fi
Shell Programming
Basic if Statement
#!/bin/bash
if [ -f "file.txt" ]; then
echo "file.txt exists."
fi
Checks if file.txt exists as a regular file.
Executes the echo command if true.
Shell Programming
Syntax with else:
if [ condition ]; then
# Commands to execute if condition is true
else
# Commands to execute if condition is false
fi
Shell Programming
Using if-else
#!/bin/bash
if [ -d "my_directory" ]; then
echo "Directory exists."
else
echo "Directory does not exist."
fi
Checks if my_directory exists as a directory.
Executes different commands based on the result.
Shell Programming
Syntax with elif:
if [ condition1 ]; then
# Commands if condition1 is true
elif [ condition2 ]; then
# Commands if condition2 is true
else
# Commands if none of the conditions are true
fi
Shell Programming
Using if-elif-else else

#!/bin/bash echo "Neither file.txt nor


my_directory exists."
if [ -f "file.txt" ]; then
fi
echo "file.txt exists."
Adds a second condition with
elif [ -d "my_directory" ]; then elif. Executes commands based
echo "my_directory exists." on the first matching condition.
Shell Programming
The expr command in shell programming is used to evaluate expressions
and perform arithmetic, string operations, and comparisons. It outputs the
result of the evaluated expression to standard output, making it useful for
simple calculations and text processing within shell scripts.

Syntax : expr EXPRESSION


Shell Programming
Common Use Cases of expr :
Arithmetic Operations
String Operations
Logical Comparisons
Regular Expression Matching
Shell Programming
Arithmetic Operations : The expr command supports basic arithmetic
operators: +, -, *, /, %.
#!/bin/bash
result=$(expr 5 + 3)
echo "5 + 3 = $result"
result=$(expr 10 - 4)
echo "10 - 4 = $result"
Shell Programming
result=$(expr 6 \* 7)
echo "6 * 7 = $result“

result=$(expr 20 / 4)
echo "20 / 4 = $result“

result=$(expr 10 % 3)
echo "10 % 3 = $result"
Shell Programming
String Operations
Finding String Length:
str="Hello"
length=$(expr length "$str")
echo "Length of '$str' is $length"
Shell Programming
Substring Extraction:
str="HelloWorld"
# Extract 5 characters starting from the 4th position
substr=$(expr substr "$str" 4 5)
echo "Substring: $substr"
Shell Programming
Logical Comparisons
Equality and Inequality
if [ $(expr 5 = 5) -eq 1 ]; then
echo "5 equals 5"
fi
if [ $(expr 10 != 5) -eq 1 ]; then
echo "10 is not equal to 5"
fi
Shell Programming
Greater Than, Less Than
num1=8
num2=10
if [ $(expr $num1 \< $num2) -eq 1 ]; then
echo "$num1 is less than $num2"
fi
Shell Programming
Regular Expression Matching
Match a pattern in a string:
str="hello123world"
match=$(expr "$str" : '.*\([0-9][0-9]*\)')
echo "Extracted digits: $match"
Shell Programming
The while loop in shell programming is used to execute a block of code
repeatedly as long as a given condition is true. It is often used when the
number of iterations is not known beforehand and depends on the condition
being evaluated during execution.
Shell Programming
Syntax: while [ condition ]; do
# Commands to execute
done
[ condition ]: The test condition that is checked before each iteration.
do: Indicates the start of the loop body.
done: Indicates the end of the loop body.
Shell Programming
Basic Example
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
count=$((count + 1))
done
Shell Programming
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
Shell Programming
The for loop in shell programming is used to iterate over a list of items,
performing a set of commands for each item in the list. It is versatile and
commonly used when the number of iterations or the list of items is known
beforehand.
Shell Programming
Basic Syntax
for variable in list; do
# Commands to execute
done
variable: The loop variable that takes the value of each item in the list.
list: A space-separated list of items to iterate over.
do: Indicates the start of the loop body.
done: Indicates the end of the loop body.
Shell Programming
Iterating Over a List of Strings
#!/bin/bash
for name in Alice Bob Charlie; do
echo "Hello, $name!"
done
Output:
Hello, Alice!
Hello, Bob!
Hello, Charlie!
Shell Programming
Using a Step Value
Output:
#!/bin/bash
Odd Number: 1
for i in {1..10..2}; do
Odd Number: 3
echo "Odd Number: $i"
Odd Number: 5
done
Odd Number: 7
The syntax {start..end..step}
Odd Number: 9
defines the range and step
value.
Shell Programming
Iterating Over Command Output
#!/bin/bash
for file in $(ls *.txt); do
echo "Processing file: $file"
done
This iterates over the list of .txt files in the current directory.
Shell Programming
Iterating Over Files in a Directory
#!/bin/bash
for file in /path/to/directory/*; do
echo "File: $file"
done
This lists all files and directories in /path/to/directory.
Shell Programming
The case control statement in shell programming is used to execute
different blocks of commands based on the value of a variable or expression.
It is similar to the switch statement in languages like C or Java, but with a
syntax tailored for shell scripts.
Shell Programming
Basic Example
#!/bin/bash
read -p "Enter a number (1-3): " num
case $num in
1)
echo "You chose One."
;;
Shell Programming
2)
echo "You chose Two."
;;
3)
echo "You chose Three."
;;
*)
echo "Invalid choice!"
;;
esac
Shell Programming
Input/Output:

Enter a number (1-3): 2


You chose Two.
Shell Programming
In shell programming, positional parameters are special variables that hold
the values of arguments passed to a script or function. These variables allow
you to access and manipulate the arguments dynamically.
Shell Programming
List of Positional Parameters
Parameter Description
$0 The name of the script or function being executed.
$1, $2, ... The arguments passed to the script or function.
$# The number of arguments passed to the script.
$@ All arguments as a list, preserving quotes.
$* All arguments as a single word (uses IFS as separator).
"$@" Each argument is treated as a separate quoted string.
"$*" All arguments treated as a single quoted string.
Shell Programming
The set command is used to set or reset the positional parameters for a shell
script or function. When used with arguments, it replaces the existing
positional parameters with the specified values.
Syntax :
set [options] [arguments]
Shell Programming
Setting Positional Parameters
#!/bin/bash
set apple banana cherry
echo "First: $1"
echo "Second: $2"
echo "Third: $3"
Output: First: apple
Second: banana
Third: cherry
Shell Programming
Resetting Positional Parameters
#!/bin/bash
set a b c
echo "Before resetting: $@"
set
echo "After resetting: $@"
Output: Before resetting: a b c
After resetting:
Calling set with no arguments resets positional parameters.
Shell Programming
Using Options with set
Some common options for set include:
-x: Enables debugging mode, showing each command before execution.
-e: Exits the script if any command returns a non-zero status.
-u: Treats unset variables as errors.
Shell Programming
The shift command shifts the positional parameters to the left by a specified
number of positions. The first $n arguments are removed, and the remaining
arguments are renumbered. If no argument is provided, shift defaults to 1.
Syntax : shift [n]
n: The number of positions to shift (default is 1).
Shell Programming
Shifting Positional Parameters
#!/bin/bash
set apple banana cherry
echo "Before shift: $@"
shift
echo "After 1 shift: $@"
Output: Before shift: apple banana cherry
After 1 shift: banana cherry
Shell Programming
Using shift in a Loop Output: Argument: one

#!/bin/bash Argument: two

Argument: three
set one two three four five
Argument: four
while [ $# -gt 0 ]; do
Argument: five
echo "Argument: $1"
The shift command reduces the number of
shift arguments ($#) until none remain.

done
Shell Programming
Shifting Multiple Positions
#!/bin/bash
set a b c d e f
echo "Before shift: $@"
shift 3
echo "After 3 shifts: $@"
Output: Before shift: a b c d e f
After 3 shifts: d e f

You might also like