My very first time Bash-Scripting

avatar

There is a saying that “learning never stops” this is true with every aspect of my life including programming. Over the years, I have always wanted to become a pro at programming but the technology seems to be faster than I could learn. This is why I have a lot of to-do lists on my desk.

Anyways, this year, I ticked the box to learn a backend stack so as to complement my frontend skill. I reached out to developers on LinkedIn and on my personal contact for feedback and most of them suggested that it would make more sense if I learn Nodejs because It’s more of JavaScript which I’m used to writing already. I agreed to that suggestion and learned for the first week but dropped it because I had to return to an unfinished project(Fiverr gig).

luckily, I finished the gig in a short period of time then I went back to learning backend stacks. But this time, I didn't 'go back to learning Nodejs. Instead, I decided to follow Alx's career path. ALX is a leading technology training provider, built to accelerate the careers of young Africans through technology and professional skills._

four4.png

three3.png

two2.png

one1.png


I have registered for the career path but I heard it takes time before they send onboarding messages to members. So, in the meantime, I have started studying by myself. From the image above, Git and Command Line Edittors is the very first topic which seems very easy for me because I have been working with these tools for years now.

Next, was Bash. This right here isn't a new word to me but is definitely a new environment. Here's what I mean; I have always heard of bash-scripting but I have never seen what the code looks like. But my recent approach made me experience how interesting it is to instruct your computer via scripts on the command line.

Note: I still haven't been accepted to the Alx academy. I am only learning these by myself via the help of Youtube videos. Here's the video I studied with;

What's Bash?

Bash is the shell, or command language interpreter, for the GNU operating system. The name is an acronym for the ' Bourne-Again SHell

One must be used to using CLI (command line interface) to be comfortable with writing bash script. My first bash script was easy because I have been communicating with Cli before now. Although, I learned new commands and they were very helpful during my bash script journey.

The challenge

I had issues writing the commands that includes 'sudo' simply because I was running my scripts on git bash and cmd Cli on Window os. I had to install WSL

Bash Script vs Other programming languages

Right before this tutorial, I thought bash script is only a simple script that performs a task. Well, this is right but I never looked at it like a programming language. Yes, you read it right. Bash has similarities to other languages. Example

Variables

The interesting part of variables in Bash script is that declaring them is very easy compared to other programing languages. Check the examples before

//Bash script

#!/bin/bash

hiveUser=rufans

//javascript
const hiveUser='rufans'


Math functions

Okay, here's where things get completely different. On the command lIne, one must add the 'expr' keyword to perform a math fucntion.

//Bash script


expr 2 + 3

//javascript
console.log(2+3)


If statement

Truth be told, I enjoy playing around with if statement while learning any langauage and Bash wans't left out. The syntax wasn't completely different form javascript but there were slight changes.

The bash syntax below is known as using Test. One should also note that the space between the brackets and the values is compulsory.

'echo' is similar to console.log(). Lastly, terminating if condition with fi is important.

//Bash script

#!/bin/bash

if [ 2 -gt 4 ]
then
echo "2 is greater"
else
echo "2 is not greater."
fi
//javascript
if(2>4){
    console.log(''2 is greater)
}else{
    console.log('2 is not greater')
}


While Loop

//Bash script

myvar=1
while [ $myvar -le 10 ]
do
echo $myvar
myvar=$(($myvar +1))
done

//javascript

let myvar =1

while (myvar < 10){
    console.log(myvar)
    myvar++

}


For Loop


 #!/bin/bash


for i in {1..10}
do
  echo $i
done


//javascript

for (let i = 1; i <= 10; i++) {
  console.log(i)
}



Data streams

Accepting inputs from both languages are easy especailly for Bash.

Echoing an output is same as consoling an output.


 #!/bin/bash

echo "Enter your hive username: "
read hive_User
echo "The Hive user name  is $hive_User"


//javascript

let hive_user = prompt("Enter your hive username", "rufans");

 console.log(hive_user)


    • Input
    • Output
    • Error

Functions

Truth be told, I never thought writing functions is bash would be this easy. Same as calling the fucntion.


 #!/bin/bash

print_My_Hive_Account () {
  echo "rufans"
}

 print_My_Hive_Account
//javascript

fucntion print_My_Hive_Account () {
  console.log("rufans")
}

 print_My_Hive_Account()


###
Case statements

 #!/bin/bash

case numb in

  1)
    echo "one"
    ;;

  2)
    echo "two"
    ;;
  3)
    echo "three"
    ;;
      4)
    echo "four"
    ;;
      5)
    echo "five"
    ;;

  *)
    echo "sorry, number not in the range"
    ;;
esac

//javascript

switch (numb) {

  case 1:
    console.log('one')
    break;
      case 2:
    console.log('two')
    break;
      case 3:
    console.log('three')
    break;
      case 4:
    console.log('four')
    break;
      case 5:
    console.log('5')
    break;
     default:
    console.log('number not in range')
    }


I covered all of these within 4 days and I must say it was worth it. Next, I will be starting with C language while I await the Academy's onboarding mail. But for now, I keep learning on my own, taking Fiverr gigs, and coding my personal projects.

Contact
https://metonj.dev
https://github.com/realrufans
https://www.linkedin.com/in/realrufans.



0
0
0.000
8 comments
avatar
(Edited)

Bash scripting is an interesting thing to learn, it has helped me a lot, especially in automating daunting tasks. I wish you all the best in your learning journey

0
0
0.000
avatar

Wow 😯 thanks bro. I specialise in frontend development. I just decided to start backend with Bash. And I know it’d really help.

0
0
0.000
avatar

Congratulations @rufans! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You distributed more than 22000 upvotes.
Your next target is to reach 23000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

HiveBuzz rewards participants in the Afri-Tunes Anniversary event
Keep Hive Buzzing - Support our proposal!
The Hive Gamification Proposal
Support the HiveBuzz project. Vote for our proposal!
0
0
0.000
avatar

The issue with the downvotes has been resolved, you can now power up your airhawk token and receive rewards in airhawk when you use our community tag Airhawk-project as one of the tags on your content.

You also receive curation reward when your upvote other authors using our tag on their content.

Stake airhawk and continue enjoying support upvote from being a token holder.
Check your earnings using this link

https://he.dtools.dev/@rufans?symbol=AIRHAWK

Trade airhawk on tribaldex with the link below

https://tribaldex.com/trade/AIRHAWK

0
0
0.000