Shell script to find whether an input number is palindrome or not

in Categories Academic, Shell Math last updated April 12, 2008

A palindrome is a word, phrase, number or other sequence of units that has the property of reading the same in either direction (the adjustment of punctuation and spaces between words is generally permitted). Composing literature in palindromes is an example of constrained writing. The word “palindrome” was coined from Greek roots palin (“back”) and dromos (“way, direction”) by English writer Ben Jonson in the 1600s.

A palindromic number is a number where the digits, with decimal representation usually assumed, are the same read backwards, for example, 58285. They are studied in recreational mathematics where palindromic numbers with special properties are sought. A palindromic prime is a palindromic number that is a prime number.

#!/bin/bash
# Shell script to find whether an input number is palindrome or not
# -----------------------------------------------
# Copyright (c) 2006 nixCraft <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
# store single digit
sd=0
 
# store number in reverse order
rev=""
 
# store original number
on=$n
 
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 ))  # get next digit
    # store previous number and current digit in reverse 
    rev=$( echo ${rev}${sd} ) 
done
 
if [ $on -eq $rev ];
then
  echo "Number is palindrome"
else
  echo "Number is NOT palindrome"
fi

Shell program to read a number and find the sum of digits

in Categories Academic, Shell Math last updated April 8, 2008
#!/bin/bash
# Shell program to read a number and find the sum of digits
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
# store single digit
sd=0
 
# store number of digit 
sum=0
 
# use while loop to caclulate the sum of all digits
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 ))  # get next digit
    sum=$(( $sum + $sd )) # calculate sum of digit
done
echo  "Sum of all digit  is $sum"

Shell program to read a number and reverse the number

in Categories Academic, Shell Math last updated April 8, 2008
#!/bin/bash
# Shell program to read a number and reverse the number
# for example 123 should output as 321
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
# store single digit
sd=0
 
# store number in reverse order
rev=""
 
# store original number
on=$n
 
# use while loop to caclulate the sum of all digits
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 ))  # get next digit
    # store previoues number and current digit in rev
    rev=$( echo ${rev}${sd} ) 
done
 
echo  "$on in a reverse order $rev"

Shell program to calculate the number of digits in a number read from the user

in Categories Academic, Shell Math last updated April 8, 2008
#!/bin/bash
# Shell program to calculate the number of digits in a
# number read from the user
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
echo -n "Enter number : "
read n
 
sd=0
 
# store number of digit 
nd=0
on=$n # store $n so that we can use it later
 
# use while loop to caclulate the number of digit
while [ $n -gt 0 ]
do
    sd=$(( $n % 10 )) # get Remainder 
    n=$(( $n / 10 )) 
    nd=$(( $nd + 1)) # calculate all digit in a number till n is not zero
done
echo  "Numnber of digit in a $on is $nd"

Shell program to display numbers from 1 to 10

in Categories Academic last updated April 8, 2008
#!/bin/bash
# Shell program to display numbers from 1 to 10
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
# using for loop
echo "Using for loop method # 1... "
for i in 1 2 3 4 5 6 7 8 9 10
do
  echo -n "$i " 
done
echo ""
 
# this is much better as compare to above for loop
echo "Using for loop method # 2... "
for (( i=1; i<=10; i++ ))
do
   echo -n "$i "
done
echo ""
 
# use of while loop
echo "Using while loop..."
j=1
while [ $j -le 10 ]
do
   echo -n "$j "
   j=$(( j + 1 )) # increase number by 1 
done
echo ""

Menu Driven Shell Script

in Categories Decision Making last updated April 5, 2008

Explains how to write a menu driven Shell script, which has following options:
1. Contents of /etc/passwd
2. List of users currently logged
3. Present handling directory (working directory)
4. Exit

Take action as per selected option

#!/bin/bash
# A menu driven Shell script which has following options
# Contents of /etc/passwd
# List of users currently logged
# Prsent handling directory
# Exit
# As per option do the job
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
while :
do
 clear
 echo "   M A I N - M E N U"
 echo "1. Contents of /etc/passwd"
 echo "2. List of users currently logged"
 echo "3. Prsent handling directory"
 echo "4. Exit"
 echo -n "Please enter option [1 - 4]"
 read opt
 case $opt in
  1) echo "************ Conents of /etc/passwd *************";
     more /etc/passwd;;
  2) echo "*********** List of users currently logged";
     who | more;;
  3) echo "You are in $(pwd) directory";   
     echo "Press [enter] key to continue. . .";
     read enterKey;;
  4) echo "Bye $USER";
     exit 1;;
  *) echo "$opt is an invaild option. Please select option between 1-4 only";
     echo "Press [enter] key to continue. . .";
     read enterKey;;
esac
done