• 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 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

82 comments:

  1. Hi Venu,

    Just another version to server the same purpose using comparison with ASCII chart:

    #!/bin/bash

    echo -e "\nScript to validate whether input is an Interger value of not"

    if [ $# -ne 1 ]
    then
    echo "Usage: $0 [Input Value to be validated]"
    exit
    fi

    str="$1"
    cnt=${#str} # Counting the length of string fetched

    for ((i=0; i < cnt; i++))
    do
    char=${str:$i:1} # Reading one character at a time from the input string

    code=`printf '%d' "'$char"` # Echo the ASCII value of character

    echo "code: " $code
    if [ $code -lt 48 ] || [ $code -gt 57 ] # Comparing the ASCII value range of Intergers ( 48 - 57 )
    then
    if [ $code -ne 45 ] # To accept the negative integer value as well
    then
    echo -e "Input is not an integer value\n"
    exit
    fi
    fi
    done

    echo -e "\nThe input is valid integer value\n"

    ReplyDelete
  2. Hi Bond,

    Your idea is good, but above script has a mistake.

    $ sh validintegernum.sh -12-12

    Script to validate whether input is an Integer value of not
    code: 45
    code: 49
    code: 50
    code: 45
    code: 49
    code: 50

    The input is valid integer value

    It accepts hyphen any where. I rectified it and posted as method 3.

    ReplyDelete
  3. From Method 1, how would I adapt

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

    so that it worked in a while loop so that I could keep prompting the user for a number until they entered one?

    ReplyDelete
  4. the ultimate numeric testing:

    #!/bin/bash

    n="$1"

    echo "Test numeric '$n' "
    if ((n)) 2>/dev/null; then
    n=$((n))
    echo "Yes: $n"
    else
    echo "No: $n"
    fi

    ReplyDelete
    Replies
    1. That accepts undesirable mathematical expressions as input too.

      E.g. try evaluating the string "1234 != 4321".

      Delete
  5. Samples:
    Test numeric 'foo'
    No: foo
    Test numeric '2'
    Yes: 2
    Test numeric '-7'
    Yes: -7
    Test numeric '+8'
    Yes: 8
    Test numeric '2**7'
    Yes: 128
    Test numeric '3+4+5'
    Yes: 12
    Test numeric '20%14'
    Yes: 6
    Test numeric '+'
    No: +
    Test numeric '-'
    No: -
    Test numeric '%'
    No: %

    ReplyDelete
  6. Maybe better use something like this:
    [[ $1 =~ ^-{0,1}[0-9]+$ ]] && echo 'It's a integer'
    or
    [[ $1 =~ ^-{0,1}[[:digit:]]+$ ]] && echo 'It's a integer'
    or
    [[ $1 =~ ^-{0,1}[[:xdigit:]]+$ ]] && echo 'It's a hex integer'

    ReplyDelete
  7. @Anonymous the 1st

    Great, but doesn't detect 0

    if ((${n}1)) 2>/dev/null

    works better to me :)

    ReplyDelete
    Replies
    1. Alex: Your logic too has bug. It doesnt detect null or space.
      for example n="" or n=" "

      Delete
  8. case $1 in
    *[!0-9]* | "") echo Not integer ;;
    *) echo Integer ;;
    esac

    ReplyDelete
  9. The simplest and most elegant solution is the section commented out in Method 1 above, but with the variable in quotes to take care of null value:

    # You can compare number (to) 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

    This takes care of all the possible variations (including "-12-12") and uses a test that is absolutely specific - "Can we carry out arithmetic operations on the variable or not?". If we can then it is a number, otherwise it isn't.

    ReplyDelete
    Replies
    1. Verification and validation techniques applied throughout the development process enable you to find errors before they can derail your project.
      they are involved in lot of process improvements which will actually help the clients to deliver best software.

      software validation

      Delete
  10. hi ,

    I have doubt in passing input values to the script.please some one help me?

    ReplyDelete
  11. I just wanted to comment your blog and say that I really enjoyed reading your blog post here.
    It was very informative and I also digg the way you write!I also provide this service u can visit my site.

    software validation

    ReplyDelete
  12. I really like examining and also following ones write-up when i locate them incredibly beneficial and also fascinating.
    That write-up is usually just as beneficial along with fascinating.Verification and Validation both are independent type of testing. Obviously,
    If we look both of these activities as a whole, we can also call it testing.

    software validation

    ReplyDelete
  13. I stumbled upon this topic via Google. Very interesting view on subject. Thanks for sharing.

    ReplyDelete
  14. Just one way that you can improve your chances of success. After All things considered, these devices are accessible so you ought to exploit them when you can.best project management app

    ReplyDelete
  15. there are other more included alternatives too. With a CMS, you don't need to know HTML, and you can set up a page rapidly and effortlessly.content management system

    ReplyDelete
  16. Such a great post and its give me big piece of information about math scripts ad its really helped him thanks for share it project management homework help .

    ReplyDelete
  17. Today i learned a new concept which is help me to update new things and i like those math script what you have posted.




    abap training in chennai

    ReplyDelete
  18. Its an extremely helpful and osm guide and helpful info cover-all that things.I am a large lover of the website. I love how a post is offered. Aspire to discover selenium internet motorist . For more info on this go here

    ReplyDelete
  19. Nice post, Thanks for sharing such a useful post.

    seo training in Chennai

    ReplyDelete
  20. It's quite helpful to learn about integeration method using shell script.
    Regards,
    Software testing training|Software training|Software testing training in chennai

    ReplyDelete
  21. MSBI accreditations being the best and most eminent virtualization confirmations, is viewed as the pioneer advancements and driving supplier of virtualization items and answers for organizations and IT industry. | PHP Training | VMWare Training | MSBI Training | Chef Training | OpenStack Training

    ReplyDelete
  22. Thanku for this details about validate integer nice posts..

    sap fiori training in hyderabad


    ReplyDelete
  23. Great article. This is very useful post. Thanks for sharing.

    PHP Course in Chennai

    ReplyDelete
  24. Wonderful post. I learned some new things. Thanks for sharing.

    seo training in chennai

    ReplyDelete
  25. great article.you have post very informative blog.thank you for sharing this blog.
    codedUI training

    ReplyDelete
  26. Helpful post.Thanks for taking time to share this informative post to my vision.
    Regards,
    VMware courses in Chennai | VMware Training institutes in Chennai

    ReplyDelete
  27. Wonderful post. I learned some new things. Thanks for sharing.
    java training in chennai

    ReplyDelete
  28. This comment has been removed by the author.

    ReplyDelete
  29. The best thing is that your blog really informative thanks for your great information!
    ERP Software Solutions in Chennai

    ReplyDelete
  30. Thanks for sharing this information, Hi we at Colan Infotech Private Limited , a company which is Situated in US and India, will provide you best service and our talented team will assure you best result and we are familiar with international markets, We work with customers in a wide variety of sectors. Our talented team can handle all the aspects of custom application development, we are the best among the dot net development companies in Chennai . asp .net web development company We have quite an extensive experience working with asp .net development services. we are the only asp.net web development company which offer custom services to a wide range of industries by exceeding our client’s expectations. You can even interact directly with the team regarding your project, just as you would with your in-house team. hire asp.net programmers to achieve your dream product.
    Custom application development company, asp.net development companies, asp .net web development company,Hire asp .net programmers,asp.net web development services,dot net development companies in chennai Hire asp .net programmers/ hire asp .net developer  . We are the best asp .net development company providing top notch asp .net development services Hire asp .net programmers/ hire asp .net developer  . We are the best asp .net development company providing top notch asp .net development services.

    ReplyDelete



  31. Thanks for posting useful information.You have provided an nice article, Thank you very much for this one. And i hope this will be useful for many people.. and i am waiting for your next post keep on updating these kinds of knowledgeable things...Really it was an awesome article...very interesting to read..
    please sharing like this information......
    Android training in chennai
    Ios training in chennai

    ReplyDelete

  32. It's interesting that many of the bloggers your tips helped to clarify a few things for me as well as giving.. very specific nice content. And tell people specific ways to live their lives.Sometimes you just have to yell at people and give them a good shake to get your point across.
    Web Design Company
    Web Development Company
    Mobile App Development Company

    ReplyDelete
  33. Good and nice information, thanks for sharing your views and ideas.. keep rocks and updating

    Software Testing Training in chennai | Software Testing Training institute in chennai

    ReplyDelete
  34. Hi, i really enjoyed to read your article.. i got clear idea through your views and ideas.. thanks for sharing your post..


    Android Training in chennai

    IOS Training in chennai

    Dot Net Training in chennai

    ReplyDelete
  35. These ways are very simple and very much useful, as a beginner level these helped me a lot thanks fore sharing these kinds of useful and knowledgeable information.
    Texting API
    Text message marketing
    Digital Mobile Marketing
    Sms API
    Sms marketing

    ReplyDelete
  36. Nice and informative post..Thanks for sharing this helpful blog.
    Best BE | B.Tech Project Center in Chennai | ME | M.Tech Project Center in Chennai | Best IT Project Center in Chennai

    ReplyDelete
  37. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.

    Java Training In Bangalore

    ReplyDelete
  38. Useful information. i am looking SAP HANA Online Training with real time project.

    ReplyDelete
  39. Really great post, Thank you for sharing This knowledge.Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!
    B.Com Project Center in Chennai | B.Com Project Center in Velachery

    ReplyDelete
  40. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    Austere Technologies | Best Cloud Solution services | Austere Technologies cloud Solutions

    ReplyDelete
  41. REALLY VERY EXCELLENT INFORMATION. I AM VERY GLAD TO SEE YOUR BLOG FOR THIS INFORMATION. THANKS FOR SHARING. KEEP UPDATING.

    NO.1 SYSTEM INTEGRATION SERVICES | SYSTEM INTEGRATION MIDDLEWARE | MASSIL TECHNOLOGIES

    ReplyDelete
  42. Very good informative article. Thanks for sharing such nice article, keep on up dating such good articles.

    Best IT Security Services | Austere Technologies

    ReplyDelete
  43. Wonderful post. I am learning so many things from your blog.keep posting..Summer Course in Thiruvanmiyur | Summer Course in Chennai

    ReplyDelete
  44. Good to know such things, sometimes they can be useful.

    ReplyDelete
  45. Interesting post! This is really helpful for me. I like it! Thanks for sharing!
    c sharp training in chennai | asp .net training in chennai

    ReplyDelete
  46. A decent path for you to take in more about screenplay composing is to watch completed items in real life. On the off chance that you need to be a content author for TV appears, assault yourself with TV programs that you need to make.how to write a screenplay

    ReplyDelete
  47. Great article, really very helpful content you made. Thank you, keep sharing.

    Best Degree Colleges Hyderabad | Avinash College of Commerce

    ReplyDelete
  48. I am really happy with your blog because your article is very unique and powerful for new reader.
    Selenium Training in Chennai

    ReplyDelete
  49. Thank you for sharing this valuable information. But get out of this busy life and find some peace with a beautiful trip book best Andaman honeymoon packages

    ReplyDelete
  50. Thanks for sharing the information, Salesforce experts a lot of openings in multi-level companies, for more information n
    Salesforce Training
    Salesforce certification Training program

    Salesforce Online Training in Bangalore

    ReplyDelete
  51. Thank you for sharing this valuable information. But get out this busy life and find some peace with a beautiful trip. book ANDAMAN HOLIDAY PACKAGES @ 35999

    ReplyDelete
  52. Hi Thanks for the nice information its very useful to read your blog. We provide best Find All Isfs Courses

    ReplyDelete
  53. "
    At the point when all activities and slimming down stage gets fizzled, Keto Primal works splendidly to give the coveted results by giving a thin and sharp body appearance since it is a characteristic craving boosting fat buster. The supplement additionally limits the nourishment desires or enthusiastic eating of people where they would encounter less appetite feel and that would offer them to stay in controlled calorie admission.

    The supplement attempts to influence a person to go fit as a fiddle and carry on with a sound and a la mode way of life. The weight reduction process that begins after the admission of the pills go normally, and you may encounter the results inside 2-3 weeks of time. The expansion of every common concentrate here attempts to support the stomach related framework and furthermore clean the colon framework to stay free from hurtful poison squander. It supports the digestion level and gives a lift to vitality and stamina level where you would have the capacity to exercise more without getting worn out and stay dormant way of life. Visit for more informations:
    Keto Primal
    Health Care 350"

    ReplyDelete
  54. "I like this post,And I guess that they having fun to read this post,they shall take a good site to make a information,thanks for sharing it to me.
    Read more here:
    kim kardashian sex tape
    porn sex video hd
    mia khalifa sex video
    sunny leone sexy movie"

    ReplyDelete
  55. Progressions in innovation have made life a great deal less demanding for every single one of us. This is valid for all parts of our lives, even in the region of our funds. Liquidating checks, for instance, is a considerable measure less demanding these days than it was previously.
    usacheckcashingstore.com/carson

    ReplyDelete