• Welcome to Bashguru

    Linux is one of popular version of UNIX operating System. It is open source as its source code is freely available. It is free to use. Linux was designed considering UNIX compatibility. It's functionality list is quite similar to that of UNIX and become very popular over the last several years. Our Basic motive is to provide latest information about Linux Operating system.

  • Python Programming

    Python is a comparatively simple programming language, compared to c++. Although some of the benefits of c++ are abstracted away in python, they are replaced with an overall easier to learn language with many “intuitive” features. For this reason it is common and recommended by most professionals that people new to programming start with python.

  • Perl Programming

    Perl is an open-source, general-purpose interpreted programming language. Used often for CGI, Perl is also used for graphics programming, system administration, network programming, finance, bioinformatics, and other applications. The Perl languages borrow features from other programming languages including C, shell scripting (sh), AWK, and sed. They provide powerful text processing facilities without the arbitrary data-length limits of many contemporary UNIX command line tools, facilitating easy manipulation of text files.

  • Android

    Android is an operating system based on the Linux kernel, and designed primarily for touch screen mobile devices such as smart phones and tablet computers. Android is a Linux-based software system, and similar to Linux, is free and open source software. This means that other companies can use the Android operating developed by Google and use it in their mobile devices.Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.

Sunday, December 26, 2010

Posted by venu k
310 comments | 7:36 AM

One thing that often confuses new users to the Unix / Linux shell, is
how to do (even very simple) maths. In most languages, x = x + 1 (or
even x++) does exactly what you would expect. The Unix/Linux shell is
different,however. It doesn’t have any built-in mathematical operators
for variables. It can do comparisons, but maths isn’t supported, not
even simple addition.

Shell script variables are by default treated as strings,not numbers,
which adds some complexity to doing math in shell script. To keep with
script programming paradigm and allow for better math support, langua-
ges such Perl or Python would be better suited when math is desired.
However, it is possible to do math with shell script.In fact, over the
years, multiple facilities have been added to Unix to support working
with numbers.

You can do maths using any one of the following methods.

1. Using expr command
2 Using $(()) construct.
3 Using let command
4 Using bc command.
5 Using $[] construct.


expr command:

expr command performs arithmetic operations on integers. It can
perform the four basic arithmetic operations, as well as the modulus
(remainder function).

$ expr 5 + 10
15
$ a=10 b=5
$ expr $a + $b
15
$ expr $a / $b
2
$ expr $a * $b
expr: syntax error
$ expr $a \* $b
50


The operand, be it +,-,* etc., must be enclosed on either side by
whitespace. Observe that the multiplication operand (*) has to be
escaped to prevent the shell from interpreting it as the filename meta
character. Since expr can handle only integers, division yields only
the integral part.

expr is often used with command substitution to assign a variable.
For example, you can set a variable x to the sum of two numbers:

$ x=`expr $a + $b`
$ echo $x
15

Note: As you can see, for expr, you must put spaces around the
arguments: "expr 123+456" doesn’t work. "expr 123 + 456" works.


With double parentheses: $(()) and (())

In bash version 3.2 and later you can (and should) use $(()) and (())
for integer arithmetic expressions. You may have may not have spaces
around the operators, but you must not have spaces around the equal
sign, as with any bash variable assignment.

$ c=$(($a+9))
$ echo $c
19
$ c=$((a+9)) #Also correct, no need of $ sign.
$ c=$((a + 9)) #Also correct, no restriction on spaces.
$ c= $((a + b)) #Incorrect, space after assignment operator.


You may also use operations within double parentheses without
assignment.

$ ((a++))
$ echo $a
11
$ ((a+=1)) ; echo $a
12
$ ((d=a+b+9)) ; echo $d
26
$ ((a+=$b)) #Correct
$ (($a+=1)) #Incorrect

let command:

The let command carries out arithmetic operations on variables. In
many cases, it functions as a less complex version of expr command.
As you can see, it is also a little picky about spaces, but it wants
the opposite of what expr wanted. let also relaxes the normal rule of
needing a $ in front of variables to be read.

$ let a=10 # Same as 'a=11'
$ let a=a+5 # Equivalent to let "a = a + 5"

# Quotes permit the use of spaces in variable assignment. (Double
# quotes and spaces make it more readable.)

