shell script to check if input is a string/integer/floatBash script, cannote replace string in a file with escaped $ and &Evaluating a string in shell scriptCan you help me to understand this explanation of shell quoting?Shell script file (.sh) does not run, and throws an errorBash always evaluate Regex as trueexit code of diffreplace a string by variable in a file using bash scriptAutomated Shell script to run fdisk command with user inputCreate bash script that allows you to choose multiple options instead of just one?How to check if a package is installed from Bash?

What explanation do proponents of a Scotland-NI bridge give for it breaking Brexit impasse?

Parallel resistance in electric circuits

What organs or modifications would be needed for a life biological creature not to require sleep?

Insight into cavity resonators

How much would a 1 foot tall human weigh?

Why is this sentence grammatical?

Are there any rules about taking damage whilst holding your breath in combat?

International Orange?

geschafft or geschaffen? which one is past participle of schaffen?

What would happen if Protagoras v Euathlus were heard in court today?

Asked to Not Use Transactions and to Use A Workaround to Simulate One

Why is the UK still pressing on with Brexit?

Unable to find solution to 6 simultaneous equations

Is there any way to land a rover on the Moon without using any thrusters?

Some Prime Peerage

Should you only use colons and periods in dialogues?

If I want an interpretable model, are there methods other than Linear Regression?

In what state are satellites left in when they are left in a graveyard orbit?

How to control the output voltage of a solid state relay

In what sequence should an advanced civilization teach technology to medieval society to maximize rate of adoption?

Ethernet, Wifi and a little human psychology

How would you control supersoldiers in a late iron-age society?

Shouldn't countries like Russia and Canada support global warming?

Bash awk command with quotes



shell script to check if input is a string/integer/float


Bash script, cannote replace string in a file with escaped $ and &Evaluating a string in shell scriptCan you help me to understand this explanation of shell quoting?Shell script file (.sh) does not run, and throws an errorBash always evaluate Regex as trueexit code of diffreplace a string by variable in a file using bash scriptAutomated Shell script to run fdisk command with user inputCreate bash script that allows you to choose multiple options instead of just one?How to check if a package is installed from Bash?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








2















#!/bin/bash

read -p "Enter value:" val

echo "$val"|grep "^[0-9]*$"
val="$?"

if [[ $val == 0 ]]
then
echo "Integer"
exit
fi

echo $val|grep "^[a-zA-Z]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "String"
exit
fi


echo $val|grep "^[0-9]*.[0-9]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "Float"
exit
fi


If I enter a string like "ape" it says "grep invalid range" and then prints float. Where did I go wrong?










share|improve this question



















  • 1





    What locale are you using? Please edit your question and include the output of locale.

    – terdon
    8 hours ago






  • 1





    +1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

    – steeldriver
    8 hours ago


















2















#!/bin/bash

read -p "Enter value:" val

echo "$val"|grep "^[0-9]*$"
val="$?"

if [[ $val == 0 ]]
then
echo "Integer"
exit
fi

echo $val|grep "^[a-zA-Z]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "String"
exit
fi


echo $val|grep "^[0-9]*.[0-9]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "Float"
exit
fi


If I enter a string like "ape" it says "grep invalid range" and then prints float. Where did I go wrong?










share|improve this question



















  • 1





    What locale are you using? Please edit your question and include the output of locale.

    – terdon
    8 hours ago






  • 1





    +1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

    – steeldriver
    8 hours ago














2












2








2








#!/bin/bash

read -p "Enter value:" val

echo "$val"|grep "^[0-9]*$"
val="$?"

if [[ $val == 0 ]]
then
echo "Integer"
exit
fi

echo $val|grep "^[a-zA-Z]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "String"
exit
fi


echo $val|grep "^[0-9]*.[0-9]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "Float"
exit
fi


If I enter a string like "ape" it says "grep invalid range" and then prints float. Where did I go wrong?










share|improve this question














#!/bin/bash

read -p "Enter value:" val

echo "$val"|grep "^[0-9]*$"
val="$?"

if [[ $val == 0 ]]
then
echo "Integer"
exit
fi

echo $val|grep "^[a-zA-Z]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "String"
exit
fi


echo $val|grep "^[0-9]*.[0-9]*$"

