Simple Script in Linux “Bash”

June 8, 2016 | Views: 5240

Begin Learning Cyber Security for FREE Now!

FREE REGISTRATIONAlready a Member Login Here

Introduction:

Bash is the scripting language used in Linux – it stands for Bourne Again Shell.

You can perform several tasks with this scripting language. It can make your lives easy using simple words. There are some tasks you have to do every day; you can write a script to do them for you instead of repeating these tasks and wasting time.

 

Simple Usage: 

Here’s how you write “Hello World” in Bash:

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:

  1. For Loop
  2. While Loop
  3. Until Loop

 

For Loop Usage:

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.

Share with Friends
FacebookTwitterLinkedInEmail
Use Cybytes and
Tip the Author!
Join
Share with Friends
FacebookTwitterLinkedInEmail
Ready to share your knowledge and expertise?
13 Comments
  1. Very easy to understand

Page 3 of 3«123
Comment on This

You must be logged in to post a comment.

Our Revolution

We believe Cyber Security training should be free, for everyone, FOREVER. Everyone, everywhere, deserves the OPPORTUNITY to learn, begin and grow a career in this fascinating field. Therefore, Cybrary is a free community where people, companies and training come together to give everyone the ability to collaborate in an open source way that is revolutionizing the cyber security educational experience.

Cybrary On The Go

Get the Cybrary app for Android for online and offline viewing of our lessons.

Get it on Google Play
 

Support Cybrary

Donate Here to Get This Month's Donor Badge

 
Skip to toolbar

We recommend always using caution when following any link

Are you sure you want to continue?

Continue
Cancel