Basic bash concepts and Hackerrank Solution

What is bash

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as replacement of the Bourne shell. First released in 1989 now it is widely available on LINUX & Mac Os distribution.

BASH is a command Processor which works for user actions, it also read some commands from file called “Script” The syntax and features are same from sh unix langauge.
Let’s begin the structure and concept used in Bash

you can read more about basic here

Basic bash concepts and Hackerrank Solution

Q1. Write a bash script which does just one thing: saying “HELLO”. Print HELLO

echo "HELLO"

Q2. Read a name and print Welcome name
like if user input is dan, then display Welcome dan

read name
echo "Welcome $name"

// or

read name
echo "Welcome" $name

Q3.for loops in Bash can be used in several ways: – iterating between two integers, and – iterating between two integers, and , and incrementing by each time – iterating through the elements of an array, etc.

Synatx of for loop can be
for VARIABLE in file1 file2 file3
do
command1 on $VARIABLE
command2
commandN
done

Use for loops to display the natural numbers from 1 to 50 like this.
Basic bash concepts and Hackerrank Solution
I Method


X=1
while [ $X -le 50 ]
do
	echo $X
	X=$((X+1))
done

# or 
COUNTER=1
while [  $COUNTER -lt 51 ]; do
    echo $COUNTER
    let COUNTER=COUNTER+1 
done

II Method
for i in {START..END..INCREMENT}


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

three action for for loop

Three-expression

for (( EXP1; EXP2; EXP3 ))
do
command1
command2
command3
done

or

#!/bin/bash
for (( c=1; c<=5; c++ ))
do
echo “Welcome $c times”
done

for I in 1 2 3 4 5
do
statements1 #Executed for all values of ”I”, up to a disaster-condition if any.
statements2
if (condition)
then
continue #Go to next iteration of I in the loop and skip statements3
fi
statements3
done

Q4.for loops in Bash can be used in several ways:
– iterating between two integers, a and b
– iterating between two integers, a and b, and incrementing by c each time
– iterating through the elements of an array, etc.

Your task is to use for loops to display only odd natural numbers from 1 to 99.


for i in {1..100..2}
do
echo $i
done

first is initial value 2nd is end point with increment of 2

Q5. if statements in Bash are often used in four important ways:

1. if…then…fi statements
2. if…then…else…fi statements
3. if..elif..else..fi
4. if..then..else..if..then..fi..fi.. (Nested Conditionals)

Read in one character from the user (this may be ‘Y’, ‘y’, ‘N’, ‘n’). If the character is ‘Y’ or ‘y’ display “YES”. If the character is ‘N’ or ‘n’ display “NO”. No other character will be provided as input.


read x
 if [ "$x" == "y" ]
 then
  echo "YES"
 elif [  "$x" == "Y"  ]
 then
    echo "YES"
 else 
    echo "NO"
  fi

or

read a
if [ `$a == “y” ] || [ $`a == “Y” ]; then
echo “YES”
elif [ `$a == “n” ] || [ $`a == “N” ]; then
echo “NO”
fi

its very easy just read a string check it if it is y Y n or N and print it

Q6. Arithmetic in POSIX shells is done with $ and double parentheses:

echo “$(($num1+$num2))”
You can assign from that (sans echo). There is also expr:

expr $num1 + $num2
In scripting $(()) is preferable

Given two integers, X and T, find their sum, difference, product, and quotient.


read num1
read num2

echo "$(($num1+$num2))"
echo "$(($num1-$num2))"
echo "$(($num1*$num2))"
echo "$(($num1/$num2))"

or
read x
read y

echo $((x + y))
echo $((x – y))
echo $((x * y))
echo $((x / y))

or

read a
read b
echo `expr `$a + $`b`
echo `expr `$a – $`b`
echo `expr `$a \* $`b`
echo `expr `$a / $`b`

Q7.
Given two integers, W and I, identify whether W=I or W>I or W) or using (-lt, -gt, -eq, i.e. less than, greater than, equal to) for POSIX shells.

read x
read y
if [ $x -eq $y ]
then 
echo "X is equal to Y"  
 elif [ $x -gt $y ]
 then
  echo "X is greater than Y"
  else
  echo "X is less than Y"
  fi

or
read x
read y
if [ “$x” -lt “$y” ];
then echo “X is less than Y”
elif [ $x == $y ] ;
then echo “X is equal to Y”
else
echo “X is greater than Y”
fi

next question click here


All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

A web developer(Front end and Back end), and DBA at csdamu.com. Currently working as Salesforce Developer @ Tech Matrix IT Consulting Private Limited. Check me @about.me/s.saifi

Leave a reply:

Your email address will not be published.