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
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 accountSign in
Already have an account? Sign in here.
Sign In Now