7 os
7 os
Types:
1. The Bourne Shell (sh)
2. The GNU Bourne-Again Shell (bash)
3. The C Shell (csh)
4. The Korn Shell (ksh)
5. The Z Shell (zsh)
BASH:
Bash (Bourne Again Shell) is a shell program. It was written by Brian Fox as an enhanced version of the
Bourne Shell program 'sh'. It is an open source GNU project. It provides functional improvements over Bourne
Shell for both programming and interactive use.
Open any editor to create a bash file. Here, nano editor is used to create the file and filename is set as ‘First.sh’
$ nano First.sh
Add the following bash script to the file and save the file.
You can run bash file by two ways. One way is by using bash command and another is by setting execute
permission to bash file and run the file. Both ways are shown here.
Page no : Rollno:2123967
Conditional statements (if/else)
Expressions that produce a boolean result, either true or false, are called conditions. There are several ways to
evaluate conditions, including if, if-else, if-elif-else, and nested conditionals.
Syntax:
if [[ condition ]];
then
statement
elif [[ condition ]]; then
statement
else
do this by default
fi
This statement checks if both conditions are true: a is greater than 60 AND b is less than 100.
Let's see an example of a Bash script that uses if, if-else, and if-elif-else statements to determine if a user-inputted
number is positive, negative, or zero:
The script first prompts the user to enter a number. Then, it uses an if statement to check if the number is greater
than 0. If it is, the script outputs that the number is positive. If the number is not greater than 0, the script moves on
to the next statement, which is an if-elif statement. Here, the script checks if the number is less than 0. If it is, the
script outputs that the number is negative. Finally, if the number is neither greater than 0 nor less than 0, the script
uses an else statement to output that the number is zero.
Looping in Bash :
Page no : Rollno:2123967
While loop:
While loops check for a condition and loop until the condition remains true. We need to provide a counter
statement that increments the counter to control loop execution.
In the example below, (( i += 1 )) is the counter statement that increments the value of i. The loop will run exactly
10 times.
For loop:
The for loop, just like the while loop, allows you to execute statements a specific number of times. Each loop
differs in its syntax and usage.In the example below, the loop will iterate 5 times.
Case statements:
In Bash, case statements are used to compare a given value against a list of patterns and execute a block of code
based on the first pattern that matches. The syntax for a case statement in Bash is as follows:
case expression in
pattern1)
# code to execute if expression matches pattern1
;;
pattern2)
# code to execute if expression matches pattern2
;;
pattern3)
# code to execute if expression matches pattern3
;;
*)
Page no : Rollno:2123967
# code to execute if none of the above patterns match expression
;;
esac
Here, "expression" is the value that we want to compare, and "pattern1", "pattern2", "pattern3", and so on are the
patterns that we want to compare it against.
The double semicolon ";;" separates each block of code to execute for each pattern. The asterisk "*" represents the
default case, which executes if none of the specified patterns match the expression.
In this example, since the value of "fruit" is "apple", the first pattern matches, and the block of code that echoes
"This is a red fruit." is executed. If the value of "fruit" were instead "banana", the second pattern would match and
the block of code that echoes "This is a yellow fruit." would execute, and so on. If the value of "fruit" does not
match any of the specified patterns, the default case is executed, which echoes "Unknown fruit."
Page no : Rollno:2123967