how do i check if an input argument of a bash script is a base ten number? [on hold]











up vote
-4
down vote

favorite












I need to check if the input argument of a bash script is a base ten number. How can I do?










share|improve this question







New contributor




Zendaya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as unclear what you're asking by Jeff Schaller, mosvy, sebasth, RalfFriedl, sam 2 days ago


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2




    Possibly related to unix.stackexchange.com/questions/482822/… and to its duplicate (homework questions from the last few days/weeks).
    – Kusalananda
    2 days ago










  • Possible duplicate of How can I get my external IP address in a shell script?
    – Mr Shunz
    2 days ago






  • 3




    Is 5.0 a base 10 number? Or 4.2e2?
    – Jeff Schaller
    2 days ago

















up vote
-4
down vote

favorite












I need to check if the input argument of a bash script is a base ten number. How can I do?










share|improve this question







New contributor




Zendaya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as unclear what you're asking by Jeff Schaller, mosvy, sebasth, RalfFriedl, sam 2 days ago


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.











  • 2




    Possibly related to unix.stackexchange.com/questions/482822/… and to its duplicate (homework questions from the last few days/weeks).
    – Kusalananda
    2 days ago










  • Possible duplicate of How can I get my external IP address in a shell script?
    – Mr Shunz
    2 days ago






  • 3




    Is 5.0 a base 10 number? Or 4.2e2?
    – Jeff Schaller
    2 days ago















up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











I need to check if the input argument of a bash script is a base ten number. How can I do?










share|improve this question







New contributor




Zendaya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I need to check if the input argument of a bash script is a base ten number. How can I do?







linux shell-script






share|improve this question







New contributor




Zendaya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Zendaya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Zendaya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Zendaya

2




2




New contributor




Zendaya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Zendaya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Zendaya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as unclear what you're asking by Jeff Schaller, mosvy, sebasth, RalfFriedl, sam 2 days ago


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






put on hold as unclear what you're asking by Jeff Schaller, mosvy, sebasth, RalfFriedl, sam 2 days ago


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2




    Possibly related to unix.stackexchange.com/questions/482822/… and to its duplicate (homework questions from the last few days/weeks).
    – Kusalananda
    2 days ago










  • Possible duplicate of How can I get my external IP address in a shell script?
    – Mr Shunz
    2 days ago






  • 3




    Is 5.0 a base 10 number? Or 4.2e2?
    – Jeff Schaller
    2 days ago
















  • 2




    Possibly related to unix.stackexchange.com/questions/482822/… and to its duplicate (homework questions from the last few days/weeks).
    – Kusalananda
    2 days ago










  • Possible duplicate of How can I get my external IP address in a shell script?
    – Mr Shunz
    2 days ago






  • 3




    Is 5.0 a base 10 number? Or 4.2e2?
    – Jeff Schaller
    2 days ago










2




2




Possibly related to unix.stackexchange.com/questions/482822/… and to its duplicate (homework questions from the last few days/weeks).
– Kusalananda
2 days ago




Possibly related to unix.stackexchange.com/questions/482822/… and to its duplicate (homework questions from the last few days/weeks).
– Kusalananda
2 days ago












Possible duplicate of How can I get my external IP address in a shell script?
– Mr Shunz
2 days ago




Possible duplicate of How can I get my external IP address in a shell script?
– Mr Shunz
2 days ago




3




3




Is 5.0 a base 10 number? Or 4.2e2?
– Jeff Schaller
2 days ago






Is 5.0 a base 10 number? Or 4.2e2?
– Jeff Schaller
2 days ago












2 Answers
2






active

oldest

votes

















up vote
1
down vote













