
By: mrNayaNi
June 8, 2016
Simple Script in Linux "Bash"

By: mrNayaNi
June 8, 2016

echo "Hello World"
That's it. Wait...is there a trap? It can't be that easy! It really is that easy.
This is how you declare a variable in Bash:i=10
Remember not to put space between them.
Classic "if" statement:if [ 1 -eq 2 ]; then
echo "1 is equals to 2"
else
echo "1 is not equal to 2"
fi
This code will check whether 1 is equal to 2 or not.
Let's compare strings in another example of an "if" statement:if [ "Hello" == 'world" ];then;
echo "Hello is equal to world"
else
echo "Hello is not equal to world"
fi
You may have noticed that when I compared strings, I used double equals to (==). When comparing numbers, I used (-eq). In Bash, when comparing numbers, we'll use -eq(equals to), -lt (less than), -gt (greater than) etc. and to compare strings we'll use. e.g. == (equals to), != (not equals to) etc.
Loops;There are three types of loops in Bash:- For Loop
- While Loop
- Until Loop
for i in seq 1 10;
do
echo $i
done
This will print numbers 1 to 10
While Loop Usage:number=0
while [ $number -lt 10 ]; do
echo "Number is $number"
$((number++))
done
This code starts with declaring a variable that's a number, which is equal to 0. The condition is while number is less than 10 than print Number is $number the variable $((number++)) this line will increment the variable
Until Loop Usage:counter=20
until [ $counter -lt 10 ]; do
echo "counter $counter"
$((counter--))
done
This code will decrement the variable counter until it reaches 10.
Conclusion:Today, I showed you how to write simple Bash scripts. I'll soon write more. Hope you learned a lot.