Ben Lawton
Ben Lawton

Reputation: 159

Bash: Checking a string contains numbers within a certain range

I'm writing a bash script to check the syntax of some simple MIPS instructions. One of the things I need it to do is to check that a valid register number has been given.

So let's say it reads the instruction...

add $s0, $s1, $s2

...it should check that the integer after the the s is within a given range. How would I implement that?

**If there's something easily Google-able to help me with this, please say. I thought there would be, but alas I wasn't able to find anything. :(*

Upvotes: 0

Views: 1120

Answers (1)

Peter Bowers
Peter Bowers

Reputation: 3093

If you want to allow $s0 to $s7 but not above then this would work:

grep -E '\$s([89]|\d\d)' myfile

If you want to go up to 23 but not beyond it gets a little more complicated:

grep -E '\$s(2[4-9]|[3-9]\d|\d\d\d)' myfile

For 31:

grep -E '\$s(3[2-9]|[4-9]\d|\d\d\d)' myfile

Upvotes: 1

Related Questions