$ let a=$a + 5 # Without quotes spaces not allowed.
bash: let: +: syntax error: operand expected (error token is "+")


You need to use quotes if you want to use spaces between tokens of
the expression, for example

$ let "a = a + 5";echo $a
20


The only construct that is more convenient to use with let is incre-
ment such as

$ let a++ ; echo $a # as well as to ((i++))
16


bc: THE CALCULATOR

Bash doesn't support floating point arithmetic. The obvious candidate
for adding floating point capabilities to bash is bc. bc is not only a
command, it also a pseudo-programming language featuring arrays,funct-
ions,conditional(if) and loops(for and while). It also comes with a
library for performing scientific calculations. It can handle very,
very large numbers. If a computation results in a 900 digit number, bc
will show each and every digit. But you have to treat the variables as
strings.

Here is what happens when we try to do floating point math with the
shell:

$ let a=12.5
bash: let: a=12.5: syntax error: invalid arithmetic operator (error
token is ".5")
$ ((b=1*0.5))
bash: ((: b=1*0.5: syntax error: invalid arithmetic operator (error
token is ".5")


I can't explain everything about bc here, it needs another post.
But I will give some examples here.

Most of the bellow examples follow a simple formula:

$ echo '57+43' | bc
100
$ echo '57*43' | bc
2451
$ echo '6^6' | bc # Power
46656
$ echo '1.5*5'|bc # Allows floating point math.
7.5

$[] construct:

$ x=85
$ y=15
$ echo $[x+y]
100
$ echo $[x/y]
5
$ c=$[x*y]
$ echo $c
1275


Working of above methods shell dependent. Bash shell supports all 5
methods. Following shell script demonstrates above methods.

#!/bin/bash
# SCRIPT: basicmath.sh
# USAGE: basicmath.sh
# PURPOSE: Addition, Subtraction, Division and Multiplication of
# two numbers.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Variable Declaration #
#####################################################################

clear #Clears Screen

Bold="\033[1m" #Storing escape sequences in a variable.
Normal="\033[0m"

echo -e "$Bold Basic mathematics using bash script $Normal\n"

items="1. ADDITTION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT"

choice=

#####################################################################
# Define Functions Here #
#####################################################################

# If didn't understand these functions, simply remove functions and
# its entries from main script.

exit_function()
{
clear
exit
}

#Function enter is used to go back to menu and clears screen

enter()
{
unset num1 num2
ans=
echo ""
echo -e "Do you want to continue(y/n):\c"
stty -icanon min 0 time 0

# When -icanon is set then one character has been received.
# min 0 means that read should read 0 characters.
# time 0 ensures that read is terminated the moment one character
# is hit.

while [ -z "$ans" ]
do
read ans
done

#The while loop ensures that so long as at least one character is
# not received read continue to get executed

if [ "$ans" = "y" -o "$ans" = "Y" ]
then
stty sane # Restoring terminal settings
clear
else
stty sane
exit_function
fi
}

#####################################################################
# Main Starts #
#####################################################################

while true
do
echo -e "$Bold \tPROGRAM MENU $Normal\n"
echo -e "\t$items \n"
echo -n "Enter your choice : "
read choice

case $choice in
1) clear
echo "Enter two numbers for Addition : "
echo -n "Number1: "
read num1
echo -n "Number2: "
read num2
echo "$num1 + $num2 = `expr $num1 + $num2`"
enter ;;
2) clear
echo "Enter two numbers for Subtraction : "
echo -n "Number1: "
read num1
echo -n "Number2: "
read num2
echo "$num1 - $num2 = $((num1-num2))"
enter ;;
3) clear
echo "Enter two numbers for Multiplication : "
echo -n "Number1: "
read num1
echo -n "Number2: "
read num2
echo "$num1 * $num2 = `echo "$num1*$num2"|bc`"
enter ;;
4) clear
echo "Enter two numbers for Division : "
echo -n "Number1: "
read num1
echo -n "Number2: "
read num2
let div=num1/num2
echo "$num1 / $num2 = $div"
enter ;;
5) exit_function ;;
*) echo "You entered wrong option, Please enter 1,2,3,4 or 5"
echo "Press enter to continue"
read
clear
esac

done

OUTPUT:
[venu@localhost ~]$ sh basicmath.sh

