multi criterias search - dossier - console collection manager DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 9da2c6c29fe7d7b2c1b86bdccd5544e04b54913d DIR parent b63c0c8606127cdb2990ef9c4db1d1177ad9e8b1 HTML Author: Solene Rapenne <solene@perso.pw> Date: Tue, 17 Jul 2018 18:31:53 +0200 multi criterias search Diffstat: M cdb | 38 +++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) --- DIR diff --git a/cdb b/cdb @@ -1,6 +1,6 @@ #!/bin/sh -: ${REPO:=/home/solene/dev/cbd/games/} +: ${REPO:=collection} mkdir -p "${REPO}" || exit 1 if [ ! -d "${REPO}/.git" ] then @@ -136,7 +136,37 @@ add_value() { # $2 value search_value() { cd "$REPO" - grep -rl "$2" "$1" | cut -d '/' -f 2 + shift + + RESULT=$(mktemp /tmp/cdb.XXXXXXXXXXXX) + COUNT=0 + if [ -f "$RESULT" ] + then + while [ "$#" -gt 1 ] + do + ATTRIBUTE="$1" + VALUE="$2" + shift 2 + if [ ! -d "$ATTRIBUTE" ] + then + printf 'The attribute %s do not exists\n' "${ATTRIBUTE}" + exit 5 + fi + grep -rl "$VALUE" "$ATTRIBUTE" | cut -d '/' -f 2 >> "$RESULT" + COUNT=$(( COUNT + 1 )) + done + sort "$RESULT" | uniq -c | \ + awk -v count=$COUNT \ + '{ if($1==count) { + $1="" # remove uniq result + gsub(/^[ ]+/,"",$0) # remove leading space due to uniq + print + }}' + else + printf 'Cannot create a temporary file in /tmp\n' + exit 6 + fi + exit 0 } @@ -154,7 +184,7 @@ usage() { "cdb help" \ "cdb export" \ "cdb show [identifier]" \ - "cdb search [attribute [value]]" \ + "cdb search [attribute [value]] ... [attribute [value]] ..." \ "cdb identifier attribute value ... attribute value ..." exit 0 } @@ -175,7 +205,7 @@ if [ "$1" = "search" ]; then if [ "$#" -eq 1 ]; then show_attributes ; fi if [ "$#" -eq 2 ]; then list "$2" ; fi - if [ "$#" -eq 3 ]; then search_value "$2" "$3" ; fi + if [ "$#" -ge 3 ]; then search_value "$@" ; fi fi if [ "$#" -ge 3 ]; then add_value "$@" ; fi