Jump to content

Bash shell scripting - check if input is a number

Go to solution Solved by omniomi,

Regex... Which is probably the purpose of the assignment. 
https://en.wikipedia.org/wiki/Regular_expression

#!/bin/bashecho Enter value one:read valueOneecho Enter value two:read valueTwoif ! [[ $valueOne =~ ^[0-9]+$ ]];    then        echo $valueOne "is not a number"fiif ! [[ $valueTwo =~ ^[0-9]+$ ]];    then        echo $valueTwo "is not a number"fi
omniomi@bitch:~$ ./test.shEnter value one:aEnter value two:fa is not a numberf is not a numberomniomi@bitch:~$ ./test.shEnter value one:sEnter value two:2s is not a numberomniomi@bitch:~$ ./test.shEnter value one:1Enter value two:2omniomi@bitch:~$ ./test.shEnter value one:12Enter value two:13

I have this task: Write a shell script which takes two parameters via the command line. The two parameters
should be numbers. Print an error message if the parameters were not numbers.

 

And I've been trying to figure it out for ages and it still doesn't work. If anyone could help me that would be great. I'm new to shell scripting and programming so the only way to learn is to ask.

 

Here is my code for what I currently have that doesn't work:

#!/bin/bashecho Enter value one:read valueOneecho Enter value two:read valueTwoif [[ $valueOne =~ -s ]]; then   echo $valueOne "is not a number"   if [[ $valueTwo =~ -s ]]; then   echo $valueTwo "is not a number"fi
   
   
Link to comment
Share on other sites

Link to post
Share on other sites

Regex... Which is probably the purpose of the assignment. 
https://en.wikipedia.org/wiki/Regular_expression

#!/bin/bashecho Enter value one:read valueOneecho Enter value two:read valueTwoif ! [[ $valueOne =~ ^[0-9]+$ ]];    then        echo $valueOne "is not a number"fiif ! [[ $valueTwo =~ ^[0-9]+$ ]];    then        echo $valueTwo "is not a number"fi
omniomi@bitch:~$ ./test.shEnter value one:aEnter value two:fa is not a numberf is not a numberomniomi@bitch:~$ ./test.shEnter value one:sEnter value two:2s is not a numberomniomi@bitch:~$ ./test.shEnter value one:1Enter value two:2omniomi@bitch:~$ ./test.shEnter value one:12Enter value two:13
Link to comment
Share on other sites

Link to post
Share on other sites

 

Regex... Which is probably the purpose of the assignment. 

https://en.wikipedia.org/wiki/Regular_expression

#!/bin/bashecho Enter value one:read valueOneecho Enter value two:read valueTwoif ! [[ $valueOne =~ ^[0-9]+$ ]];    then        echo $valueOne "is not a number"fiif ! [[ $valueTwo =~ ^[0-9]+$ ]];    then        echo $valueTwo "is not a number"fi
omniomi@bitch:~$ ./test.shEnter value one:aEnter value two:fa is not a numberf is not a numberomniomi@bitch:~$ ./test.shEnter value one:sEnter value two:2s is not a numberomniomi@bitch:~$ ./test.shEnter value one:1Enter value two:2omniomi@bitch:~$ ./test.shEnter value one:12Enter value two:13

Oh right is is limited from 0-9 then?

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

I have this task: Write a shell script which takes two parameters via the command line. The two parameters

should be numbers. Print an error message if the parameters were not numbers.

 

And I've been trying to figure it out for ages and it still doesn't work. If anyone could help me that would be great. I'm new to shell scripting and programming so the only way to learn is to ask.

 

Here is my code for what I currently have that doesn't work:

#!/bin/bashecho Enter value one:read valueOneecho Enter value two:read valueTwoif [[ $valueOne =~ -s ]]; then   echo $valueOne "is not a number"   if [[ $valueTwo =~ -s ]]; then   echo $valueTwo "is not a number"fi

For one thing you're forgetting to close the first "if" statement. You can do that either

with "fi" (thus having two separate if statements, with either none, one or both of them

being triggered depending on input), or you can go for an "elif" (bash's version of "else if"),

in which case either none or one if clause will be triggered.

Secondly, you seem to be mixing up some of bash's test constructs. "-s" tests if a file

is non-empty, like this:

 

if [[ -s $filename ]];then    echo "File not empty."fi
But for this task, at least as far as I can tell, pattern matching via regex would be the

way to go as per the previous post.

Side note: While asking questions is a good way to learn, the man pages are actually also

often a quite decent source of info, especially when it comes to traditional Unix tools.

Not meant as a criticism, just a hint. I've learnt a lot from them over the years, though

they can be a bit overwhelming to a newcomer admittedly. ;)

Also see here: http://www.gnu.org/software/bash/manual/bashref.html

The Bash hacker's wiki is also worth a read IMO: http://wiki.bash-hackers.org/start

EDIT:

Oh right is is limited from 0-9 then?