Your can use pattern matching (improve on the regex if you don't want to accept a leading 0):



if [[ $num =~ ^[+-]?[1-9][0-9]*$ ]]
then
echo Decimal
else
echo Not decimal
fi





share|improve this answer























  • Yes to both, edited.
    – xenoid
    2 days ago


















up vote
0
down vote













Compare it to itself while forcing base-ten interpretation.
As seen in the shell Arithmetic manual:




Constants with a leading 0 are interpreted as octal numbers. A leading
‘0x’ or ‘0X’ denotes hexadecimal. Otherwise, numbers take the form
[base#]n, where the optional base is a decimal number between 2 and 64
representing the arithmetic base, and n is a number in that base. If
base# is omitted, then base 10 is used. When specifying n, the digits
greater than 9 are represented by the lowercase letters, the uppercase
letters, ‘@’, and ‘_’, in that order. If base is less than or equal to
36, lowercase and uppercase letters may be used interchangeably to
represent numbers between 10 and 35.




Therefore if it's the first argument you can do...



EDITED AGAIN TO ACCOMMODATE NEGATIVE INPUT



#!/bin/bash
b=${1#-}
if [[ $b =~ ^[0-9]+$ ]]; then #If it's all numbers
a=$((10#$b)) #create a base 10 version
if [ "$a" == "$b" ]; then #if they LOOK the same...
echo "$b is Base 10!"; exit 1; fi; #It's base 10; exit.
fi
echo "$b is not base 10" #If it has letters or $a and $b look different, it's not base 10.


This code will take any input and tell you if it is base 10 or not. The arg is stripped of a negative sign first, then compared with $a non-numerically, because 012 -eq $((10#012)); but we don't want the script to tell us it's base ten because it isn't.



root@xxxx:~# ./testing.sh -124124
124124 is Base 10!
root@xxxx:~# ./testing.sh 58135
58135 is Base 10!
root@xxxx:~# ./testing.sh aksjflkfj929148
aksjflkfj929148 is not Base 10





share|improve this answer



















  • 1




    Doesn't work, non numeric args elicit a value too great for base
    – xenoid
    2 days ago










  • @xenoid Edited :) Now takes all inputs!
    – KuboMD
    2 days ago










  • @KuboLD Glad you liked my solution... But IMHO the adequate regexp is sufficient.
    – xenoid
    2 days ago










  • @xenoid Agreed. I just like the chance to take something and run with it whenever I can. This was fun!
    – KuboMD
    2 days ago


















2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













Your can use pattern matching (improve on the regex if you don't want to accept a leading 0):



if [[ $num =~ ^[+-]?[1-9][0-9]*$ ]]
then
echo Decimal
else
echo Not decimal
fi





share|improve this answer























  • Yes to both, edited.
    – xenoid
    2 days ago















up vote
1
down vote













Your can use pattern matching (improve on the regex if you don't want to accept a leading 0):



if [[ $num =~ ^[+-]?[1-9][0-9]*$ ]]
then
echo Decimal
else
echo Not decimal
fi





share|improve this answer























  • Yes to both, edited.
    – xenoid
    2 days ago













up vote
1
down vote










up vote
1
down vote









Your can use pattern matching (improve on the regex if you don't want to accept a leading 0):



if [[ $num =~ ^[+-]?[1-9][0-9]*$ ]]
then
echo Decimal
else
echo Not decimal
fi





share|improve this answer














Your can use pattern matching (improve on the regex if you don't want to accept a leading 0):



if [[ $num =~ ^[+-]?[1-9][0-9]*$ ]]
then
echo Decimal
else
echo Not decimal
fi






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









xenoid

2,5881724




2,5881724












  • Yes to both, edited.
    – xenoid
    2 days ago


















  • Yes to both, edited.
    – xenoid
    2 days ago
















Yes to both, edited.
– xenoid
2 days ago




Yes to both, edited.
– xenoid
2 days ago












up vote
0
down vote













Compare it to itself while forcing base-ten interpretation.
As seen in the shell Arithmetic manual:




Constants with a leading 0 are interpreted as octal numbers. A leading
‘0x’ or ‘0X’ denotes hexadecimal. Otherwise, numbers take the form
[base#]n, where the optional base is a decimal number between 2 and 64
representing the arithmetic base, and n is a number in that base. If
base# is omitted, then base 10 is used. When specifying n, the digits
greater than 9 are represented by the lowercase letters, the uppercase
letters, ‘@’, and ‘_’, in that order. If base is less than or equal to
36, lowercase and uppercase letters may be used interchangeably to
represent numbers between 10 and 35.




Therefore if it's the first argument you can do...



EDITED AGAIN TO ACCOMMODATE NEGATIVE INPUT



#!/bin/bash
b=${1#-}
if [[ $b =~ ^[0-9]+$ ]]; then #If it's all numbers
a=$((10#$b)) #create a base 10 version
if [ "$a" == "$b" ]; then #if they LOOK the same...
echo "$b is Base 10!"; exit 1; fi; #It's base 10; exit.
fi
echo "$b is not base 10" #If it has letters or $a and $b look different, it's not base 10.


This code will take any input and tell you if it is base 10 or not. The arg is stripped of a negative sign first, then compared with $a non-numerically, because 012 -eq $((10#012)); but we don't want the script to tell us it's base ten because it isn't.



root@xxxx:~# ./testing.sh -124124
124124 is Base 10!
root@xxxx:~# ./testing.sh 58135
58135 is Base 10!
root@xxxx:~# ./testing.sh aksjflkfj929148
aksjflkfj929148 is not Base 10





share|improve this answer



















  • 1




    Doesn't work, non numeric args elicit a value too great for base
    – xenoid
    2 days ago










  • @xenoid Edited :) Now takes all inputs!
    – KuboMD
    2 days ago










  • @KuboLD Glad you liked my solution... But IMHO the adequate regexp is sufficient.
    – xenoid
    2 days ago










  • @xenoid Agreed. I just like the chance to take something and run with it whenever I can. This was fun!
    – KuboMD
    2 days ago















up vote
0
down vote













Compare it to itself while forcing base-ten interpretation.
As seen in the shell Arithmetic manual:




Constants with a leading 0 are interpreted as octal numbers. A leading
‘0x’ or ‘0X’ denotes hexadecimal. Otherwise, numbers take the form
[base#]n, where the optional base is a decimal number between 2 and 64
representing the arithmetic base, and n is a number in that base. If
base# is omitted, then base 10 is used. When specifying n, the digits
greater than 9 are represented by the lowercase letters, the uppercase
letters, ‘@’, and ‘_’, in that order. If base is less than or equal to
36, lowercase and uppercase letters may be used interchangeably to
represent numbers between 10 and 35.




Therefore if it's the first argument you can do...



EDITED AGAIN TO ACCOMMODATE NEGATIVE INPUT



#!/bin/bash
b=${1#-}
if [[ $b =~ ^[0-9]+$ ]]; then #If it's all numbers
a=$((10#$b)) #create a base 10 version
if [ "$a" == "$b" ]; then #if they LOOK the same...
echo "$b is Base 10!"; exit 1; fi; #It's base 10; exit.
fi
echo "$b is not base 10" #If it has letters or $a and $b look different, it's not base 10.


This code will take any input and tell you if it is base 10 or not. The arg is stripped of a negative sign first, then compared with $a non-numerically, because 012 -eq $((10#012)); but we don't want the script to tell us it's base ten because it isn't.



root@xxxx:~# ./testing.sh -124124
124124 is Base 10!
root@xxxx:~# ./testing.sh 58135
58135 is Base 10!
root@xxxx:~# ./testing.sh aksjflkfj929148
aksjflkfj929148 is not Base 10





share|improve this answer



















  • 1




    Doesn't work, non numeric args elicit a value too great for base
    – xenoid
    2 days ago










  • @xenoid Edited :) Now takes all inputs!
    – KuboMD
    2 days ago










  • @KuboLD Glad you liked my solution... But IMHO the adequate regexp is sufficient.
    – xenoid
    2 days ago










  • @xenoid Agreed. I just like the chance to take something and run with it whenever I can. This was fun!
    – KuboMD
    2 days ago













up vote
0
down vote










up vote
0
down vote









Compare it to itself while forcing base-ten interpretation.
As seen in the shell Arithmetic manual:




Constants with a leading 0 are interpreted as octal numbers. A leading
‘0x’ or ‘0X’ denotes hexadecimal. Otherwise, numbers take the form
[base#]n, where the optional base is a decimal number between 2 and 64
representing the arithmetic base, and n is a number in that base. If
base# is omitted, then base 10 is used. When specifying n, the digits
greater than 9 are represented by the lowercase letters, the uppercase
letters, ‘@’, and ‘_’, in that order. If base is less than or equal to
36, lowercase and uppercase letters may be used interchangeably to
represent numbers between 10 and 35.




Therefore if it's the first argument you can do...



EDITED AGAIN TO ACCOMMODATE NEGATIVE INPUT



#!/bin/bash
b=${1#-}
if [[ $b =~ ^[0-9]+$ ]]; then #If it's all numbers
a=$((10#$b)) #create a base 10 version
if [ "$a" == "$b" ]; then #if they LOOK the same...
echo "$b is Base 10!"; exit 1; fi; #It's base 10; exit.
fi
echo "$b is not base 10" #If it has letters or $a and $b look different, it's not base 10.


This code will take any input and tell you if it is base 10 or not. The arg is stripped of a negative sign first, then compared with $a non-numerically, because 012 -eq $((10#012)); but we don't want the script to tell us it's base ten because it isn't.



root@xxxx:~# ./testing.sh -124124
124124 is Base 10!
root@xxxx:~# ./testing.sh 58135
58135 is Base 10!
root@xxxx:~# ./testing.sh aksjflkfj929148
aksjflkfj929148 is not Base 10





share|improve this answer














Compare it to itself while forcing base-ten interpretation.
As seen in the shell Arithmetic manual:




Constants with a leading 0 are interpreted as octal numbers. A leading
‘0x’ or ‘0X’ denotes hexadecimal. Otherwise, numbers take the form
[base#]n, where the optional base is a decimal number between 2 and 64
representing the arithmetic base, and n is a number in that base. If
base# is omitted, then base 10 is used. When specifying n, the digits
greater than 9 are represented by the lowercase letters, the uppercase
letters, ‘@’, and ‘_’, in that order. If base is less than or equal to
36, lowercase and uppercase letters may be used interchangeably to
represent numbers between 10 and 35.




Therefore if it's the first argument you can do...



EDITED AGAIN TO ACCOMMODATE NEGATIVE INPUT



#!/bin/bash
b=${1#-}
if [[ $b =~ ^[0-9]+$ ]]; then #If it's all numbers
a=$((10#$b)) #create a base 10 version
if [ "$a" == "$b" ]; then #if they LOOK the same...
echo "$b is Base 10!"; exit 1; fi; #It's base 10; exit.
fi
echo "$b is not base 10" #If it has letters or $a and $b look different, it's not base 10.


This code will take any input and tell you if it is base 10 or not. The arg is stripped of a negative sign first, then compared with $a non-numerically, because 012 -eq $((10#012)); but we don't want the script to tell us it's base ten because it isn't.



root@xxxx:~# ./testing.sh -124124
124124 is Base 10!
root@xxxx:~# ./testing.sh 58135
58135 is Base 10!
root@xxxx:~# ./testing.sh aksjflkfj929148
aksjflkfj929148 is not Base 10






share|improve this answer














share|improve this answer



share|improve this answer








edited 2 days ago

























answered 2 days ago









KuboMD

346




346








  • 1




    Doesn't work, non numeric args elicit a value too great for base
    – xenoid
    2 days ago










  • @xenoid Edited :) Now takes all inputs!
    – KuboMD
    2 days ago










  • @KuboLD Glad you liked my solution... But IMHO the adequate regexp is sufficient.
    – xenoid
    2 days ago










  • @xenoid Agreed. I just like the chance to take something and run with it whenever I can. This was fun!
    – KuboMD
    2 days ago














  • 1




    Doesn't work, non numeric args elicit a value too great for base
    – xenoid
    2 days ago










  • @xenoid Edited :) Now takes all inputs!
    – KuboMD
    2 days ago










  • @KuboLD Glad you liked my solution... But IMHO the adequate regexp is sufficient.
    – xenoid
    2 days ago










  • @xenoid Agreed. I just like the chance to take something and run with it whenever I can. This was fun!
    – KuboMD
    2 days ago








1




1




Doesn't work, non numeric args elicit a value too great for base
– xenoid
2 days ago




Doesn't work, non numeric args elicit a value too great for base
– xenoid
2 days ago












@xenoid Edited :) Now takes all inputs!
– KuboMD
2 days ago




@xenoid Edited :) Now takes all inputs!
– KuboMD
2 days ago












@KuboLD Glad you liked my solution... But IMHO the adequate regexp is sufficient.
– xenoid
2 days ago




@KuboLD Glad you liked my solution... But IMHO the adequate regexp is sufficient.
– xenoid
2 days ago












@xenoid Agreed. I just like the chance to take something and run with it whenever I can. This was fun!
– KuboMD
2 days ago




@xenoid Agreed. I just like the chance to take something and run with it whenever I can. This was fun!
– KuboMD
2 days ago



Popular posts from this blog

Entries order in /etc/network/interfaces

新発田市

Grub takes very long (several minutes) to open Menu (in Multi-Boot-System)