Better handle if single argument item doesnt exist - dossier - console collection manager DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit af4d6f9504d0bc8395b6bbd5cafadadbbd526fa2 DIR parent c56a8be32bc63cb4abe9d17312e308fa31e81bfd HTML Author: Solene Rapenne <solene@perso.pw> Date: Tue, 24 Jul 2018 10:46:08 +0200 Better handle if single argument item doesnt exist Diffstat: M dossier | 39 ++++++++++++++++++------------- 1 file changed, 23 insertions(+), 16 deletions(-) --- DIR diff --git a/dossier b/dossier @@ -13,27 +13,34 @@ else printf 'No current collection in use\n' fi +# return if an item exists +# $1 identifier +exists() { + cd "$REPO" + ls */$1 >/dev/null 2>&1 + return $? +} # displays the values of an identifier # $1 identifier show() { cd "${REPO}" - SEEN=0 - for attribute in * - do - if [ -f "${attribute}/${1}" ] - then - if [ "$SEEN" -eq 0 ] - then - printf "%s:\n" "$1" - SEEN=1 - fi - printf "%15s: %s\n" ${attribute} "$(cat "${attribute}/${1}")" - fi - done - if [ "$SEEN" -eq 1 ] + if exists "$1" then - exit 0 + SEEN=0 + for attribute in * + do + if [ -f "${attribute}/${1}" ] + then + if [ "$SEEN" -eq 0 ] + then + printf "%s:\n" "$1" + SEEN=1 + fi + printf "%15s: %s\n" ${attribute} "$(cat "${attribute}/${1}")" + fi + done + exit 0 else printf '%s is not in the library.\n' "$1" exit 1 @@ -272,6 +279,6 @@ fi if [ "$#" -ge 3 ]; then add_value "$@" ; fi # no command, maybe it's an item, try it -if [ "$#" -eq 1 ]; then show "$1" ; fi +if [ "$#" -eq 1 ] && exists "$1" ; then show "$1" ; fi usage