Linux Commands Practical
Linux Commands Practical
(Degree Wing)
Submitted By Submitted To
Ravi Verma Dr. Dheerendra Singh
Roll No: CO19352 Professor
CSE 4th Semester
Aim: Learning of Shell programming and perform as well as run minimum of five programs
based on arithmetic operations.
Shell
In computing, a shell is a computer program which exposes an operating system's services to a
human user or other program. In general, operating system shells use either a command-line
interface or graphical user interface, depending on a computer's role and particular operation
Types of shell
In Unix, there are two major types of shells
● Bourne shell − If you are using a Bourne-type shell, the $ character is the default prompt.
● C shell − If you are using a C-type shell, the % character is the default prompt.
Shell Programming
A shell script is a computer program designed to be run by the Unix/Linux shell, a command-line
interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical
operations performed by shell scripts include file manipulation, program execution, and printing
text.
Advantages
The command and syntax are exactly the same as those directly entered in the command line, so
programmer do not need to switch to entirely different syntax
Writing shell scripts are much quicker
Quick start
Interactive debugging etc.
Disadvantages
Prone to costly errors, a single mistake can change the command which might be harmful
Slow execution speed
Design flaws within the language syntax or implementation
Not well suited for the large and complex task
Provide minimal data structure, unlike other scripting languages. etc
Comments
Comments are the useful information that the developers provide to make the reader understand
the source code. It explains the logic or a part of it used in the code. Comments are usually
helpful to someone maintaining or enhancing your code when you are no longer around to
answer questions about it. These are often cited as a useful programming convention that does
not take part in the output of the program but improves the readability of the whole program.
Single-line comments
A single-line comment starts with a hashtag symbol with no white spaces (#) and lasts till the
end of the line. If the comment exceeds one line then put a hashtag on the next line and continue
the comment.
The shell script is commented out prefixing # character for single-line comment.
Syntax
#This is a comment
Multi-line comments
A multi-line comment is a piece of text enclosed in a delimiter (”) on each end of the comment.
Again there should be no white space between delimiter (‘ ‘). They are useful when the comment
text does not fit into one line; therefore need to span across lines. Multi-line comments or
paragraphs serve as documentation for others reading your code. See the following code snippet
demonstrating multi-line comment
Syntax
:'
This is a
Multi-line comments'
Variables
A variable is nothing more than a pointer to the actual data. The shell enables you to create,
assign, and delete variables.
The name of a variable can contain only letters (a to z or A to Z), numbers ( 0 to 9) or the
underscore character ( _).
Types of variables
● Local Variables − A local variable is a variable that is present within the current instance
of the shell. It is not available to programs that are started by the shell. They are set at the
command prompt.
● Environment Variables − An environment variable is available to any child process of the
shell. Some programs need environment variables in order to function correctly. Usually,
a shell script defines only those environment variables that are needed by the programs
that it runs.
● Shell Variables − A shell variable is a special variable that is set by the shell and is
required by the shell in order to function correctly. Some of these variables are
environment variables whereas others are local variables.
Syntax
Declaring a variable
variable_name=variable_value
Accessing variable
$variable_name
Deleting variable
unset variable_name
Arrays
Shell supports a different type of variable called an array variable. This can hold multiple values
at the same time. Arrays provide a method of grouping a set of variables. Instead of creating a
new name for each variable that is required, you can use a single array variable that stores all the
other variables.
Syntax
Declaring array
array_name[0] = value1
array_name[1] = value2
Operators
There are 5 basic operators in bash/shell scripting:
1. Arithmetic Operators
2. Relational Operators
3. Boolean Operators
4. Bitwise Operators
5. File Test Operators
Arithmetic Operators :
These operators are used to perform normal arithmetics/mathematical operations. There are 7
arithmetic operators:
1. Addition (+): Binary operation used to add two operands.
2. Subtraction (-): Binary operation used to subtract two operands.
3. Multiplication (*) :Binary operation used to multiply two operands.
4. Division (/) :Binary operation used to divide two operands.
5. Modulus (%) :Binary operation used to find remainder of two operands.
6. Increment Operator (++) : Uniary operator used to increase the value of operand by one.
7. Decrement Operator (–) : Uniary operator used to decrease the value of a operand by one
Relational Operators
Relational operators are those operators which define the relationship between two operands.
They give either true or false depending upon the relation. They are of 6 types:
1. ‘==’ Operator : Double equal to operator compares the two operands. Its returns true is
they are equal otherwise returns false.
2. ‘!=’ Operator : Not Equal to operator return true if the two operands are not equal
otherwise it returns false.
3. ‘<' Operator : Less than operator returns true if first operand is lees than second operand
otherwse returns false.
4. ‘<=' Operator : Less than or equal to operator returns true if first operand is less than or
equal to second operand otherwise returns false
5. ‘>’ Operator : Greater than operator return true if the first operand is greater than the
second operand otherwise return false.
6. ‘>=’ Operator : Greater than or equal to operator returns true if first operand is greater
than or equal to second operand otherwise returns false
Logical Operators
They are also known as boolean operators. These are used to perform logical operations. They
are of 3 types:
1. Logical AND (&&) : This is a binary operator, which returns true if both the operands are
true otherwise returns false.
2. Logical OR (||) : This is a binary operator, which returns true is either of the operand is
true or both the operands are true and returns false if none of then is false.
3. Not Equal to (!) : This is a uninary operator which returns true if the operand is false and
returns false if the operand is true.
Bitwise Operators
A bitwise operator is an operator used to perform bitwise operations on bit patterns. They are of
6 types:
1. Bitwise And (&) : Bitwise & operator performs binary AND operation bit by bit on the
operands.
2. Bitwise OR (|) : Bitwise | operator performs binary OR operation bit by bit on the
operands.
3. Bitwise XOR (^) : Bitwise ^ operator performs binary XOR operation bit by bit on the
operands.
4. Bitwise compliment (~) : Bitwise ~ operator performs binary NOT operation bit by bit on
the operand.
5. Left Shift (<<) : This operator shifts the bits of the left operand to left by number of times
specified by right operand.
6. Right Shift (>>) : This operator shifts the bits of the left operand to right by number of
times specified by right operand.
Conditional Statements
There are total 5 conditional statements that can be used in bash programming
1. if statement
2. if-else statement
3. if..elif..else..fi statement (Else If ladder)
4. if..then..else..if..then..fi..fi..(Nested if)
5. switch statement
if statement
This block will process if the specified condition is true.
Syntax
if [ expression ]
then
statement
fi
if-else statement
If the specified condition is not true in the if part then else part will be executed.
Syntax
if [ expression ]
then
statement1
else
statement2
fi
If-else if ladder
To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1
is true then it executes statements 1 and 2, and this process continues. If none of the condition is
true then it processes else part.
Syntax
if [ expression1 ]
then
statement1
statement2
elif [ expression2 ]
then
statement3
statement4
else
statement5
Nested if-else
Nested if-else block can be used when, one condition is satisfies then it again checks another
condition. In the syntax, if expression1 is false then it processes else part, and again expression2
will be check.
Syntax
if [ expression1 ]
then
statement1
statement2
else
if [ expression2 ]
then
statement3
fi
fi
Switch statement
case statement works as a switch statement if specified value match with the pattern then it will
execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is
executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax
case var in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac
Loops
Looping Statements in Shell Scripting: There are total 3 looping statements that can be used in
bash programming
● while statement
● for statement
● until statement
To alter the flow of loop statements, two commands are used they are,
● break
● continue
While loop
Here command is evaluated and based on the result loop will executed, if command raise to false
then loop will be terminated
Syntax
while command
do
Statement to be executed
done
For loop
The for loop operate on lists of items. It repeats a set of commands for every item in a list.
Here var is the name of a variable and word1 to wordN are sequences of characters separated by
spaces (words). Each time the for loop executes, the value of the variable var is set to the next
word in the list of words, word1 to wordN.
Syntax
for var in word1 word2 ...wordn
do
Statement to be executed
done
Until Loop
The until loop is executed as many as times the condition/command evaluates to false. The loop
terminates when the condition/command becomes true.
Syntax
until command
do
Statement to be executed until the command is true
done
Functions
Functions enable you to break down the overall functionality of a script into smaller, logical
subsections, which can then be called upon to perform their individual tasks when needed.
Using functions to perform repetitive tasks is an excellent way to create code reuse. This is an
important part of modern object-oriented programming principles.
Shell functions are similar to subroutines, procedures, and functions in other programming
languages.
Syntax
function_name () {
list of commands
}
Function call
function_name
Deleting function
unset -f function_name
Creating shell script
A shell script can be written any text editor and must be saved with a file extension .sh