Experiment -5
1.Commandline
The command line in Linux is a text-based interface that allows users to interact
with the operating system by typing commands. It is also known as the shell,
terminal, console, or prompt. The command line is a powerful tool that can be used
to perform various tasks such as file manipulation, system administration, and
software installation .
To access the command line in Linux, we need to open a terminal window. Once
we have opened the terminal, we can start typing commands and pressing enter to
execute them . Some of the basic commands that you can use in the command line
include ls (list files), cd (change directory), mkdir (make directory), and rm
(remove files) .
Some examples are-
touch: Create an empty file.
cat: Display the contents of a file.
cp: Copy a file or directory.
mv: Move or rename files or directories.
2.Array
In Linux, an array is a variable that can hold multiple values of the same type or
different types . It is a structured arrangement of similar data elements .Arrays are
useful when you want to store and manipulate a large number of variables. Instead
of creating multiple variables, you can store the same type of values in an array
and access them via an index number .
In shell scripting, arrays are used to group a set of variables together 2. You can
declare an array in a shell script using various approaches such as indirect
declaration, explicit declaration, or compound assignment 1.
Here are some basic examples that we can perform on arrays in Linux:
Printing the first element of an array: echo ${array_name[0]}
Printing a selected index element: echo ${array_name[index_number]}
3.Conditional Statement
A conditional statement is a logical statement that describes a relationship between
two propositions, where the truth of one proposition depends on the truth of the
other . It is also known as an if-then statement because it consists of two parts: an
antecedent (if) and a consequent (then) .
The antecedent is the condition that must be met for the consequent to be
true .Alternatively known as a conditional expression, conditional flow statement,
and conditional processing , a conditional statement is a set of rules performed if a
certain condition is met. It is sometimes called an If-Then statement, because IF a
condition is met, THEN an action is performed.
In programming, conditional statements are used to control the flow of a program
by executing certain code only if a certain condition is met .
For Example-
if (x > 0) {
// execute this code if x is greater than 0
In this example, the code inside the curly braces will only be executed if the
condition inside the parentheses (x > 0) is true.
4.Decision Making
In Linux, decision-making is an important concept that is used in shell scripting to
control the flow of a program by executing certain code only if a certain condition
is met . The programmer provides one or more conditions for the execution of a
block of code. If the conditions are satisfied, then those block of codes only gets
executed .
There are two types of decision-making statements that are used within shell
scripting: if-else statement and case statement .
Example-#!/bin/bash
# Check if the file exists
if [ -f /path/to/file ]
then
echo "File exists"
else
echo "File does not exist"
fi
Q1-Write a script that takes a number as input whether it is prime or not?
Ans-
num=$1
for (( i=2 ; i<$num/2 ; i++ ))
do
rem=$((num%i))
if [$rem -eq 0]
then
echo “Number is a Prime no”
exit 0.
fi
echo “No it is not a Prime no”.
done