1

I was told that in the shell it is possible to check if number X is three times bigger than Y without using expr. I have been thinking about that for a few days but really can't think of any solution.

Does anyone have any? Thank you for help.

EDIT: out of curiosity, is it also possible to check if number is a multiple of another using regex?

psmears
  • 26,070
  • 4
  • 40
  • 48
Superian007
  • 395
  • 7
  • 25
  • 1
    You can use `bc` or if using BASH then use `((...))` – anubhava Mar 24 '15 at 14:48
  • 1
    Which shell? Some have a lot more features than others... – Carl Norum Mar 24 '15 at 14:56
  • 1
    And why would you ever want to use a regular expression for a problem like this one? – Carl Norum Mar 24 '15 at 14:57
  • Regular expressions match strings. They don't do math. – Ian McLaird Mar 24 '15 at 14:57
  • 1
    @IanMcLaird: Actually you can check for divisibility of numbers using regular expressions. Of course, that certainly doesn't mean you *should*, quite the reverse :-) – psmears Mar 24 '15 at 14:58
  • @CarlNorum most basic one (I don't know all types but all used commands should be in every another shell). I saw several topics where people were solving for example if number X is divisible by 3 using regular expressions. So I was wondering If is it possible to do dynamically. I get two numbers on standard input and check if number X is for example two times bigger then number Y. – Superian007 Mar 24 '15 at 15:01
  • Yep. I sort of stand corrected. http://stackoverflow.com/questions/12403842/check-number-divisibility-with-regular-expressions – Ian McLaird Mar 24 '15 at 15:07
  • Though of note, all of the answers on that question stop short of actually supplying a regex to check for divisibility by 7. They create state machines that do it or convert bases, but it's not as simple as just "multiples of foo look like bar" – Ian McLaird Mar 24 '15 at 15:13

2 Answers2

2

If both numbers are represented in "base 1", then checking if one number is a multiple of another is trivial with regular expressions.

x=11111    # 5
y=111111111111111  # 15

# Using bash's regular expression operator for clarity
if [[ $y =~ ($x)* ]]; then
    echo "y is a multiple of x"
fi

It's almost certain that you don't have your numbers in base 1, and it's an inefficient representation anyway, so you are better off using actual math. Using POSIX arithmetic:

x=5
y=15
z=$((y/x))

if [ $((z * x)) -eq "$y" ]; then
    echo "$y is a multiple of $x"
fi

Since POSIX doesn't support floating-point arithmetic, you can check for divisibility by verifying that the quotient z times the divisor x is equal to the dividend y, which means that there was no remainder left over from the division.

chepner
  • 497,756
  • 71
  • 530
  • 681
0

Two ways I can think of in bourne shell plus another one for Bash suggested by John Kugelman:

$ cat test
#!/bin/bash

# For all bourne shells    
[ $(echo "3*$2" | bc) -eq $1 ] && echo $1 is three times bigger than $2
[ $((3*$2)) -eq $1 ]           && echo $1 is three times bigger than $2

# Only for bash
((3*$2 == $1))                 && echo $1 is three times bigger than $2

$ sh ./test 10 30
$ sh ./test 30 10
30 is three times bigger than 10
30 is three times bigger than 10
30 is three times bigger than 10
Community
  • 1
  • 1
Antxon
  • 1,855
  • 15
  • 21