View on GitHub

🧰 Utilities.sh

for a Better BASH experience!

download .ZIPdownload .TGZ

_ any

View Source
local ERROR=''
local ERROR_USAGE="[conditional expression], [items to search]...
Note: [items to search] may be piped from STDIN"

if [ $# -lt 1 ]
then
  ERROR=true
  local ERROR_REASON=ARGUMENT_LENGTH
  local ERROR_MINIMUM_ARGUMENTS_REQUIRED=1
fi

if [ "$ERROR" != true ]
then
  !include selectors/parseExpression
  !include arguments/withStdin
fi

if [ "$ERROR" != true ]
then
  if [ "${#ARGUMENTS[@]}" -eq 0 ]
  then
    return 1 # None matched, none provided.
  fi

  local SELECTOR_EXPRESSION_LEFT_HAND_SIDE=''
  for SELECTOR_EXPRESSION_LEFT_HAND_SIDE in "${ARGUMENTS[@]}"
  do
    !include selectors/getResult
    if [ "$SELECTOR_EXPRESSION_MATCHED_OK" = true ]
    then
      if [ "$UNDERSCORE_NOT" = true ]
      then
        return 1
      else
        return 0
      fi
    fi
  done

  if [ "$UNDERSCORE_NOT" = true ]
  then
    return 0
  else
    return 1
  fi
fi

!include error

Assert provided items contain item matching given conditional expression.

Arguments

  Type Description
$@ Array Collection of items to search
$1 Conditional Expression Conditional expression

Return Values

  Description
0 Item was found
1 Item was not found
2 Argument error

Example

if _ any =~ ^-- "$@"
then
  echo "At least one argument is a --flag"
fi
View Spec
expect { example } toBeEmpty
expect { example foo } toBeEmpty
expect { example --foo } toEqual "At least one argument is a --flag"
expect { example foo --foo --bar } toEqual "At least one argument is a --flag"