Comparing Numbers

Problem Statement

Given two integers, X and Y, identify whether X<Y or X>Y or X=Y.

Comparisons in a shell script may either be accomplished using regular operators (such as < or >) or using (-lt, -gt, -eq, i.e. less than, greater than, equal to) for POSIX shells. This discussion on stack overflow contains useful information on this topic.

Input Format
Two lines containing one integer each (X and Y, respectively).

Output Format
Exactly one of the following lines:
X is less than Y
X is greater than Y
X is equal to Y

Sample Input 1

5  
2  

Sample Output 1

X is greater than Y  

Sample Input 2

2
2  

Sample Output 2

X is equal to Y   

Sample Input 3

2
3  

Sample Output 3

X is less than Y  

Reference Resources
A relevant and interesting discussion on Stack-Exchange for those getting started with handling numbers and arithmetic operations in Bash.

These discussions on stack overflow and geeksforgeeks contains useful information on this topic.

Solution

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

The World of Numbers

Problem Statement

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

Input Format
Two lines containing one integer each (X and Y, respectively).

Input Constraints
100X,Y100
Y0

Output Format
Four lines containing the sum (X+Y), difference (XY), product (X×Y), and quotient (XY), respectively.
(While computing the quotient, print only the integer part.)

Sample Input

5  
2

Sample Output

7
3
10
2

Reference Resources
A relevant and interesting discussion on Stack-Exchange for those getting started with handling numbers and arithmetic operations in Bash.

Solution

  
read a
read b
echo $(($a+$b))
echo $(($a-$b))
echo $(($a*$b))
echo $(($a/$b))


Looping with Numbers

Problem Statement

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.

Use _for: loops to display the natural numbers from 1 to 50.

Input
There is no input

Output

1
2
3
4
5
.
.
.
.
.
50  

Recommended Resources

A quick but useful tutorial for Bash newcomers is here.
Handling input is documented and explained quite well on this page.
Different ways in which for loops may be used are explained with examples here.

Solution

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

A Personalized Echo

Problem Statement

Write a Bash script which accepts name as input and displays a greeting: “Welcome (name)”

Input Format
One line, containing a name.

Output Format
One line: “Welcome (name)” (quotation marks excluded).
The evaluation will be case-sensitive.

Sample Input 0

Dan  

Sample Output 0

Welcome Dan  

Sample Input 1

Prashant

Sample Output 1

Welcome Prashant

A quick introduction to ‘Echo’

This is the equivalent of common output commands in most programming language (print or puts statements).

For Example:

echo "Greetings"

This outputs just one word “Greetings” (without the quotation marks).

echo "Greetings `$USER, your current working directory is $`PWD"

This picks up the values of the environment variables USER and PWD and displays something like:

Greetings prashantb1984, your current working directory is /home/prashantb1984  

The above message, of course, will vary from system to system, depending on the setting of environment variables.

Accepting Inputs

This can be accomplished using the read statement.

read number
echo "The number you have entered is $number"  

If the user provides an input 4, the output is:

The number you have entered is 4  

Consider this snippet:

read name
echo "The name you have entered is $name"  

If the user provides an input Max, the output is:
The name you have entered is Max

Recommended Resources

A quick but useful tutorial for Bash newcomers is here.
Handling input is documented and explained quite well on this page.

Solution

  
read name
echo "Welcome $name"

Looping and Skipping

Problem Statement

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.

Input
There is no input.

Output

1
3
5
.
.
.
.
.
99  

Recommended Resources

A quick but useful tutorial for bash newcomers is here.
Handling input is documented and explained quite well on this page.
Different ways in which for loops may be used are explained with examples here.

Solution

  
for (( c=1; c<=100; c+=2 ))
do
   echo "$c"
done

Let’s Echo

Problem Statement

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

Input Format
There is no input file required for this problem.

Output Format
HELLO

Sample Output
HELLO

A quick introduction to ‘Echo’
This is the equivalent of common output commands in most programming language (print or puts statements).

For Example:

echo "Greetings"

This outputs just one word “Greetings” (without the quotation marks).

echo "Greetings $USER, your current working directory is $PWD"

This picks up the values of the environment variables $USER and $PWD and displays something like:

Greetings prashantb1984, your current working directory is /home/prashantb1984  

The above message, of course, will vary from system to system, depending on the setting of environment variables.

Recommended Resource
A quick but useful tutorial for bash newcomers is here.

Solution

  
echo "HELLO"