Problem Question
Write a script to display the given number in reverse order.
Explanation of Problem
Here we wish to write a Linux Shell Script that would accept one command line parameter (integer) and reverse it. A sample run should yield the output of the following kind:
sh ReverseNumber.sh 123
the output should be 321.
Code
#*ReverseNumber*
#@Shell: Bash
#@Author: Toxifier
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 20-03-2014
n=$1
a=""
while [ $n -gt 0 ]
 do
  a=$( echo ${a}$(( $n % 10)) )
  n=$(( $n / 10))
 done
echo $1 in Reverse Order is $a
Explanation of Code
Please note the number of whitespaces in the above script. In Linux Shell Scripts, a single extra whitespace could lead to hours of unnecessary debugging like a missing semicolon in a C program.
n=$1 -> -> We save the command line argument into a variable named ‘n’. This variable is required since we are going to use it to segregate the digits by recursively dividing it with 10 and storing the remainder.
a="" -> ‘a’ is our string variable here which is going to hold he answer, the reverse number.
while [ $n -gt 0 ] -> Our main loop of the program which is going to extract the digits one by one from the variable ‘n’ and store it into our variable ‘a’. For this purpose we us the line a=$( echo ${a}$(( $n % 10)) ) where we do the following –
The value of the last digit is extracted by applying modulus function to the number. We use the echo command to display the current value of variable ‘a’ followed by this extracted digit. But since we enclose this echo inside another $, we store the result of the echo to the variable ‘a’ rather than displaying it on the terminal. Thus we build up the string ‘a’ holding the reverse of the number as we traverse through the loop.
n=$(( $n / 10)) -> Since we have used the last digit in the above line, thus we reject it using this line of code so that we are left with the remaining part of the number.
do done -> While loop block delimiters.
echo $1 in Reverse Order is $a -> We this time use the echo command of the linux shell to display the output to the screen. The output, i.e. the reverse number is stored in variable ‘a’ while ‘$1’ denotes the command line argument, i.e. our original number.
Output(s)

Problem Question
Write a script to calculate the sum of digits of the given number.
Explanation of Problem
Here we wish to write a Linux Shell Script that would accept one command line parameter (integer) and find the sum of it’s digits. Thus, in a sample run like:
sh SumOfDigits.sh 123
the output should be 6.
Code
#*SumOfDigits*
#@Shell: Bash
#@Author: Toxifier
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 19-03-2014
x=$1
a=0
b=0
while [ $x -gt 0 ]
 do
  a=$(( $x % 10 ))
  x=$(( $x / 10 ))
  b=$(( $b + $a ))
 done
echo Sum of digits of $1 is $b
Explanation of Code
Please note the number of whitespaces in the above script. In Linux Shell Scripts, a single extra whitespace could lead to hours of unnecessary debugging like a missing semicolon in a C program.
x=$1 -> We save the command line argument into a variable named ‘x’. This variable is required since we are going to use it to segregate the digits by recursively dividing it with 10 and storing the remainder.
a=0 b=0 -> We use the variable ‘a’ to store the remainder using a=$(( $x % 10 )) and variable ‘b’ to store the sum using b=$(( $b + $a )).
while [ $x -gt 0 ] -> We use this while loop to do the operations stated above. Apart from that, we divide ‘x’ by 10 using x=$(( $x / 10 )) everytime in the loop and store the answer in ‘x’ because we want to reject the last digit as soon as we have stored it in variable ‘a’.
do done -> While loop block delimiters.
echo Sum of digits of $1 is $b -> echo command of the linux shell is then used to display the string following it. ‘$1’ implies the first command line argument, and ‘$b’ the sum we were required to calculate.
Output(s)

Problem Question
Write a script to calculate the factorial of a given number
Explanation of Problem
Here we wish to write a Linux Shell Script that would accept one command line parameter and find the factorial of that number.
Code
#*Factorial*
#@Shell: Bash
#@Author: Toxifier
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 18-03-2014
fact=1
for (( i=1;i<=$1;i++ ))
 do
  fact=$(($fact*$i))
 done
echo Factorial of $1 is $fact
Explanation of Code
Please note the number of whitespaces in the above script. In Linux Shell Scripts, a single extra whitespace could lead to hours of unnecessary debugging like a missing semicolon in a C program.
fact=1 -> We initialise a variable ‘fact’ that would hold the value of the final answer. We initialise it to one because we are going to multiply it recursively (1 being the multiplicative identity).
for (( i=1;i<=$1;i++ )) -> Our main loop where we multiply the number(say n) with all numbers from 1 to (n – 1). We do this by using fact=$(($fact*$i)), where the variable ‘fact’ is multiplied by the loop counter (‘i’) and at the same time we store the value in ‘fact’ itself since we have to multiply it with more numbers(probably). The loop counter ‘i’ helps by taking the values ‘1’ to ‘n – 1’ to be multiplied by the number whose factorial was to be found.
do done -> The for loop block delimiters.
echo Factorial of $1 is $fact -> echo command of the linux shell used to display the string following it to the standard output display of the terminal. ‘$1’ represents he first command line argument (the number whose factorial is to be calculated).
Output(s)

