0

I have a bash script which checks if the input date($1) falls in a range/ranges of dates. User inputs a date and (a or b, which is $2).

#!/usr/bin/env bash

today=$(date +"%Y%M%d")
declare -A dict=$2_range
a_range=( ["20140602"]="20151222" ["20170201"]="$today" )
b_range=( ["20140602"]="20150130" )

for key in ${!dict[@]}; do
  if [[ $1 -le ${dict[$key]} ]] && [[ $1 -ge $key ]]; then
    echo $1 falls in the range of $2
  fi
done

I do not know how to copy the associative array to the dict variable. Sample usage

$ ./script.sh 20170707 a

  20170707 falls in the range of a
pdna
  • 541
  • 1
  • 8
  • 17
  • `b_range` is not a range. – Jack Jul 15 '17 at 01:43
  • I have a start date and end date as a key-value pair. Its not really a range – pdna Jul 15 '17 at 01:45
  • Then why are there two elements in `a_range`? – Jack Jul 15 '17 at 01:48
  • There are two different ranges that the date can fall under for a. – pdna Jul 15 '17 at 01:49
  • Possible duplicate: https://stackoverflow.com/questions/11180714/how-to-iterate-over-an-array-using-indirect-reference. That is, maybe not an exact duplicate, but the solution here sounds like it does what you need. Scroll down to the first answer's second solution (i.e., skip the solution that uses `eval`). Or, just skip to the second answer, which uses the same solution (indirect reference). You might need to figure out how to modify it to work with associative arrays though. – Mike Holt Jul 15 '17 at 02:05
  • Also relevant: https://stackoverflow.com/questions/27456950/bash-indirect-reference-to-an-associative-array – Mike Holt Jul 15 '17 at 02:09
  • @MikeHolt I get the same syntax error for both solutions `operand expected error token is "$2range[@]"` Let me try the second one you suggested. Thanks – pdna Jul 15 '17 at 02:10
  • The second one uses `eval`, unless you have bash 4.3 or higher, in which case you can (and probably should) use the last solution (`declare -n` to define a reference). – Mike Holt Jul 15 '17 at 02:16
  • I have tried using `declare -n` and it doesn't seem to work either. It works for a indexed array but not for an associative array – pdna Jul 15 '17 at 02:23
  • Yeah, nevermind the first link, it only works for regular arrays. I can get it to iterate over the values of the referenced associative array, but I can't find a way to iterate over the keys using an indirect reference. – Mike Holt Jul 15 '17 at 02:24
  • Yeah same here. I have tried using `declare -A dict` `dict="\${!$2range[@]}"` and I get a different error now. `operand expected (error token is "${!a_range[@]}")` I don't get this error with a direct reference – pdna Jul 15 '17 at 02:31
  • Which shell version? If you have 4.3 or newer, namerefs are your friend. (If you **don't** have 4.3 or newer, maybe you should think about installing it?) – Charles Duffy Jul 15 '17 at 03:29
  • @CharlesDuffy GNU bash, version 4.3.11(1). Thanks, I am looking into it. – pdna Jul 15 '17 at 03:31

1 Answers1

2

You don't need to copy anything at all; you just need an alias.

#!/usr/bin/env bash

today=$(date +"%Y%M%d")

# you need declare -A **before** data is given.
# previously, these were sparse indexed arrays, not associative arrays at all.
declare -A a_range=( ["20140602"]="20151222" ["20170201"]="$today" )
declare -A b_range=( ["20140602"]="20150130" )

# declare -n makes dict a reference to (not a copy of) your named range.
declare -n dict="$2_range"

for key in "${!dict[@]}"; do
  if (( $1 <= ${dict[$key]} )) && (( $1 >= key )); then
    echo "$1 falls in the range of $2"
  fi
done

declare -n is the bash (4.3+) version of the ksh93 feature nameref; see http://wiki.bash-hackers.org/commands/builtin/declare#nameref

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • ah, I missed the quotes around $2_range. I only tried dict=$2_range so far. Thanks – pdna Jul 15 '17 at 03:34
  • Those quotes aren't strictly essential. It's `declare -n` that makes the difference. – Charles Duffy Jul 15 '17 at 03:35
  • I have tried `declare -n` and the above change before. Maybe I tried wrong combination of changes. Thanks for the explanations – pdna Jul 15 '17 at 03:43