Basic mathematics using bash script

PROGRAM MENU

1. ADDITTION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT

Enter your choice : 1

Enter two numbers for Addition :
Number1: 123
Number2: 456
123 + 456 = 579

Do you want to continue(y/n):y

PROGRAM MENU

1. ADDITTION
2. SUBTRACTION
3. MULTIPLICATION
4. DIVISION
5. EXIT

Enter your choice : 3
Enter two numbers for Multiplication :
Number1: 12.5
Number2: 2
12.5 * 2 = 25.0

Do you want to continue(y/n):n

Wednesday, December 22, 2010

Posted by venu k
39 comments | 12:38 PM
  By definition in mathematics, the Fibonacci Numbers are the numbers
in the below sequence:
0,1,1,2,3,5,8,13,21,34,55,89,144, ......
By definition, the first two Fibonacci numbers are 0 and 1, and each
subsequent number is the sum of the previous two. Some sources omit
the initial 0, instead beginning the sequence with two 1s.
In mathematical terms, the sequence Fn of Fibonacci numbers is defi-
ned by the recurrence relation
Fn = Fn-1 + Fn-2,
with seed values
F0 = 0 and F1 = 1.

Iterative Method:


#!/bin/bash#!/bin/bash
# SCRIPT: fibo_iterative.sh
# USAGE: fibo_iterative.sh [Number]
# PURPOSE: Generate Fibonacci sequence.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Script Starts Here #
#####################################################################

if [ $# -eq 1 ]
then
Num=$1
else
echo -n "Enter a Number :"
read Num
fi

f1=0
f2=1

echo "The Fibonacci sequence for the number $Num is : "

for (( i=0;i<=Num;i++ ))
do
echo -n "$f1 "
fn=$((f1+f2))
f1=$f2
f2=$fn
done

echo

OUTPUT:
# sh fibo_iterative.sh 18
The Fibonacci sequence for the number 18 is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584
# sh fibo_iterative.sh
Enter a Number :20
The Fibonacci sequence for the number 20 is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

Recursive Method:


#!/bin/bash
# SCRIPT: fibo_recursive.sh
# USAGE: fibo_recursive.sh [Number]
# PURPOSE: Generate Fibonacci sequence.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Arguments Checking #
#####################################################################

if [ $# -eq 1 ]
then
Num=$1
else
echo -n "Enter a Number : "
read Num
fi

#####################################################################
# Define Functions Here #
#####################################################################

Fibonacci()
{

case $1 in
0|1) printf "$1 " ;;
*) echo -n "$(( $(Fibonacci $(($1-2)))+$(Fibonacci $(($1-1))) )) ";;
esac

#$(( )) construct is used instead of expr command for doing addition.
#$( ) constrict is used instead of back ticks.

}

#####################################################################
# Main Script Starts Here #
#####################################################################

echo "The Fibonacci sequence for the number $Num is : "

for (( i=0; i<=$Num; i++ ))
do
Fibonacci $i #Calling function Fibonacci
done

echo

OUTPUT:
# sh fibo_recursive.sh
Enter a Number : 11
The Fibonacci sequence for the number 11 is :
0 1 1 2 3 5 8 13 21 34 55 89
# sh fibo_recursive.sh 13
The Fibonacci sequence for the number 13 is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233

Be aware that recursion is resource-intensive and executes slowly,
and is therefore generally not appropriate to use in a script.

[root@localhost shell]# time ./fibo_iterative.sh 15
The Fibonacci sequence for the number 15 is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

real 0m0.008s
user 0m0.008s
sys 0m0.000s
[root@localhost shell]# time ./fibo_recursive.sh 15
The Fibonacci sequence for the number 15 is :
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

real 0m7.875s
user 0m0.908s
sys 0m5.188s

Too many levels of recursion may crash a script with a segfault.


Monday, December 20, 2010

Posted by venu k
16 comments | 12:08 PM

#!/bin/bash
# SCRIPT: armstrong_bw_range.sh
# USAGE: armstrong_bw_range.sh
# PURPOSE: Finding Armstrong numbers between given range.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#Armstrong numbers are the sum of their own digits to the power of
#the number of digits. As that is a slightly brief wording, let me
#give an example:
#153 = 1³ + 5³ + 3³
#Each digit is raised to the power three because 153 has three
#digits. They are totalled and we get the original number again!
#Notice that Armstrong numbers are base dependent,but we'll mainly be
#dealing with base 10 examples.The Armstrong numbers up to 5 digits
#are 1 to 9,153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084
#
#####################################################################
# Script Starts Here #
#####################################################################