That solution will work with numbers with multiple digits. The regex used works as follows

^ beginning of line[0-9] any numerical digit+ one or more of those$ until end of line
Edited by alpenwasser

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

Oh right is is limited from 0-9 then?

 

^[0-9]+$ breaks down as such:

 

^ the beginning of the string.

[0-9] matches characters in the numeric range 0-9.

+ allows for between 1 match and unlimited matches (so 1, 12, 111, 114551, etc would all match.)

$ the end of the string.

 

Without the + it would only accept 1, 2, 3, 4, 5, 6, 7, 8, 9, or 0. With the plus it can be any length of number. All of the characters must of course be numbers (11n66 would not match.)

 

If you need to allow decimal places it would be ^[0-9]+([.][0-9]+)?$

Link to comment
Share on other sites

Link to post
Share on other sites

In case you're interested, a short intro to regular expressions in bash:

http://www.tldp.org/LDP/abs/html/x17129.html

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to comment
Share on other sites

Link to post
Share on other sites

For one thing you're forgetting to close the first "if" statement. You can do that either

with "fi" (thus having two separate if statements, with either none, one or both of them

being triggered depending on input), or you can go for an "elif" (bash's version of "else if"),

in which case either none or one if clause will be triggered.

Secondly, you seem to be mixing up some of bash's test constructs. "-s" tests if a file

is non-empty, like this:

 

if [[ -s $filename ]];then    echo "File not empty."fi
But for this task, at least as far as I can tell, pattern matching via regex would be the

way to go as per the previous post.

Side note: While asking questions is a good way to learn, the man pages are actually also

often a quite decent source of info, especially when it comes to traditional Unix tools.

Not meant as a criticism, just a hint. I've learnt a lot from them over the years, though

they can be a bit overwhelming to a newcomer admittedly. ;)

Also see here: http://www.gnu.org/software/bash/manual/bashref.html

The Bash hacker's wiki is also worth a read IMO: http://wiki.bash-hackers.org/start

EDIT:

That solution will work with numbers with multiple digits. The regex used works as follows

^ beginning of line[0-9] any numerical digit+ one or more of those$ until end of line

Nice, thanks a lot. My teacher literally tells us nothing, so I have to find bits with google searches xD.

 

For one thing you're forgetting to close the first "if" statement. You can do that either

with "fi" (thus having two separate if statements, with either none, one or both of them

being triggered depending on input), or you can go for an "elif" (bash's version of "else if"),

in which case either none or one if clause will be triggered.

Secondly, you seem to be mixing up some of bash's test constructs. "-s" tests if a file

is non-empty, like this:

 

if [[ -s $filename ]];then    echo "File not empty."fi
But for this task, at least as far as I can tell, pattern matching via regex would be the

way to go as per the previous post.

Side note: While asking questions is a good way to learn, the man pages are actually also

often a quite decent source of info, especially when it comes to traditional Unix tools.

Not meant as a criticism, just a hint. I've learnt a lot from them over the years, though

they can be a bit overwhelming to a newcomer admittedly. ;)

Also see here: http://www.gnu.org/software/bash/manual/bashref.html

The Bash hacker's wiki is also worth a read IMO: http://wiki.bash-hackers.org/start

EDIT:

That solution will work with numbers with multiple digits. The regex used works as follows

^ beginning of line[0-9] any numerical digit+ one or more of those$ until end of line

 

 

^[0-9]+$ breaks down as such:

 

^ the beginning of the string.

[0-9] matches characters in the numeric range 0-9.

+ allows for between 1 match and unlimited matches (so 1, 12, 111, 114551, etc would all match.)

$ the end of the string.

 

Without the + it would only accept 1, 2, 3, 4, 5, 6, 7, 8, 9, or 0. With the plus it can be any length of number. All of the characters must of course be numbers (11n66 would not match.)

 

If you need to allow decimal places it would be ^[0-9]+([.][0-9]+)?$

 

Nice, thanks a lot. My teacher literally tells us nothing, so I have to find bits with google searches xD.

   
   
Link to comment
Share on other sites

Link to post
Share on other sites

Nice, thanks a lot. My teacher literally tells us nothing, so I have to find bits with google searches xD.

 

Regex is a pain in the butt; Super useful (and necessary) if you're going into programming or systems administration though so worth learning regardless of what you want to do in IT.

Link to comment
Share on other sites

Link to post
Share on other sites

I found that looking things up yourself means you remember it longer then if someone just told you.

Can Anybody Link A Virtual Machine while I go download some RAM?

 

Link to comment
Share on other sites

Link to post
Share on other sites

Nice, thanks a lot. My teacher literally tells us nothing, so I have to find bits with google searches xD.

This is what helpdesk does, it is a good skill to have, when you get up to sysadmin level (assuming you are going down the sysadmin path, bash isn't really what programmers use) you need to re-learn everything every ~5 years.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×