Shell Programming Language in Operating System .pptx
1. Shell
• A shell is a command-line interface (CLI) program that interprets user commands and
executes them.
• It acts as an intermediary between the user and the operating system (OS), allowing users
to interact with the OS by typing commands into a text-based interface rather than using
graphical user interfaces (GUIs).
• Graphical Shell
• Command line shell
• A shell provides a way for users to access the functionality and services of the underlying
operating system through commands and scripts.
2. Shell features
• Command Execution: Shells execute commands entered by the user or read from scripts.
• Input/Output Redirection: Shells allow users to redirect input and output streams to and from files
or other processes.
• Command Pipelines: Shells support chaining commands together using pipes (|) to pass the output
of one command as the input to another.
• Variables and Environment Control: Shells support variables for storing data and controlling the
environment in which commands are executed.
• Control Structures: Shells provide control structures such as loops and conditional statements for
scripting and automation.
• Job Control: Shells allow users to manage processes, including running them in the background,
suspending them, or bringing them to the foreground.
3. Common types of shells
• Bash (Bourne Again Shell): The default shell for most Linux distributions and macOS. It is a
powerful and widely used shell with extensive features and capabilities.
• Zsh (Z Shell): A highly customizable shell with features for improved interactive use and scripting.
• csh (C Shell): One of the earliest Unix shells, developed by Bill Joy for the BSD Unix system. It has a
syntax similar to the C programming language.
• tcsh (TENEX C Shell): An enhanced version of csh, adding features such as command line editing
and history.
4. Shell Variables
• Shell variables in C shell are entities that store data, such as strings or integers, for later
use within the shell environment. They can be set and accessed using the set command
and referenced using the $ symbol.
For example:
set variable_name = value
Example:
set name = "John"
set age = 25
5. Variable Access:
• To access the value of a variable, you prepend a dollar sign ($) to its name:
echo $variable_name
echo $name
John
6. Variable Substitution:
• Variables can be substituted within strings using curly braces ({}) to distinguish
the variable name from surrounding text. This is useful when constructing complex
strings:
echo "My name is ${name} and I am ${age} years old.“
output:
My name is John and I am 25 years old.
7. certain rules for naming Variables
• Start with a Letter or Underscore: Variable names must begin with a letter (a-z or A-Z) or an underscore (_).
• Subsequent Characters: After the initial letter or underscore, variable names can include letters, digits (0-9), or
underscores.
• Case Sensitivity: Shell variables are typically case-sensitive. For instance, $var, $VAR, and $vAr would refer to
different variables.
• Reserved Keywords: Avoid using shell keywords or reserved words as variable names. These are words with
special meaning to the shell, such as if, while, for, do, done, case, etc.
• Avoid Special Characters: It's best to avoid using special characters such as spaces, punctuation marks, or
operators in variable names. Stick to alphanumeric characters and underscores for simplicity and compatibility.
• Length: While there is no strict limit on the length of variable names, it's recommended to keep them reasonably
short and descriptive for readability.
8. certain rules for naming Variables
• Valid variable names:
• name
• age
• _count
• var123
• _my_var
• Invalid variable names:
• 1var # Cannot start with a digit
• my-var # Hyphens are not allowed
• my var # Spaces are not allowed
• if # Reserved keyword
9. Parameter shell commands,
• Parameter shell commands, also known as control flow or flow control commands, are fundamental
constructs in shell scripting that allow you to control the flow of execution based on conditions, loop
through sets of data, and manipulate the behavior of your script.
• if, while, until, for, break, continue
10. if
• “if-else” is dealing with two-part of the executions. If the “if-else “condition is “true” then the first
group of statements will execute. If the condition is “false” then the second group of statements will
execute.
11. if
Syntax:
• The if statement starts with the keyword if.
• Inside square brackets [ ], you specify the condition that needs to be evaluated. This condition can
involve comparisons, logical operators, or any other expression that evaluates to true or false.
• After the condition, the then keyword marks the beginning of the code block to be executed if the
condition is true.
• The fi keyword marks the end of the if statement.
• It's important to note that spaces are required around the square brackets [ ] and after the if, then,
and fi keywords for proper syntax.
12. If example
echo "Enter a number:"
read number
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
13. while
• The while loop in shell scripting is used to execute a block of code repeatedly as long as a specified
condition is true.
• The while keyword marks the beginning of the loop.
• Inside square brackets [ ], you specify the condition that needs to be evaluated.
• This condition can involve comparisons, logical operators, or any other expression that evaluates to
true or false.
• After the condition, the do keyword marks the beginning of the code block to be executed while the
condition is true.
• At the end of the code block, the done keyword marks the end of the loop.
• It's important to note that spaces are required around the square brackets [ ] and after the while, do,
and done keywords for proper syntax.
14. while
Example script using a while loop to count from 1 to 5
• count=1
• while [ $count -le 5 ]; do
• echo "Count is $count"
• count=$((count + 1))
• done
Output:
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5
15. until
• The until loop in shell scripting is similar to the while loop but with an opposite condition.
• It executes a block of code repeatedly until a specified condition becomes true.
• The until keyword marks the beginning of the loop.
• Inside square brackets [ ], you specify the condition that needs to be evaluated.
• This condition can involve comparisons, logical operators, or any other expression that evaluates to true or false.
• After the condition, the do keyword marks the beginning of the code block to be executed until the condition
becomes true.
• At the end of the code block, the done keyword marks the end of the loop.
• It's important to note that spaces are required around the square brackets [ ] and after the until, do, and done
keywords for proper syntax.
16. until
• count=1
• until [ $count -gt 5 ]; do
• echo "Count is $count"
• count=$((count + 1))
• done
Output:
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5
17. for
• The for loop in shell scripting is used to iterate over a list of items and execute a block of code for each
item in the list.
• The for keyword marks the beginning of the loop.
• variable is a variable that will take on each value in the list during each iteration of the loop.
• list is a sequence of items separated by spaces.
• After the list, the do keyword marks the beginning of the code block to be executed for each item in the
list.
• At the end of the code block, the done keyword marks the end of the loop.
• It's important to note that spaces are required after the for, in, do, and done keywords for proper
18. for
• for num in 1 2 3 4 5; do
• echo "Number is $num"
• Done
Outuput
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
• The for loop iterates over the list of numbers 1 2 3 4 5.
• During each iteration, the variable num takes on the value of each number in the list.
• Inside the loop, it executes the code block, which prints "Number is ..." for each value of num.
19. break
• In shell scripting, the break statement is used to exit or terminate a loop prematurely.
• It is typically used within a loop (such as for, while, or until) to stop the loop execution based on
certain conditions
• count=1
• while [ $count -le 10 ]; do
• echo "Count is $count"
• if [ $count -eq 5 ]; then
• break # Exit loop if count reaches 5
• fi
• count=$((count + 1))
• done
OUTPUT
Count is 1
Count is 2
Count is 3
Count is 4
Count is 5
20. continue
• In shell scripting, the continue statement is used to skip the remaining code within a loop iteration
and proceed with the next iteration of the loop.
• It is typically used within a loop (such as for, while, or until) to skip certain iterations based on
specific conditions
• for (( i=1; i<=10; i++ )); do
• if [ $((i % 2)) -ne 0 ]; then
• continue # Skip odd numbers
• fi
• echo "Number is $i"
• done