val="$?"

if [[ $val == 0 ]]
then
echo "Float"
exit
fi


If I enter a string like "ape" it says "grep invalid range" and then prints float. Where did I go wrong?







bash scripts






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 8 hours ago









miamia

133 bronze badges




133 bronze badges










  • 1





    What locale are you using? Please edit your question and include the output of locale.

    – terdon
    8 hours ago






  • 1





    +1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

    – steeldriver
    8 hours ago













  • 1





    What locale are you using? Please edit your question and include the output of locale.

    – terdon
    8 hours ago






  • 1





    +1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

    – steeldriver
    8 hours ago








1




1





What locale are you using? Please edit your question and include the output of locale.

– terdon
8 hours ago





What locale are you using? Please edit your question and include the output of locale.

– terdon
8 hours ago




1




1





+1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

– steeldriver
8 hours ago






+1 - you should probably also rethink re-using the same variable name (val) for the exit status that you use for the value to be tested. Also == is a lexical test (to test an integer exit status better to use -eq ). And remember that . means "any character" in a grep regular expression - if you want a literal decimal point, use .

– steeldriver
8 hours ago











2 Answers
2






active

oldest

votes


















1
















This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



#!/bin/bash
read -p "Type a number or a string: " input
if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
echo "Input is an integer."

elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
echo "Input is a string."

elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
echo "Input is a float."

else
echo "Input is a string."
fi





share|improve this answer



























  • "00001" is a string not an integer :+

    – Rinzwind
    7 hours ago











  • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

    – terdon
    7 hours ago











  • I fixed the + and - problem, but it still chokes on 1..

    – terdon
    7 hours ago











  • I edited it so it doesn't choke on 1. anymore.

    – karel
    6 hours ago












  • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

    – Hannu
    6 hours ago



















2
















I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



LC_ALL=C yourscript.sh


Of course, that won't fix the other problem which is:



echo $val|grep "^[a-zA-Z]*$"

val="$?"


After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




In any case, this is really needlessly complex. All you really need is:



#!/bin/bash

val="$@"

[[ -z $val ]] && echo "No input!" && exit

if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
echo "Number!"
elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
echo "Float!"
elif [[ $val =~ [0-9] ]]; then
echo "Mixed, some numbers"
else
echo "No numbers!"
fi


Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



Also note that I changed some of your terms. I now consider 4 possibilities:



  1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

  2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

  3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

  4. The input has no numbers: print "No numbers".

I split 3 and 4, but you can join them and have them print the same, if you like.



Also, kudos to Karel for thinking of +N and -N numbers.






