sets.sh 1.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12

# LECA Bash library
#
# The LECA bash library provides a set of function used for helping
# development of bash script mainly to write job script on the luke
# cluster
#
# The files from the LECA Bash library must be sourced from your main script
#
#

include logging
13 14
include ifs

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
logdebug "Load sets package"

function newset() {
  eval declare -a "${1}"
}

function setsize() {
  eval "echo \${#${1}[@]}"
  
}

function setaddvalue() {
  local S="${1}"
  local V="${2}"
  eval $S[$(setsize $S)]=$V
30 31

  pushifs $'\n'
32
  eval "$S=(\$(echo \"\${$S[*]}\" | sort -u))"
33
  popifs
34 35 36 37 38
}

function setdelvalue() {
  local S="${1}"
  local V="${2}"
39 40

  pushifs $'\n'
41
  eval "$S=(\$(echo \"\${$S[*]}\" | grep -v \"$V\"))"
42
  popifs
43 44 45 46 47
}

function setcontains() {
  local S="${1}"
  local V="${2}"
48 49

  pushifs $'\n'
50
  eval "echo \"\${$S[*]}\" | grep -q \"$V\""
51 52
  popifs

53 54 55 56 57 58 59 60
  local status=$?
  return $status
}

function setunion() {
  local S="${1}"
  local S1="${2}"
  local S2="${3}"
61 62

  pushifs $'\n'
63
  eval "$S=(\$((echo \"\${$S1[*]}\";echo \"\${$S2[*]}\") | sort -u))"
64
  popifs
65 66 67 68 69 70
}

function setintersec() {
  local S="${1}"
  local S1="${2}"
  local S2="${3}"
71 72

  pushifs $'\n'
73 74 75 76 77
  eval "$S=(\$((echo \"\${$S1[*]}\";echo \"\${$S2[*]}\") | \
               sort | \
               uniq -c | \
               awk '(\$1==2)' | \
               sed 's/^ *2 //'))"
78
  popifs
79
}