Skip to content

Commit

Permalink
Operators and Decision Making
Browse files Browse the repository at this point in the history
  • Loading branch information
logicopslab committed Dec 15, 2021
1 parent d99c5b8 commit 1f5f66b
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 03.Operators/01.Arithmetic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

a=10
b=20

add=$((a + b))
echo Addition of a and b are $add

sub=$((a - b))
echo Subtraction of a and b are $sub

mul=$((a * b))
echo Multiplication of a and b are $mul

div=$((a / b))
echo division of a and b are $div

mod=$((a % b))
echo Modulus of a and b are $mod

46 changes: 46 additions & 0 deletions 03.Operators/02.Relational.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

a=10
b=20

if(( $a==$b ))
then
echo a is equal to b.
else
echo a is not equal to b.
fi

if(( $a!=$b ))
then
echo a is not equal to b.
else
echo a is equal to b.
fi

if(( $a<$b ))
then
echo a is less than b.
else
echo a is not less than b.
fi

if(( $a<=$b ))
then
echo a is less than or equal to b.
else
echo a is not less than or equal to b.
fi

if(( $a>$b ))
then
echo a is greater than b.
else
echo a is not greater than b.
fi

if(( $a>=$b ))
then
echo a is greater than or equal to b.
else
echo a is not greater than or equal to b.
fi
25 changes: 25 additions & 0 deletions 03.Operators/03.Logical.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

a=true
b=false

if [[ "$a" == "true" && "$b" == "false" ]];
then
echo Both conditions are true.
else
echo Both conditions are not true.
fi

if [[ "$a" == "true" || "$b" == "false" ]];
then
echo At least one of them is true.
else
echo None of them is true.
fi

if [[ ! "$a" == "true" ]];
then
echo "a" was initially false.
else
echo "a" was initially true.
fi
25 changes: 25 additions & 0 deletions 03.Operators/04.Bitwise.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

a=7
b=4

b_AND=$(( a&b ))
echo Bitwise AND of a and b is $b_AND

b_OR=$(( a|b ))
echo Bitwise OR of a and b is $b_OR

b_XOR=$(( a^b ))
echo Bitwise XOR of a and b is $b_XOR

b_Complement=$(( ~a ))
echo Bitwise Compliment/NOT of a is $b_Complement
# n -> -(n+1)

l_shift=$(( a<<2 ))
echo Left Shift of a is $l_shift
# 00000111 << 00001110 << 00011100

r_shift=$(( b>>2 ))
echo Right Shift of b is $r_shift
# 00000100 >> 00000010 >> 00000001
38 changes: 38 additions & 0 deletions 03.Operators/05.FileTest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

FileName="04.Bitwise.sh"

if [ -e $FileName ]
then
echo Yes! The file exists
else
echo No! The file doesn"'"t exist
fi

if [ -s $FileName ]
then
echo File is not empty.
else
echo File is empty.
fi

if [ -r $FileName ]
then
echo File has read access.
else
echo File does not has read access.
fi

if [ -w $FileName ]
then
echo The file has write access.
else
echo The file does not has write access.
fi

if [ -x $FileName ]
then
echo The file has execute access.
else
echo The file doesn"'"t has execute access.
fi
20 changes: 20 additions & 0 deletions 04.DecisionMaking/01.If.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

#Initializing two variables

a=20
b=40

# Equal or Not check

if [ $a == $b ]
then
echo "a is equal to b"
fi

# Not equal check

if [ $a != $b ]
then
echo "a is not equal to b"
fi
13 changes: 13 additions & 0 deletions 04.DecisionMaking/02.If-Else.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

# Initializing a and b as variables

a=20
b=20

if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
9 changes: 9 additions & 0 deletions 04.DecisionMaking/03.If-Elif-Else.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
number=4

if [ $number -eq 1 ]; then
echo value of number is 1
elif [ $number -eq 2 ]; then
echo value of number is 2
else
echo Invalid value
fi
13 changes: 13 additions & 0 deletions 04.DecisionMaking/04.If-Then-Elif-Else.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/bin/bash

total=10

if [ $total -eq 1000 ]
then
echo "total is equal to 1000"
elif [ $total -lt 1000 ]
then
echo "Total is less than 1000"
else
echo "Total is greater than 1000"
fi
16 changes: 16 additions & 0 deletions 04.DecisionMaking/05.SwitchCase.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

DOGS="husky"

#Pass the variable in string

case "$DOGS" in
#case 1
"indie") echo "Found in moderate temperature places" ;;

#case 2
"scoobydoo") echo "Found only in cartoons" ;;

#case 3
"husky") echo "Found in very cold places" ;;
esac

0 comments on commit 1f5f66b

Please sign in to comment.