OS mod 5(8m)
OS mod 5(8m)
There are several different types of shells available, with some of the most
popular ones being:
1. Bash (Bourne Again SHell): Bash is the default shell for most Linux
distributions and is widely used on Unix-based systems. It is a powerful
and feature-rich shell, compatible with the original Bourne shell (sh) and
includes additional functionality.
2. sh (Bourne Shell): The Bourne shell was one of the first Unix shells and is
still available on many Unix-like systems. It is a relatively simple shell and
lacks some of the advanced features found in newer shells.
3. csh (C Shell): The C shell provides a C-like syntax and includes features
like command-line history and job control. It was popular in the early days
of Unix but has been largely replaced by more modern shells.
4. tcsh (TENEX C Shell): The tcsh is an enhanced version of the C shell,
adding additional features such as command completion and improved
command-line editing capabilities.
5. Korn Shell (ksh): The Korn shell is an advanced shell that combines
features from the Bourne shell (sh) and the C shell (csh). It offers a rich
set of features, including advanced scripting capabilities.
6. PowerShell: While not a traditional Unix shell, PowerShell is a powerful
shell and scripting language developed by Microsoft for Windows. It
provides access to system administration tasks and automation on
Windows-based systems.
These are just a few examples of the many shells available. Different
operating systems may have their own default shells, and users can often
choose which shell they prefer to use based on their needs and
preferences.
#!/bin/bash
# Define a function
greet() {
echo "Hello, $1!"
}
To call the function, we simply write its name followed by the argument(s)
we want to pass. In this case, we call greet twice, passing "Alice" and "Bob"
as arguments. When the function is called, it executes the code inside its
block, which results in the greeting message being displayed for each name.
In this example, the add function takes two arguments and calculates their
sum. The local keyword is used to declare a local variable called result,
which stores the sum. Then, the function echoes the value of result,
effectively returning it.
When we call the add function with the arguments 5 and 3, the return value
is assigned to the variable sum. Finally, we display the result using the echo
command.
The sum is: 8
This is a basic example of how functions work in shell scripting. You can
create more complex functions with multiple arguments, use control
structures inside functions, and even call other functions from within a
function. Functions provide a way to structure and organize your code,
making it more readable and maintainable.
If-else statement
case-sac statement
1. If-else statement
If else statement is a conditional statement. It can be used to execute
two different codes based on whether the given condition is satisfied or
not. There are a couple of varieties present within the if-else statement.
They are –
a) if-fi
b) if-else-fi
c) if-elif-else-fi
d) nested if-else
a. if [ expression ]; then
statements
fi
b. if [ expression ]
then
statement1
else
statement2
fi
c. if [ expression1 ]
then
statement1
statement2
.
.
elif [ expression2 ]
then
statement3
statement4
.
.
else
statement5
fi
case $var in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
Esac
Name="Satyajit"
case "$Name" in
#case 1
"Rajib") echo "Profession : Software Engineer" ;;
#case 2
"Vikas") echo "Profession : Web Developer" ;;
#case 3
"Satyajit") echo "Profession : Technical Content Writer" ;;
Esac
Output:
Profession : Technical Content Writer
In this example, the for loop iterates over a range of numbers from 1
to 5. In each iteration, the value of i is assigned to the current number
in the range, and the code block within the loop is executed. The echo
command displays the message "Number: " followed by the value of
i. When you run this script, the output will be:
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
2. While loop:
#!/bin/bash
In this example, the while loop iterates until the value of the counter
variable is less than or equal to 5. In each iteration, the code block
within the loop is executed. The echo command displays the message
"Count: " followed by the value of counter. After each iteration, the
counter variable is incremented by 1 using the $(( )) syntax. When the
value of counter becomes 6, the condition becomes false, and the
loop terminates. When you run this script, the output will be:
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5