Create a range of events in bash

I want to enter two and three digits into a function and output a range of events, i.e. enter 33and 66and withdraw 3[3-9] | [4-5][0-9] | 6[0-6]or enter 33and 666and withdraw3[3-9] | [4-9][0-9] | [1-5][0-9][0-9] | 6[0-5][0-9] | 66[0-6]

What are some ideas as to how? Thank!

+4
source share
3 answers

Just want to share the final code: https://gist.github.com/zaydek/1627329f88c444f6d71d5cbc0cda616f :)

function enc {

    a=${1: -3:1}; b=${1: -2:1}; c=${1: -1}
    d=${2: -3:1}; e=${2: -2:1}; f=${2: -1}

    if   (( $[a] < $[d] )); then

        if   (( $b$c == 00 && $e$f == 99 )); then echo [$a-$d][0-9][0-9]
        elif (( $b$c == 00 )); then if (( $[a+1] < $d )); then echo [$a-$[d-1]][0-9][0-9] "|"; echo $(enc $[d]00 $d$e$f)
                                                          else echo $a[0-9][0-9] "|"; echo $(enc $[a+1]00 $d$e$f); fi

        else echo $(enc $a$b$c $[a]99) "|"; echo $(enc $[a+1]00 $d$e$f); fi

    elif (( $b < $e )); then

        if   (( $c == 0 && $f == 9 )); then echo $a[$b-$e][0-9]
        elif (( $c == 0 )); then if (( $[b+1] < $e )); then echo $a[$b-$[e-1]][0-9] "|"; echo $(enc $a$[e]0 $d$e$f)
                                                       else echo $a$b[0-9] "|"; echo $(enc $a$[e]0 $d$e$f); fi

        else echo $(enc $a$b$c $a$[b]9) "|"; echo $(enc $a$[b+1]0 $d$e$f); fi

    else

        if (( $c == $f )); then echo $a$b$c
        else echo $a$b[$c-$f]; fi; fi

}

function int {

    if [[ $1 = *.* ]]; then

        integer=${1%.*}
        decimal=${1#*.}

        if (( ${decimal:0:1} >= 5 )); then integer=$[integer+1]; fi

         echo $integer
    else echo $1; fi

}

function cse {

    minimum=$(int $(echo $1 $2 | awk '{ print ($1/$2)*( 90) }'))
    maximum=$(int $(echo $1 $2 | awk '{ print ($1/$2)*(110) }'))

    echo $(enc $minimum $maximum)

}

cse $1 $2

cse , , .. 90 110 -10% + 10%, minimum maximum enc, , case ( ).

: bash, , case, . 2 3 , .

+1

. 2 0-99, , .

:

./range.sh 1 390[1-9] | [1-2][0-9] | 3[0-9]

./range.sh 21 492[1-9] | [3-4][0-9]

./range.sh 33 663[3-9] | [4-5][0-9] | 6[0-6]

./range.sh 33 993[3-9] | [4-8][0-9] | 9[0-9]

: range.sh, , :

#!/bin/bash

LHS=$1
RHS=$2
B2=10

D2=$(echo "($RHS-$LHS)/$B2" | bc)
if [[ ${D2} -gt 0 ]]; then
  S2=(`seq $(echo "$LHS/$B2" | bc) $(echo "$LHS/$B2+$D2" | bc)`)
  #echo ${S2[@]}
  C=${#S2[@]}
  #echo $C

  L1=$(echo "$LHS%$B2" | bc)
  if [[ ${L1} -lt 9 ]]; then 
    L1R="[${L1}-9]"
  else
    L1R="9"
  fi

  R1=$(echo "$RHS%$B2" | bc)
  if [[ ${R1} -gt 0 ]]; then
    R1R="[0-${R1}]"
  else
    R1R="[0-9]"
  fi

  if [[ ${C} -gt 3 ]]; then
    echo -n $(echo "$LHS/$B2" | bc)$L1R
    echo -n " | "[${S2[1]}-${S2[$C-2]}][0-9]
    echo -n " | "${S2[$C-1]}$R1R
  elif [[ ${C} -eq 3 ]]; then
    if [[ ${S2[0]} -eq 0 ]]; then
      echo -n 0$L1R
    else
      echo -n $(echo "$LHS/$B2" | bc)$L1R
    fi
    echo -n " | "[${S2[1]}-${S2[$C-1]}][0-9]
  fi

else
  echo [$LHS-$RHS]
fi

echo
0

:

#!/bin/bash
l=$1
h=$2

function get_nearest_upper_10th_mutlitple()
{
    v=$(($1+10))
    echo "${v%?}0"
}
function get_nearest_lower_10th_mutlitple()
{
    v=$1
    echo "${v%?}0"
}

temp="$(printf '%d' $l | wc -m)$(printf '%d' $h | wc -m)"
case $temp in
    11) echo "[$l-$h]"      ;;
    22|12)
        a=$([[ $(($l%10)) -eq 0 ]] && echo $l || echo $(($(get_nearest_upper_10th_mutlitple $l)-1)))
        if [[ $(printf '%d' $a | wc -m) -eq 1 ]]; then
            echo [$l-$a]
        else
            [[ $a -gt $l ]] && echo "${l%?}[${l#?}-${a#?}]" || echo $a
        fi

        b=$([[ $(($h%10)) -eq 0 ]] && echo $h || get_nearest_lower_10th_mutlitple $h)

        x=$((a+1))
        y=$((b-1))
        echo "[$(echo ${x%?}~${y%?} | sed 's/~/\n/g' | sort -n | uniq | paste -s -d '-')][${x#?}-${y#?}]"

        [[ $b -lt $h ]] && echo "${h%?}[${b#?}-${h#?}]" || echo $b
esac

:

$ ./script.bash 2 8
[2-8]
$ ./script.bash 2 6
[2-6]
$ ./script.bash 2 68
[2-9]
[1-5][0-9]
6[0-8]
$ ./script.bash 23 68
2[3-9]
[3-5][0-9]
6[0-8]
$ ./script.bash 23 99
2[3-9]
[3-8][0-9]
9[0-9]
$ ./script.bash 23 80
2[3-9]
[3-7][0-9]
80
$ ./script.bash 40 80
40
[4-7][1-9]
80
$ ./script.bash 40 80 | paste -s -d '|'
40|[4-7][1-9]|80
$ ./script.bash 35 55
3[5-9]
[4][0-9]
5[0-5]

You can use paste -s -d '|'ORed to combine individual ranges into a single range.

You can use the existing functions that I did to extend them for 3-digit numbers (I will post a three-digit version too after a while with different cases for 13and 12).

0
source

All Articles