0

So I am still learning shell scripting and am trying to figure out how to return a string of numbers. My code is as follows:

#!/bin/bash
 read -p "Enter NUM1 " NUM1
 read -p "Enter NUM2 " NUM2
    if [ $NUM1 -gt $NUM2 ]; then $NUM1=g1 && $NUM2=g2
    elif [ $NUM2 -gt $NUM1 ]; then $NUM2=g1 && $NUM1=g2; fi

 for VALUE in $@; do
    if [ $VALUE -lt $g1 ] && [ $VALUE -gt $g2 ]; then COUNT=$((COUNT+1)); fi
 done
 echo $VALUE happened $COUNT times

Essentially I would pass a list of numbers and want the number of matches between g1 and g2 returned as well as the the matches themselves. Any help is appreciated.

Slykin
  • 33
  • 1
  • 3
  • 1
    There's not really a concept of "returned" in bash scripting. – Kevin Jun 27 '13 at 18:30
  • I guess I mean to echo back the numbers within the range. – Slykin Jun 27 '13 at 18:33
  • 1
    What are `g1` and `g2` supposed to be? – Kevin Jun 27 '13 at 18:38
  • g1 and g2 are supposed to be the 2 endpoints of the range. I actually just tried flipping the assignment around and it worked. But anyways I test to see if the first or second number is greater than the other so when creating the range I can have a definite variable to work with. – Slykin Jun 27 '13 at 18:41
  • Anything wrong with saying something like `if the value is in range, then output number, increment count, fi`? – Manny D Jun 27 '13 at 18:50

1 Answers1

0

The problem is that you inverted the order on the g1 and g2 assignment!

Here's your script working:

#!/bin/bash
 read -p "Enter NUM1 " NUM1
 read -p "Enter NUM2 " NUM2
    if [ $NUM1 -gt $NUM2 ]; then g1=$NUM1 && g2=$NUM2
    elif [ $NUM2 -gt $NUM1 ]; then g1=$NUM2 && g2=$NUM1; fi

 for VALUE in $@; do
    if [ $VALUE -lt $g1 ] && [ $VALUE -gt $g2 ]; then COUNT=$((COUNT+1)); fi
 done
 echo $VALUE happened $COUNT times
Renato Todorov
  • 567
  • 3
  • 15