In Linux, all tasks from execution of services to loading and unloading of modules are carried out by programs and all programs need to be executed. You use the commands to access all the basic features of kernel. Shell scripting is a way to automate such tasks, and bash is one of the language, that has capabilities enough to be called as scripting as well as a language that can be used for programming on the POSIX platform, for small tasks. Bash Scripting IF with AND or OR logic
Using If Statement
You can use if condition with single or multiple conditions. Starting and
ending block of this statement is define by ‘if’ and ‘fi’. Create a file named
‘simple_if.sh’ with the following script to know the use if statement in bash.
Here, 10 is assigned to the variable, n. if the value of $n is less than 10
then the output will be “It is a one digit number”, otherwise the output will
be “It is a two digit number”. For comparison, ‘-lt’ is used here. For
comparison, you can also use ‘-eq’ for equality, ‘-ne’ for not equality and
‘-gt’ for greater than in bash script.
#!/bin/bash
n=10
if [ $n -lt 10 ];
then
echo "It is a
one digit number"
else
echo "It is a two digit number"
fi
Run the file with bash command.
Using if statement with AND logic:
Different types of logical conditions can be used in if statement with two or more conditions. How you can define multiple conditions in if statement using AND logic is shown in the following example. ‘&&’ is used to apply AND logic of if statement. Create a file named ‘ifWithLogic1.sh’ to check the following code. Here, the value of username and password variables will be taken from the user and compared with ‘admin’ and ‘secret’. If both values match then the output will be “valid user”, otherwise the output will be “invalid user”.
#!/bin/bash
echo "Enter username"
read username
echo "Enter password"
read password
if [[ ( $username == "admin" && $password == "secret" ) ]]; then
echo "valid user"
else
echo "invalid user"
fi
Run the file with bash command.
Using if statement with OR logic:
‘||’ is used to define OR logic in if condition. Create a file
named ‘ifWithLogic.sh’ with the following code to check the use of OR logic of
if statement. Here, the value of n will be taken from the user. If the value
is equal to 15 or 45 then the output will be “You won the game”, otherwise the
output will be “You lost the game”.
#!/bin/bash
echo "Enter any number"
read n
if [[ (
$n -eq 15 || $n -eq 45 ) ]]
then
echo "You won the
game"
else
echo "You lost the game"
fi
Run the file with bash command.
I hope you liked this post, then you should not forget to share this post at
all.
Thank you so much :-)
Disclaimer
This was written for educational purpose and pentest only.
The
author will not be responsible for any damage ..!
The author of this tool
is not responsible for any misuse of the information.
You will not misuse
the information to gain unauthorized access.
This information shall only
be used to expand knowledge and not for causing malicious or damaging
attacks. Performing any hacks without written permission is illegal ..!
All tutorials and videos have been made using our own routers, servers, websites and other resources, they do not contain any illegal activity. We do not promote, encourage, support or excite any illegal activity or hacking without written permission in general. We want to raise security awareness and inform our readers on how to prevent themselves from being a victim of hackers. If you plan to use the information for illegal purposes, please leave this website now. We cannot be held responsible for any misuse of the given information.
- Hacking Truth by Kumar Atul Jaiswal