share|improve this answer





























    Your Answer








    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );














    draft saved

    draft discarded
















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1174142%2fshell-script-to-check-if-input-is-a-string-integer-float%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1
















    This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



    #!/bin/bash
    read -p "Type a number or a string: " input
    if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
    echo "Input is an integer."

    elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
    echo "Input is a string."

    elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
    echo "Input is a float."

    else
    echo "Input is a string."
    fi





    share|improve this answer



























    • "00001" is a string not an integer :+

      – Rinzwind
      7 hours ago











    • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

      – terdon
      7 hours ago











    • I fixed the + and - problem, but it still chokes on 1..

      – terdon
      7 hours ago











    • I edited it so it doesn't choke on 1. anymore.

      – karel
      6 hours ago












    • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

      – Hannu
      6 hours ago
















    1
















    This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



    #!/bin/bash
    read -p "Type a number or a string: " input
    if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
    echo "Input is an integer."

    elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
    echo "Input is a string."

    elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
    echo "Input is a float."

    else
    echo "Input is a string."
    fi





    share|improve this answer



























    • "00001" is a string not an integer :+

      – Rinzwind
      7 hours ago











    • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

      – terdon
      7 hours ago











    • I fixed the + and - problem, but it still chokes on 1..

      – terdon
      7 hours ago











    • I edited it so it doesn't choke on 1. anymore.

      – karel
      6 hours ago












    • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

      – Hannu
      6 hours ago














    1














    1










    1









    This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



    #!/bin/bash
    read -p "Type a number or a string: " input
    if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
    echo "Input is an integer."

    elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
    echo "Input is a string."

    elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
    echo "Input is a float."

    else
    echo "Input is a string."
    fi





    share|improve this answer















    This bash code returns integer for integers like 123, float for floating point numbers like 123.4 and string for any other input values like "123", One23 123. or 123.4.5.



    #!/bin/bash
    read -p "Type a number or a string: " input
    if [[ $input =~ ^[+-]?[0-9]+$ ]]; then
    echo "Input is an integer."

    elif [[ $input =~ ^[+-]?[0-9]+.$ ]]; then
    echo "Input is a string."

    elif [[ $input =~ ^[+-]?[0-9]+.?[0-9]*$ ]]; then
    echo "Input is a float."

    else
    echo "Input is a string."
    fi






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 6 hours ago

























    answered 7 hours ago









    karelkarel

    67.9k14 gold badges152 silver badges172 bronze badges




    67.9k14 gold badges152 silver badges172 bronze badges















    • "00001" is a string not an integer :+

      – Rinzwind
      7 hours ago











    • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

      – terdon
      7 hours ago











    • I fixed the + and - problem, but it still chokes on 1..

      – terdon
      7 hours ago











    • I edited it so it doesn't choke on 1. anymore.

      – karel
      6 hours ago












    • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

      – Hannu
      6 hours ago


















    • "00001" is a string not an integer :+

      – Rinzwind
      7 hours ago











    • This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

      – terdon
      7 hours ago











    • I fixed the + and - problem, but it still chokes on 1..

      – terdon
      7 hours ago











    • I edited it so it doesn't choke on 1. anymore.

      – karel
      6 hours ago












    • Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

      – Hannu
      6 hours ago

















    "00001" is a string not an integer :+

    – Rinzwind
    7 hours ago





    "00001" is a string not an integer :+

    – Rinzwind
    7 hours ago













    This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

    – terdon
    7 hours ago





    This will also consider + or - (alone) as integers. Same for an empty string. And it will report 1. as a float.

    – terdon
    7 hours ago













    I fixed the + and - problem, but it still chokes on 1..

    – terdon
    7 hours ago





    I fixed the + and - problem, but it still chokes on 1..

    – terdon
    7 hours ago













    I edited it so it doesn't choke on 1. anymore.

    – karel
    6 hours ago






    I edited it so it doesn't choke on 1. anymore.

    – karel
    6 hours ago














    Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

    – Hannu
    6 hours ago






    Leading zeroes => octal. $ echo $(( 0009 * 2 )) -> bash: 0009: value too great for base (error token is "0009")

    – Hannu
    6 hours ago














    2
















    I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



    LC_ALL=C yourscript.sh


    Of course, that won't fix the other problem which is:



    echo $val|grep "^[a-zA-Z]*$"

    val="$?"


    After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




    In any case, this is really needlessly complex. All you really need is:



    #!/bin/bash

    val="$@"

    [[ -z $val ]] && echo "No input!" && exit

    if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
    echo "Number!"
    elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
    echo "Float!"
    elif [[ $val =~ [0-9] ]]; then
    echo "Mixed, some numbers"
    else
    echo "No numbers!"
    fi


    Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



    Also note that I changed some of your terms. I now consider 4 possibilities:



    1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

    2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

    3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

    4. The input has no numbers: print "No numbers".

    I split 3 and 4, but you can join them and have them print the same, if you like.



    Also, kudos to Karel for thinking of +N and -N numbers.






    share|improve this answer































      2
















      I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



      LC_ALL=C yourscript.sh


      Of course, that won't fix the other problem which is:



      echo $val|grep "^[a-zA-Z]*$"

      val="$?"


      After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




      In any case, this is really needlessly complex. All you really need is:



      #!/bin/bash

      val="$@"

      [[ -z $val ]] && echo "No input!" && exit

      if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
      echo "Number!"
      elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
      echo "Float!"
      elif [[ $val =~ [0-9] ]]; then
      echo "Mixed, some numbers"
      else
      echo "No numbers!"
      fi


      Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



      Also note that I changed some of your terms. I now consider 4 possibilities:



      1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

      2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

      3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

      4. The input has no numbers: print "No numbers".

      I split 3 and 4, but you can join them and have them print the same, if you like.



      Also, kudos to Karel for thinking of +N and -N numbers.






      share|improve this answer





























        2














        2










        2









        I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



        LC_ALL=C yourscript.sh


        Of course, that won't fix the other problem which is:



        echo $val|grep "^[a-zA-Z]*$"

        val="$?"


        After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




        In any case, this is really needlessly complex. All you really need is:



        #!/bin/bash

        val="$@"

        [[ -z $val ]] && echo "No input!" && exit

        if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
        echo "Number!"
        elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
        echo "Float!"
        elif [[ $val =~ [0-9] ]]; then
        echo "Mixed, some numbers"
        else
        echo "No numbers!"
        fi


        Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



        Also note that I changed some of your terms. I now consider 4 possibilities:



        1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

        2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

        3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

        4. The input has no numbers: print "No numbers".

        I split 3 and 4, but you can join them and have them print the same, if you like.



        Also, kudos to Karel for thinking of +N and -N numbers.






        share|improve this answer















        I can't reproduce your error, but since it's complaining about an invalid range, it's most likely a locale issue. Try running your script again, but setting the locale:



        LC_ALL=C yourscript.sh


        Of course, that won't fix the other problem which is:



        echo $val|grep "^[a-zA-Z]*$"

        val="$?"


        After those lines, $val is no longer the value you gave, it is now the exit status of the grep, so everything after that is testing the wrong thing.




        In any case, this is really needlessly complex. All you really need is:



        #!/bin/bash

        val="$@"

        [[ -z $val ]] && echo "No input!" && exit

        if [[ "$val" =~ ^[+-]?[0-9]+$ ]]; then
        echo "Number!"
        elif [[ $val =~ ^[+-]?[0-9]*.[0-9]+$ ]]; then
        echo "Float!"
        elif [[ $val =~ [0-9] ]]; then
        echo "Mixed, some numbers"
        else
        echo "No numbers!"
        fi


        Note that I'm using val="$@" instead of read. This means you can now run your script as yourscript.sh input instead of having to type out the input every time. That way, you can see what you did in the history, you avoid typing errors, you can run the script automatically etc. It is generally a bad idea to use read and force your users to enter input.



        Also note that I changed some of your terms. I now consider 4 possibilities:



        1. The input has nothing but numbers: print "Number" (whether 001002 is an integer depends on what sort of maths you're thinking of).

        2. The input consists of 0 or more numbers, then a dot and then nothing but numbers (0 or more because .2 can be considered valid in some cases; if you don't want that, change the ^d*.d+$ to ^d+.d+$).

        3. The input has numbers but not only numbers: print "Mixed, some numbers". Note that this will also catch 1. which is not a valid float and not a valid integer.

        4. The input has no numbers: print "No numbers".

        I split 3 and 4, but you can join them and have them print the same, if you like.



        Also, kudos to Karel for thinking of +N and -N numbers.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 7 hours ago

























        answered 7 hours ago









        terdonterdon

        74k14 gold badges151 silver badges235 bronze badges




        74k14 gold badges151 silver badges235 bronze badges































            draft saved

            draft discarded















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1174142%2fshell-script-to-check-if-input-is-a-string-integer-float%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            François Viète Contents Biography Work and thought Bibliography See also Notes Further reading External links Navigation menup. 21Google Bookspp. 75–77Google BooksDe thou (from University of Saint Andrews)ArchivedGoogle BooksGoogle BooksGoogle BooksGoogle booksGoogle Bookscc-parthenay.frL'histoire universelle (fr)Universal History (en)ArchivedAdsabs.harvard.eduPagesperso-orange.frArchive.orgChikara Sasaki. Descartes' mathematical thought p.259Google BooksGoogle BooksGoogle Bookspp. 152 and onwardGoogle BooksGoogle BooksScribd.comGoogle Books1257-7979Google BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGoogle BooksGallica.bnf.frGoogle BooksGoogle Books"François Viète"Francois Viète: Father of Modern Algebraic NotationThe Lawyer and the GamblerAbout TarporleySite de Jean-Paul GuichardL'algèbre nouvelle"About the Harmonicon"cb120511976(data)1188044800000 0001 0913 5903n82164680ola2013766880073431702w6vt1sb70287374827140948071409480