Thursday, August 27, 2020

bash script

 

http://web.theurbanpenguin.com/training/scripting/mastering-bash-shell-scripting/

http://web.theurbanpenguin.com/category/bash-scripting/


http://web.theurbanpenguin.com/category/bash-scripting/


if statements

//////////////////////////////

syntax

if [ expression ];  

then  

statements  

fi  

//////////////////////////////


example 1

read -p "Enter number:" number

if [ $number -gt 123 ];

then

echo "value is greater than 123"

fi

o/p:value is greater than 123

////////////////////////////////////

example 2

if [ "myfile" == "myfile" ];

then

        echo "its true"

fi

o/p:its true

//////////////////////////////////

For using multiple conditions with AND operator:

''''''''''''''''''''''''''''''''''''''''''''''''''''''

if [ expression_1 ] && [ expression_2 ];  

then  

statements  

fi


example 1

if [ 1 -lt 10 ] && [ 100 -gt 10 ];

then

echo "we won"

fi

o/p: we won

////////////////////////////

For using multiple conditions with OR operator:

if [ expression_1 ] || [ expression_2 ];  

then  

statements  

fi  


case statement example

echo "Enter the arithmetic operation you want to perform?"

echo "A for ADD, S for SUBTRACTION, M FOR MULTIPLY, D FOR DIVISION"

echo "Enter the  operands "

read x

read y

read -p "A|S|M|D" val

case $val in

        A|a)

 sum=$((y+x))

echo $sum

;;

  S|s)

          ((sub=$x-$y))

echo " Subtraction of $x - $y =$sub"

;;

      M|m)

              ((mul=$x*$y))

echo " Multiplication of $x * $y =$mul"

;;

      D|d)

        ((div=$x/$y))

echo "Division of $x/$y =$div"

;;

esac

forloop 
**********************************
syntax

for (( expression1; expression2; expression3 ))  
do  
commands  
done
///////////////////////////////////////
example 1
echo "FINDING SUM OF N NUMBERS"
read x
sum=0
for (( i=0; i<=x; i++ ))
do
        ((sum=sum+i))
done
echo "$sum"
o/p x=5
15
//////////////////////////////////////
syntax

for variable in list  
do  
commands  
done  

example
for num in {1..10}
do
echo $num
done
echo"series of numbers from 1 to 10"
o/p:
1
2
3
4
5
6
7
8
9
10
////////////////////////////////////////////////
For Loop to Read Array Variables
syntax
array=(  "element1" "element 2" .  .  "elementN" )  
  
for i in "${arr[@]}"  
do  
echo $i  
done  

example
arr=( "sun" "moon" "earth" "mars" "venus" "mercury" )
for i in "${arr[@]}"
do
echo $i
done

o/p
sun
moon
earth
mars
venus
mercury
/////////////////////////////////////////////////
For Loop with a Continue Statement
use the 'continue' statement inside the 'for' loop to skip any specific statement on a particular condition. It tells Bash to stop executing that particular iteration of the loop and process the next iteration.

example program to print Numbers from 1 to 20, ignoring from 6 to 15 using continue statement"
for ((i=1; i<=20; i++));
do
if [[ $i -gt 5 && $i -lt 16 ]];
then
continue
fi
echo $i
done
///////////////////////////////////////////////////////////////////
For Loop with a Break Statement
A 'break' statement can be used inside 'for' loop to terminate from the loop.

example
#!/bin/bash
#Table of 2

for table in {2..100..2}
do
        echo $table
        if [ $table == 20 ]; then
                break
        fi
done
o/p:
2
4
6
8
10
12
14
16
18
20

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Infinite Bash For Loop

When there is no 'start, condition, and increment' in the bash three expressions for loop, it becomes an infinite loop. To terminate the infinite loop in Bash, we can press Ctrl+C.

example
i=1;
for (( ; ; ))
do
        sleep 1s
        echo "Current Number: $((i++))"
done


Bash While Loop
****************************
while [ condition ];
 do 
 commands; 
 done  
 
 example
 #!/bin/bash
#Script to get specified numbers

read -p "Enter starting number: " snum
read -p "Enter ending number: " enum

while [[ $snum -le $enum ]];
do
        echo $snum
        ((snum++))
done

echo "This is the sequence that you wanted."
o/p
Enter starting number: 2
Enter ending number: 9
2
3
4
5
6
7
8
9
This is the sequence that you wanted.
////////////////////////////////////////////////////////////
Infinite While Loop
An infinite loop is a loop that has no ending or termination. If the condition always evaluates to true, it creates an infinite loop. 

example
#!/bin/bash
#An infinite while loop

while :;
do
        echo "YOU r in a loop.";
done
  

Type Casting in C++

static_cast