Help Running a Check in Bash Script
Hey guys, so I wrote a small script that pretty much just takes in two numbers and counts from the first to the second, e.g.
unknown-hacker|544> count.sh 1 3 1 2 3 My problem is I want to make it so that if you input invalid parameters, such as non-numerical characters, more than 2 numbers, etc., you'd get an error message. Any suggestions? Thanks in advance. |
Hi,
Can you post the script you have this far. You are probably going to need regular expressions to check the numbers and $# to check the amount of parameters given. |
Sorry about that. Here's what I have so far:
#!/bin/bash declare -i INDEX if [ $1 -gt $2 ]; then INDEX=$1 while [[ $INDEX -gt $2 ]] || [[ $INDEX -eq $2 ]]; do echo $INDEX INDEX=$INDEX-1 done elif [ $1 -le $2 ]; then INDEX=$1 while [[ $INDEX -le $2 ]] || [[ $INDEX -eq $2 ]]; do echo $INDEX INDEX=$INDEX+1 done else echo $1 |
Hi,
Code:
#!/bin/bash I also edited your code (you have an else statement near the end that should be a fi. Hope this helps. |
This is great! I didn't know you could check all the parameters like that. Thanks for the help!
|
Actually, I might've spoke too soon. While the first check for the number of parameters works perfectly, the second check doesn't seem to work:
Code:
unknown-hacker|603> count.sh 2 1 |
Hi,
Are you sure you did not make a typo in the second check, I did check it at home and it worked. I'm not home any more and don't have access to a linux box at the moment. What that piece of code does: $1 and $2 hold the parameters that were given when starting the script. This part "$1" != "[0-9]*" checks if $1 is not (!=) a number. Same is done for $2. The OR part (||) makes sure that both are checked and if 1 (or both) are not a number the message is shown. I made a mistake, see post #9 for the solution..... Hope this helps. |
you can use a programming language such as Ruby(1.9+) instead of shell (if you are not doing homework)
Code:
#!/usr/bin/env ruby Code:
$ ruby test.rb 2 4 |
Hi again,
Just had some more time and had another look: I made a mistake, sorry about that. Change this line: if [[ "$1" != "[0-9]*" ]] || [[ "$2" != "[0-9]*" ]] to this: if ! [[ "$1" =~ "^[0-9]+$" ]] || ! [[ "$2" =~ "^[0-9]+$" ]] hOPE THIS HELPS. |
You can't quote the rhs of =~ and have it treated as a regex, so you need to remove those. Also, there's some errors in the original script: I get '1 2 3 1' when I give it '1 3', for instance.
And I hope this is for the exercise. 'seq' is a perfectly good pre-existing tool. :) -- Oh - I forgot to mention that I don't know how you'd want to handle both numbers being the same, but I'd also changed it to just replicate seq's behavior and print the same digit. Code:
#!/bin/bash |
Hi,
Quote:
Just curious. |
Yep - it definitely doesn't work on anything since bash 3.1. Do you have compat* set via shopt? Or are you using an old bash? Or has Debian (or your distro) done something weird?
|
Hi,
Quote:
OS: SLES 10.2 SP2 Bash version: 3.1.17 Just a bare test with a minimal amount of lines: Code:
~> ./blaat.sh 1 1 Does anything since bash 3.1 include 3.1? Otherwise it is explained. I'm convinced that the OP should use your example, just to make sure!! |
Quote:
|
If this is for non-learning purposes, use 'seq' to count.
|
All times are GMT -5. The time now is 06:14 PM. |