Problem Question
Write a script to print the Fibonacci series upto n terms
Explanation of Problem
Here we wish to write a Linux Shell Script that takes a command line parameter which denotes upto which term we wish to generate Fibonacci series, and generate the same.
Code
#*Fibonacci Series*
#@Shell: Bash
#@Author: Toxifier
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 17-03-2014
f1=0
f2=1
echo""
echo "Fibonacci sequence for n=$1 is : "
echo""
for (( i=0;i<=$1;i++ ))
 do
  echo -n "$f1 "
  fn=$((f1+f2))
  f1=$f2
  f2=$fn
 done
echo " "
echo""
Explanation of Code
Please note the number of whitespaces in the above script. In Linux Shell Scripts, a single extra whitespace could lead to hours of unnecessary debugging like a missing semicolon in a C program.
f1=0 f2=1 -> The first two terms of the Fibonacci Series(fixed).
echo"" -> Used to print a blank line
$1 -> The first command line argument
echo -> Linux shell command used to display the string following it to the standard output terminal display.
for (( i=0;i<=$1;i++ )) -> For loop used to loop with the code that calculates and prints the next term.
echo -n "$f1 " -> The ‘-n’ option of echo command makes the output to print in the same line. Please note a single whitespace after ‘$f1’ that I have used so that the terms are well separated.
fn=$((f1+f2)) -> We declare a variable ‘fn’ and assign it the value of expression ‘(f1+f2)’
f1=$f2 f2=$fn -> f1 is given the value of f2, and f2 the value of the new term we just found. Actually in Fibonacci series, the new term is calculated as the sum of previous two terms. Thus, we need a track of previous two terms, for which we use f1 and f2.
do done -> for loop delimiters
Output(s)

Problem Question
Write a script to check whether the given number is prime or not
Explanation of Problem
Here we wish to write a Linux Shell Script that would take a command line argument and check if that number is prime or not and print the result (PRIME or NOT PRIME) on the terminal.
Code
#*Check Prime*
#@Shell: Bash
#@Author: Toxifier
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 16-03-2014
x=`expr $1 / 2`
i=2
while [ $i -le $x ]
 do
  if [ `expr $1 % $i` -eq 0 ]
   then
    echo Not Prime
    exit
  fi
  i=`expr $i + 1`
 done
echo Prime
Explanation of Code
Please note the number of whitespaces in the above script. In Linux Shell Scripts, a single extra whitespace could lead to hours of unnecessary debugging like a missing semicolon in a C program.
x=`expr $1 / 2` -> We declare a variable ‘x’ and assign it a value which is equal to half that of the first command line argument. This will help us know where to stop our loop which will divide the number with every integer until it finds the remainder of the division to be zero. Thus we can limit the loop counter to half the value of the number to be checked. Thus we use while [ $i -le $x ] as our loop.
i=2 -> This is our loop counter. We start at 2 since every number is divisible by 1.
if [ `expr $1 % $i` -eq 0 ] -> As soon as we find the remainder of the division is zero, we use echo Not Prime to display Not Prime on the terminal, and with the exit command we close our shell running the script, i.e., the script terminates.
i=`expr $i + 1` -> Loop counter incremented by one.
echo Prime -> If the control reaches here, it means that the number is not prime since it was not divisible by any of the numbers (upto half the number itself). Thus, we print Prime on the terminal.
then fi -> if block delimiters.
do done -> while block delimiters.
Output(s)

Problem Question
Write a script to calculate the average of n numbers
Explanation of Problem
Here we wish to write a Linux Shell Script which when executed any number of command line arguments, calculates their average(integer value only). So if the script is executed as the following, the outputs would be as under.
sh AverageCalculator.sh
ERROR
sh AverageCalculator.sh 1
1
sh AverageCalculator.sh 1 2
1
sh AverageCalculator.sh 1 2 3
2
sh AverageCalculator.sh 1 2 3 4 5 23 234 234 234 23 234 235 123 14
97
Code
#*Average Calculator*
#@Shell: Bash
#@Author: Toxifier
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 14-03-2014
sum=0
for i in $*
 do
  sum=`expr $sum + $i`
 done