echo -n "Enter the Lower Limit : "
read Start
echo -n "Enter the Upper Limit : "
read Ending

echo "Armstrong Numbers between $Start and $Ending are: "

while [ $Start -le $Ending ]
do
Number=$Start
Length=${#Number}
Sum=0
OldNumber=$Number

while [ $Number -ne 0 ]
do
Rem=$((Number%10))
Number=$((Number/10))
Power=$(echo "$Rem ^ $Length" | bc )
Sum=$((Sum+Power))
done

if [ $Sum -eq $OldNumber ]
then
echo -n "$OldNumber "
fi

let Start++
done
echo


OUTPUT:
# sh armstrong_bw_range.sh
Enter the Lower Limit : 1
Enter the Upper Limit : 500
Armstrong Numbers between 1 and 500 are:
1 2 3 4 5 6 7 8 9 153 370 371 407

# sh armstrong_bw_range.sh
Enter the Lower Limit : 1000
Enter the Upper Limit : 10000
Armstrong Numbers between 1000 and 10000 are:
1634 8208 9474
Posted by venu k
20 comments | 12:00 PM

#!/bin/bash
# SCRIPT: armstrongnumber.sh
# USAGE: armstrongnumber.sh
# PURPOSE: Check if the given number is Armstrong number ?
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
# Armstrong numbers are the sum of their own digits to the power of
# the number of digits. As that is a slightly brief wording, let me
# give an example:
# 153 = 1³ + 5³ + 3³
# Each digit is raised to the power three because 153 has three
# digits. They are totalled and we get the original number again!
#Notice that Armstrong numbers are base dependent,but we'll mainly be
# dealing with base 10 examples.The Armstrong numbers up to 5 digits
# are 1 to 9,153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727,93084
#
#####################################################################
# Script Starts Here #
#####################################################################

echo -n "Enter the number: "
read Number
Length=${#Number}
Sum=0
OldNumber=$Number

while [ $Number -ne 0 ]
do
Rem=$((Number%10))
Number=$((Number/10))
Power=$(echo "$Rem ^ $Length" | bc )
Sum=$((Sum+$Power))
done

if [ $Sum -eq $OldNumber ]
then
echo "$OldNumber is an Armstrong number"
else
echo "$OldNumber is not an Armstrong number"
fi


OUTPUT:
# sh armstrongnumber.sh
Enter the number: 8208
8208 is an Armstrong number
# sh armstrongnumber.sh
Enter the number: 4210818
4210818 is an Armstrong number
# sh armstrongnumber.sh
Enter the number: 4376541
4376541 is not an Armstrong number
Posted by venu k
10 comments | 6:28 AM

#!/bin/bash
# SCRIPT: dec2binary.sh
# USAGE: dec2binary.sh Decimal_Number(s)
# PURPOSE: Decimal to Binary Conversion. Takes input as command line
# arguments.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Script Starts Here #
#####################################################################

if [ $# -eq 0 ]
then
echo "Argument(s) not supplied "
echo "Usage: dec2binary.sh Decimal_number(s)"
else
echo -e "\033[1mDECIMAL \t\t BINARY\033[0m"

while [ $# -ne 0 ]
do
DecNum=$1
Binary=
Number=$DecNum

while [ $DecNum -ne 0 ]
do
Bit=$(expr $DecNum % 2)
Binary=$Bit$Binary
DecNum=$(expr $DecNum / 2)
done

echo -e "$Number \t\t $Binary"
shift
# Shifts command line arguments one step.Now $1 holds second argument
unset Binary
done

fi

#NOTE: Using bc command you can directly get output at command line.
# $ echo 'obase=2;15' | bc
# 1111
# $ echo 'obase=2;1023' | bc
# 1111111111
# $ echo 'obase=2;1024' | bc
# 10000000000
# No need of ibase=10, because default ibase is 10.

OUTPUT:
# sh dec2binary.sh 7 16 255 256 1023 1024
DECIMAL BINARY
7 111
16 10000
255 11111111
256 100000000
1023 1111111111
1024 10000000000

# sh dec2binary.sh 9223372036854775807
DECIMAL BINARY
9223372036854775807 1111111111111111111111111111111111111
11111111111111111111111111

#sh dec2binary.sh 9223372036854775808
DECIMAL BINARY
dec2binary.sh:line 13:[:9223372036854775808:integer expression expected
9223372036854775808

NOTE:My bash can handle upto 9223372036854775807.
But with bc command you can do more.

# echo 'obase=2;9223372036854775808'|bc
1000000000000000000000000000000000000000000000000000000000000000
Posted by venu k
10 comments | 6:22 AM

#!/bin/bash
# SCRIPT: binary2dec.sh
# USAGE: binary2dec.sh Binary_Numbers(s)
# PURPOSE: Binary to Decimal Conversion. Takes input as command line
# arguments.
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
#####################################################################
# Script Starts Here #
#####################################################################
if [ $# -eq 0 ]
then
echo "Argument(s) not supplied "
echo "Usage: binary2dec.sh Binary_Number(s)"
else
echo -e "\033[1mBINARY \t\t DECIMAL\033[0m"

while [ $# -ne 0 ]
do
Binary=$1
Bnumber=$Binary
Decimal=0
power=1

while [ $Binary -ne 0 ]
do
rem=$(expr $Binary % 10 )
Decimal=$((Decimal+(rem*power)))
power=$((power*2))
Binary=$(expr $Binary / 10)
done

echo -e "$Bnumber \t\t $Decimal"
shift
done

fi

#NOTE: Using bc command you can directly get output at command line.
# Here we're converting the binary number 111 to a base 10 (decimal)
# number.
#
# $ echo 'ibase=2;obase=A;111' | bc
# 7
#
#Note that the obase is "A" and not "10".The reason for this is you've
# set the ibase to 2, so if you now had tried to use "10" as the value
# for the obase, it would stay as "2", because "10" in base 2 is "2".
# So you need to use hex to break out of binary mode or use obase=1010
#
# $ echo 'ibase=2;obase=1010;10000' | bc
# 16
# $ echo 'ibase=2;obase=1010;1000001' | bc
# 65
#
# You can omit obase=A, because default obase is 10(decimal).
# $ echo 'ibase=2;1000001' | bc
# 65

OUTPUT:
# sh binary2dec.sh 100 1111 1111111 1000000
BINARY DECIMAL
100 4
1111 15
1111111 127
1000000 64

# sh binary2dec.sh 1111111111111111111
BINARY DECIMAL
1111111111111111111 524287

# sh binary2dec.sh 10000000000000000000
BINARY DECIMAL
binary2dec.sh:line 28:[:10000000000000000000:integer expression expected
10000000000000000000

NOTE:My bash can handle upto 9223372036854775807.
But with bc command you can do more.

# echo 'ibase=2;10000000000000000000'|bc
524288
# echo 'ibase=2;110000000000000000000'|bc
1572864

Sunday, December 19, 2010

Posted by venu k
82 comments | 11:25 AM

Method 1:


#!/bin/bash
# SCRIPT: validinteger.sh
# USAGE: validinteger.sh [ Input value to be validated ]
# PURPOSE: validate integer input, allow negative integers also
#
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
# Using the $? exit status variable or integer comparison operators, a
# script may test if a parameter contains only digits, so it can be
# treated as an integer.
#
#####################################################################
# Arguments Checking #
#####################################################################

if [ $# -eq 0 ]
then
echo -n "Enter input to test: "
read Number
else
Number=$1
fi

# You can also use bellow one liner
#[ $# -eq 0 ] && { echo -n "Enter input:";read Number; } || Number=$1

#####################################################################
# Main Script Starts Here #
#####################################################################

# Check the input is an integer or not

if [ $Number -ne 0 -o $Number -eq 0 2>/dev/null ]
then

# An integer is either equal to 0 or not equal to 0.
# 2>/dev/null suppresses error message.

echo "Supplied Input $Number is an Integer"

else

echo "Supplied Input $Number is not an Integer."
fi

# You can compare number itself also

# if [ $Number -eq $Number 2> /dev/null ]
# then
# echo "Supplied Input $Number is an Integer"
# else
# echo "Supplied Input $Number is not an Integer."
# fi

OUTPUT:
# chmod 755 validinteger.sh
# ./validinteger.sh -12345
Supplied Input -12345 is an Integer
# ./validinteger.sh 12345A
Supplied Input 12345A is not an Integer.
# ./validinteger.sh
Enter input to test: 876549
Supplied Input 876549 is an Integer
# ./validinteger.sh
Enter input to test: -345k123
Supplied Input -345k123 is not an Integer.

Method 2 :



#!/bin/bash
# SCRIPT: validinteger2.sh
# USAGE: validinteger2.sh [ Input value to be validated ]
# PURPOSE: validate integer input, allow negative integers also
#
#
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
# In this method if you want to allow negative integers, it gets a
# tiny bit more complicated. You must have basic knowledge of string
# manipulation methods.
#
#####################################################################
# Arguments Checking #
#####################################################################

if [ $# -eq 0 ]
then
echo -n "Enter input to test: "
read Number
else
Number=$1
fi

#####################################################################
# Main Script Starts Here #
#####################################################################

# Check first character is -

if [ "${Number:0:1}" = "-" ] #extract first character
then
RemNumber="${Number:1}" #extract except first char
else
RemNumber="$Number"
fi

# You can extract substring using bellow method also

#if [ "$(echo $Number|cut -c 1)" = "-" ]
#then
# RemNumber=$(echo $Number|cut -c 2-)
#else
# RemNumber="$Number"
#fi

if [ -z "$RemNumber" ]
then
echo "Supplied Input $Number is not an Integer"
else

NoDigits="$(echo $RemNumber | sed 's/[[:digit:]]//g')"

if [ -z "$NoDigits" ]
then
echo "Supplied Input $Number is an Integer"
else
echo "Supplied Input $Number is not an Integer."
fi
fi

OUTPUT:
# chmod 755 validinteger2.sh
# ./validinteger2.sh -987612
Supplied Input -987612 is an Integer
# ./validinteger2.sh -A987612
Supplied Input -A987612 is not an Integer.
# ./validinteger2.sh
Enter input to test: -123456789
Supplied Input -123456789 is an Integer
# ./validinteger2.sh
Enter input to test: A234K78
Supplied Input A234K78 is not an Integer.

Method 3:


#!/bin/bash
# SCRIPT: validinteger3.sh
# USAGE: validinteger3.sh [ Input value to be validated ]
# PURPOSE: validate integer input, allow negative integers also
#
# \\\\ ////
# \\ - - //
# @ @
# ---oOOo-( )-oOOo---
#
# In this method input value validated using ASCII value range of
# numbers (48 - 57).
# Sample script provided by Bond, Thank you Bond.
#
#####################################################################
# Arguments Checking #
#####################################################################

if [ $# -eq 0 ]
then
echo -n "Enter input to test: "
read Number
else
Number=$1
fi

# You can also use bellow one liner
#[ $# -eq 0 ] && { echo -n "Enter input:";read Number; } || Number=$1

#####################################################################
# Main Script Starts Here #
#####################################################################

# Check first character is - ?
# This is another method to extract substring using Substring Removal
# method.

if [ "${Number%${Number#?}}" = "-" ] #extract first char
then
RemNumber="${Number#?}" #extract all except first char
else
RemNumber="$Number"
fi

if [ -z "$RemNumber" ]
then
echo "Supplied Input $Number is not an Integer"
else

Length=${#RemNumber} # Counting length of the string

for ((i=0; i < Length; i++))
do

char=${RemNumber:$i:1} # Reads character one by one

code=`printf '%d' "'$char"` # Get ASCII value of char

# Compare code with ASCII value range of Intergers ( 48 - 57 )

if [ $code -lt 48 ] || [ $code -gt 57 ]
then
echo "Supplied Input $Number is not an Integer"
exit 1
fi

done

echo "Supplied Input $Number is an Integer."

fi

OUTPUT:
$ sh validinteger3.sh -1234
Supplied Input -1234 is an Integer.
$ sh validinteger3.sh -12-12
Supplied Input -12-12 is not an Integer
$ sh validinteger3.sh
Enter input to test: -
Supplied Input - is not an Integer
$ sh validinteger3.sh
Enter input to test: 123a456
Supplied Input 123a456 is not an Integer