Three Shells Bathroom

Three Shells Bathroom

Know Shell Scripting !!!

In this article, I will take you through the journey of writing shell/bash scripts. A shell script is a script or a file with a series of command written in it that can be read and executed by the shell. Instead of writing the commands manually, we can write a shell script that automates the whole process. Let's write a bash script that runs the heroku commands for creating a heroku app and pushing it to heroku. The full code will look like a full version of this article.

Aayush Jaiswal Hacker Noon profile picture

What is shell scripting? It is writing a series of commands for the shell to execute them. What is a shell? An operating system has 2 major components Shell and Kernel, what shell does is it interprets the commands and then executes them.

In this article, I will take you through the journey of writing shell/bash scripts. What is a shell script? It is a script or a file with a series of command written in it that can be read and executed by the shell. A shell script has a.sh extension. Bash stands for( Bourne Again SHell). Bash is not same as shell. It is the most used UNIX shell. In this article we will be writing the bash scripts.

Let's create the simplest of script.

  1. We will create a file, first.sh (you can use any text editor)
  2. The first line will be #! /bin/sh (it tells the interpreter that it is a bash script).
  3. We will write a command ls(list the subfolders in the directory)

And then to run this script, write bash first.sh in the shell. So, we wrote our first shell script, Easy? You might be the thinking what is the use of this, let's say we have to write a series of commands manually, again and again. We can just write the commands in a shell script and then run the shell script, instead of running the series of commands.

Suppose, you have to push a commit to your git repository. Instead of writing the commands one by one, we can write a shell script that automates the whole process. Create a file git.sh

#! /bin/sh                
git add .
git commit -m $1
git push origin master

In the above script, we automate the whole process of pushing the commit to the repo. I used $1 in the above code, it is like a placeholder for the input that the user will provide.To run this script, write bash git.sh 'initial commit'. Here the 'initial commit' will be placed at the $1.

Instead of writing the git commands manually, we automate the process by running the script. Let's take a deep dive into shell scripting.

Comments

Comments in shell scripts are followed by #.

Variables

Variables are declared using the =

MY_NAME='Morty' #No spaces              

Variables are accessed using the $ sign.

Echo

Echo is used to display the statement in the shell. It is like the printf in c.

MY_NAME='Morty'                
echo "My name is $MY_NAME"

Conditional Statements

Let's see how an if else statement looks in bash scripting.

if [[condition]];
then
# write the then instructions here
else
# write the else instructions here
fi

To know more about the bash syntax, check here .

To sum up what we learned, let's write a bash script that automates the heroku commands for creating a heroku app and pushing it to heroku.
Create a file deploy.sh.

The first thing we need to check is whether the app name is already taken or not. We can check it, by sending a request to the url of the app name, and check whether the app name exists or not by checking the response. CURL command allows us to do that. Curl command here provides us with a response. Let's see how,

CURL_RESPONSE=`curl -s -o /dev/null -w "%{http_code}" 'https://www.youtube.com/'`

If the response is 404, the app name doesn't exist. If the response is 404, then run the heroku commands else echo 'App already exists'. First step is to create the requested url using the app name.

HTTP='https://'
APP_NAME=$1
DOMAIN='.herokuapp.com'
REQ_URL=$HTTP$APP_NAME$DOMAIN                  
echo $REQ_URL

The next step would be to check the response using the curl command, as we did above. And then running the heroku commands on a 404 response. The full code will look like this

HTTP='https://'
APP_NAME=$1
DOMAIN='.herokuapp.com'
CURL_RESPONSE=`curl -s -o /dev/null -w "%{http_code}" $REQ_URL`
echo $CURL_RESPONSE                #404,200,502,000              
if [[ $CURL_RESPONSE -eq 404 ]];                #app doesn't exist              
then
heroku create $APP_NAME
git add .
git commit -m $2
git push heroku master
else
echo App already exists
fi

Run this :

bash deploy.sh firstapp "first commit"

This script can be used in any project you want to deploy on heroku. Shell scripts automates the whole process. Hope, you learnt some shell scripting.
And if you did, clap your heart out and follow me for more .

Tags

# heroku# shell-script# bash-script# shellscripting# kernel

Three Shells Bathroom

Source: https://hackernoon.com/know-shell-scripting-202b2fbe03a8

Posting Komentar

0 Komentar

banner