avg=`expr $sum / $#`
echo $avg
Explanation of Code
Please note the number of whitespaces in the above script. In Linux Shell Scripts, a single extra whitespace could lead to hours of unnecessary debugging like a missing semicolon in a C program.
sum=0 -> We declare a variable 'sum' and initialize it to value zero. We will be using it for holding the sum of all the values passed as command line arguments.
for i in $* -> Here we initialize a for loop which travels through the whole list in the command line. '$*' imply the list of command line arguments. 'i' is the loop counter variable we have introduced. It could have been anything else than 'i' also.
sum=`expr $sum + $i` -> We tell the shell to store the value of the expression, (sum + i) in the variable sum. We repeat this since we are in a 'for' loop. Thus we get the sum of all the numbers in the command line arguments list.
avg=`expr $sum / $#` -> '$#' implies the number of command line arguments. Thus, we introduce a new variable named 'avg' that would hold the integer value returned by the division of 'sum' and '$#'.
echo $avg -> echo command writes the value following it to the standard display terminal.
do done -> do and done are the delimiters of the loop block. They can be understood similar to the {}braces in C/C++.
Output(s)

Problem Question
Write a script to check whether the given no. is even/odd
Explanation of Problem
Here we wish to write a Linux Shell Script that checks if the first number passed as command line argument is even or odd. So when executing the script EvenOdd.sh as under:
sh EvenOdd.sh 1
should give the answer as 1 is odd
and when run as
sh EvenOdd.sh 2
should give the output as 2 is even
Code
#*Even Odd Check*
#@Shell: Bash
#@Author: Toxifier
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 13-03-2014
x=`expr $1 % 2`
if [ $x -eq 0 ]
  then
   echo $1 is even
else
 echo $1 is odd
fi
Explanation of Code
Please note the number of whitespaces in the above script. In Linux Shell Scripts, a single extra whitespace could lead to hours of unnecessary debugging like a missing semicolon in a C program.
x=`expr $1 % 2` -> We introduce a new variable 'x' to our shell. Then, we tell it to assign 'x' the value that follows the '=' sign. The shell then encounters `` which makes it understand the expression inside will return something that has to be assigned to 'x'. Then we tell it that this is an expression, by using the keyword 'expr' which is followed by the expression. Note that the whole expression is quoted as `expr YOUR EXPRESSION`. Then we assign the expression as Command Line Argument 1(implied by $1) modulus 2. We apply the modulus operator (%) and thus we find the remainder when we divide $1 by 2.
if [ $x -eq 0 ] -> Here we have our if block. The condition says if $x is zero. Note that now we use a $ sign with x since the variable is already declared. If a number is even, it will give a remainder zero when divided by 2, else not. This is what we check.
then -> follows the code that has to executed in case the if condition is true. That means the remainder on dividing the first command line argument with 2 is zero. Thus we print $1 is even.
else -> Starts our else block executed in case the if condition fails. That means the remainder ain't zero when we divide $1 by 2. Thus we print $1 is odd.
echo -> Linux shell command used to print to standard output, the terminal display.
fi -> End of the if block.
Output(s)

Problem Question
Write a script to find the greatest of three numbers (numbers passed as command line parameters)
Explanation of Problem
We need to write a Linux Shell Script that when executed, will find the greatest of the three numbers. These numbers should be passed as command line arguments. This means that, if the name of our script is GreatestOfThree.sh, then we execute it as under to get the result:
sh GreatestOfThree.sh 23 121 1
and the result should be 121
Code
#*Greatest of Three Numbers*
#@Shell: Bash
#@Author: Toxifier
#@URL: http://letsplaycoding.blogspot.com/
#@Date: 12-03-2014
if [ $1 -gt $2 ]
 then
  if [ $1 -gt $3 ]
  then
   echo $1 is the greatest
  else
   echo $3 is the greatest
fi
else
 if [ $2 -gt $3 ]
  then
   echo $2 is the greatest
  else
   echo $3 is the greatest
 fi
fi
Explanation of Code
Please note the number of whitespaces in the above script. In Linux Shell Scripts, a single extra whitespace could lead to hours of unnecessary debugging like a missing semicolon in a C program.
$1, $2, ... $n -> The nth command line argument
if [ x -gt y ] -> -gt check if the argument before it is greater than the one that follows it.
then -> In case the 'if' condition passes, this part of code is executed that follows 'then'
else -> In case the 'if' condition fails, this part of code is executed that follows 'else'
fi -> Closes the 'if' or 'if-else' block
In the code above what we did is, first we checked if the first command line argument is greater than the second, if yes, we check if it is greater than the third. If this is also true, we print the first argument using '$1' followed by the phrase is the greatest. If the second if block fails, it means that $1>$2, but $1<$3 => $3 is the greatest. Then, we print the third argument using '$3' followed by the phrase is the greatest.
If the first if block fails, then we compare second and third argument. After testing again, we do the same thing as above.
Output(s)
